Skip to content

Commit

Permalink
Webdriver: fix window ID parsing (#3364)
Browse files Browse the repository at this point in the history
Regression snuck with 86dd367. Added a test that should work on
older branches to doublecheck.

b/343311752
  • Loading branch information
kaidokert committed May 29, 2024
1 parent 05cfe09 commit c8170cc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions cobalt/webdriver/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ target(gtest_target_type, "webdriver_test") {
testonly = true

sources = [
"dispatcher_test.cc",
"execute_test.cc",
"get_element_text_test.cc",
"is_displayed_test.cc",
Expand Down
61 changes: 61 additions & 0 deletions cobalt/webdriver/dispatcher_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/webdriver/dispatcher.h"

#include <utility>

#include "base/optional.h"
#include "base/values.h"
#include "cobalt/webdriver/protocol/log_type.h"
#include "cobalt/webdriver/protocol/window_id.h"
#include "cobalt/webdriver/session_driver.h"
#include "cobalt/webdriver/util/command_result.h"
#include "cobalt/webdriver/util/dispatch_command_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace cobalt {
namespace webdriver {

TEST(FromValueTest, WindowID) {
base::Value::Dict dict;
dict.Set("name", "foo");
base::Value value(std::move(dict));
auto result = util::internal::FromValue<protocol::WindowId>(&value);
EXPECT_EQ(result->id(), "foo");
}

TEST(FromValueTest, WindowID_Fail) {
base::Value empty;
auto result = util::internal::FromValue<protocol::WindowId>(&empty);
EXPECT_EQ(result, base::nullopt);
}

TEST(FromValueTest, LogType) {
base::Value::Dict dict;
dict.Set("type", "foo");
base::Value value(std::move(dict));
auto result = util::internal::FromValue<protocol::LogType>(&value);
EXPECT_EQ(result->type(), "foo");
}

TEST(FromValueTest, LogType_Fail) {
base::Value empty;
auto result = util::internal::FromValue<protocol::LogType>(&empty);
EXPECT_EQ(result, base::nullopt);
}

} // namespace webdriver
} // namespace cobalt
11 changes: 10 additions & 1 deletion cobalt/webdriver/protocol/log_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ namespace cobalt {
namespace webdriver {
namespace protocol {

namespace {
const char kLogTypeKey[] = "type";
}


base::Optional<LogType> LogType::FromValue(const base::Value* value) {
const base::Value::Dict* dictionary_value = value->GetIfDict();
if (!dictionary_value) {
return absl::nullopt;
}
return base::nullopt;
auto type = dictionary_value->FindString(kLogTypeKey);
if (!type) {
return absl::nullopt;
}
return LogType(*type);
}

} // namespace protocol
Expand Down
6 changes: 5 additions & 1 deletion cobalt/webdriver/protocol/window_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ base::Optional<WindowId> WindowId::FromValue(const base::Value* value) {
if (!dictionary_value) {
return absl::nullopt;
}
return base::nullopt;
auto window_id = dictionary_value->FindString(kWindowNameKey);
if (!window_id) {
return absl::nullopt;
}
return WindowId(*window_id);
}

} // namespace protocol
Expand Down
8 changes: 4 additions & 4 deletions cobalt/webdriver/util/dispatch_command_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ std::unique_ptr<base::Value> ToValue(const base::Optional<T>& value) {

// Template specialization for std::string.
template <>
std::unique_ptr<base::Value> ToValue(const std::string& value) {
inline std::unique_ptr<base::Value> ToValue(const std::string& value) {
return std::unique_ptr<base::Value>(new base::Value(value));
}

// Template specialization for bool.
template <>
std::unique_ptr<base::Value> ToValue(const bool& value) {
inline std::unique_ptr<base::Value> ToValue(const bool& value) {
return std::unique_ptr<base::Value>(new base::Value(value));
}

Expand All @@ -108,7 +108,7 @@ std::unique_ptr<base::Value> ToValue(const CommandResult<R>& command_result) {
}

template <>
std::unique_ptr<base::Value> ToValue(
inline std::unique_ptr<base::Value> ToValue(
const CommandResult<void>& command_result) {
return std::make_unique<base::Value>();
}
Expand All @@ -121,7 +121,7 @@ base::Optional<T> FromValue(const base::Value* value) {
}

template <>
base::Optional<GURL> FromValue(const base::Value* value) {
inline base::Optional<GURL> FromValue(const base::Value* value) {
const char kUrlKey[] = "url";

const base::Value::Dict* dictionary_value = value->GetIfDict();
Expand Down

0 comments on commit c8170cc

Please sign in to comment.