Skip to content

Commit

Permalink
Cherry pick PR #3364: Webdriver: backport test coverage (#3372)
Browse files Browse the repository at this point in the history
Refer to the original PR: #3364

Adding test coverage for some webdriver commands.

b/343311752

Co-authored-by: Kaido Kert <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and kaidokert committed May 30, 2024
1 parent 145a536 commit 032bff3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cobalt/webdriver/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,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 All @@ -150,6 +151,7 @@ target(gtest_target_type, "webdriver_test") {
"//testing/gmock",
"//testing/gtest",
"//third_party/devtools:devtools_all_files",
"//url",
]

data_deps = [
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::DictionaryValue dict;
dict.SetString("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::DictionaryValue dict;
dict.SetString("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
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 @@ -85,13 +85,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 @@ -101,7 +101,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 @@ -114,7 +114,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";
std::string url;
const base::DictionaryValue* dictionary_value;
Expand Down

0 comments on commit 032bff3

Please sign in to comment.