From c8170ccbfc19faff9f9acbe07016ca0a51a725ba Mon Sep 17 00:00:00 2001 From: Kaido Kert Date: Wed, 29 May 2024 15:08:37 -0700 Subject: [PATCH] Webdriver: fix window ID parsing (#3364) Regression snuck with 86dd36727e2. Added a test that should work on older branches to doublecheck. b/343311752 --- cobalt/webdriver/BUILD.gn | 1 + cobalt/webdriver/dispatcher_test.cc | 61 +++++++++++++++++++ cobalt/webdriver/protocol/log_type.cc | 11 +++- cobalt/webdriver/protocol/window_id.cc | 6 +- .../webdriver/util/dispatch_command_factory.h | 8 +-- 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 cobalt/webdriver/dispatcher_test.cc diff --git a/cobalt/webdriver/BUILD.gn b/cobalt/webdriver/BUILD.gn index 5cf3bfe2abff..a43cd40dfde1 100644 --- a/cobalt/webdriver/BUILD.gn +++ b/cobalt/webdriver/BUILD.gn @@ -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", diff --git a/cobalt/webdriver/dispatcher_test.cc b/cobalt/webdriver/dispatcher_test.cc new file mode 100644 index 000000000000..0e0b654ce840 --- /dev/null +++ b/cobalt/webdriver/dispatcher_test.cc @@ -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 + +#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(&value); + EXPECT_EQ(result->id(), "foo"); +} + +TEST(FromValueTest, WindowID_Fail) { + base::Value empty; + auto result = util::internal::FromValue(&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(&value); + EXPECT_EQ(result->type(), "foo"); +} + +TEST(FromValueTest, LogType_Fail) { + base::Value empty; + auto result = util::internal::FromValue(&empty); + EXPECT_EQ(result, base::nullopt); +} + +} // namespace webdriver +} // namespace cobalt diff --git a/cobalt/webdriver/protocol/log_type.cc b/cobalt/webdriver/protocol/log_type.cc index 6bcc7d87a440..38fe6cbbead6 100644 --- a/cobalt/webdriver/protocol/log_type.cc +++ b/cobalt/webdriver/protocol/log_type.cc @@ -18,12 +18,21 @@ namespace cobalt { namespace webdriver { namespace protocol { +namespace { +const char kLogTypeKey[] = "type"; +} + + base::Optional 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 diff --git a/cobalt/webdriver/protocol/window_id.cc b/cobalt/webdriver/protocol/window_id.cc index b63667956cbb..17bec6d4feb7 100644 --- a/cobalt/webdriver/protocol/window_id.cc +++ b/cobalt/webdriver/protocol/window_id.cc @@ -26,7 +26,11 @@ base::Optional 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 diff --git a/cobalt/webdriver/util/dispatch_command_factory.h b/cobalt/webdriver/util/dispatch_command_factory.h index 6cf6a7bf865d..e7e8b7c2ecd0 100644 --- a/cobalt/webdriver/util/dispatch_command_factory.h +++ b/cobalt/webdriver/util/dispatch_command_factory.h @@ -92,13 +92,13 @@ std::unique_ptr ToValue(const base::Optional& value) { // Template specialization for std::string. template <> -std::unique_ptr ToValue(const std::string& value) { +inline std::unique_ptr ToValue(const std::string& value) { return std::unique_ptr(new base::Value(value)); } // Template specialization for bool. template <> -std::unique_ptr ToValue(const bool& value) { +inline std::unique_ptr ToValue(const bool& value) { return std::unique_ptr(new base::Value(value)); } @@ -108,7 +108,7 @@ std::unique_ptr ToValue(const CommandResult& command_result) { } template <> -std::unique_ptr ToValue( +inline std::unique_ptr ToValue( const CommandResult& command_result) { return std::make_unique(); } @@ -121,7 +121,7 @@ base::Optional FromValue(const base::Value* value) { } template <> -base::Optional FromValue(const base::Value* value) { +inline base::Optional FromValue(const base::Value* value) { const char kUrlKey[] = "url"; const base::Value::Dict* dictionary_value = value->GetIfDict();