| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h" | 10 #include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h" |
| 11 #include "chrome/test/chromedriver/chrome/recorder_devtools_client.h" |
| 11 #include "chrome/test/chromedriver/chrome/status.h" | 12 #include "chrome/test/chromedriver/chrome/status.h" |
| 12 #include "chrome/test/chromedriver/chrome/stub_devtools_client.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 TEST(JavaScriptDialogManager, NoDialog) { | 15 TEST(JavaScriptDialogManager, NoDialog) { |
| 16 StubDevToolsClient client; | 16 StubDevToolsClient client; |
| 17 JavaScriptDialogManager manager(&client); | 17 JavaScriptDialogManager manager(&client); |
| 18 std::string message("HI"); | 18 std::string message("HI"); |
| 19 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); | 19 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); |
| 20 ASSERT_FALSE(manager.IsDialogOpen()); | 20 ASSERT_FALSE(manager.IsDialogOpen()); |
| 21 ASSERT_STREQ("HI", message.c_str()); | 21 ASSERT_STREQ("HI", message.c_str()); |
| 22 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); | 22 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); |
| 23 } | 23 } |
| 24 | 24 |
| 25 namespace { | |
| 26 | |
| 27 class RecorderDevToolsClient : public StubDevToolsClient { | |
| 28 public: | |
| 29 RecorderDevToolsClient() {} | |
| 30 ~RecorderDevToolsClient() override {} | |
| 31 | |
| 32 // Overridden from StubDevToolsClient: | |
| 33 Status SendCommandAndGetResult( | |
| 34 const std::string& method, | |
| 35 const base::DictionaryValue& params, | |
| 36 scoped_ptr<base::DictionaryValue>* result) override { | |
| 37 method_ = method; | |
| 38 params_.Clear(); | |
| 39 params_.MergeDictionary(¶ms); | |
| 40 return Status(kOk); | |
| 41 } | |
| 42 | |
| 43 std::string method_; | |
| 44 base::DictionaryValue params_; | |
| 45 }; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 TEST(JavaScriptDialogManager, HandleDialogPassesParams) { | 25 TEST(JavaScriptDialogManager, HandleDialogPassesParams) { |
| 50 RecorderDevToolsClient client; | 26 RecorderDevToolsClient client; |
| 51 JavaScriptDialogManager manager(&client); | 27 JavaScriptDialogManager manager(&client); |
| 52 base::DictionaryValue params; | 28 base::DictionaryValue params; |
| 53 params.SetString("message", "hi"); | 29 params.SetString("message", "hi"); |
| 54 ASSERT_EQ( | 30 ASSERT_EQ( |
| 55 kOk, | 31 kOk, |
| 56 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); | 32 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); |
| 57 std::string given_text("text"); | 33 std::string given_text("text"); |
| 58 ASSERT_EQ(kOk, manager.HandleDialog(false, &given_text).code()); | 34 ASSERT_EQ(kOk, manager.HandleDialog(false, &given_text).code()); |
| 59 std::string text; | 35 std::string text; |
| 60 client.params_.GetString("promptText", &text); | 36 ASSERT_TRUE(client.commands_[0].params.GetString("promptText", &text)); |
| 61 ASSERT_EQ(given_text, text); | 37 ASSERT_EQ(given_text, text); |
| 62 ASSERT_TRUE(client.params_.HasKey("accept")); | 38 ASSERT_TRUE(client.commands_[0].params.HasKey("accept")); |
| 63 } | 39 } |
| 64 | 40 |
| 65 TEST(JavaScriptDialogManager, HandleDialogNullPrompt) { | 41 TEST(JavaScriptDialogManager, HandleDialogNullPrompt) { |
| 66 RecorderDevToolsClient client; | 42 RecorderDevToolsClient client; |
| 67 JavaScriptDialogManager manager(&client); | 43 JavaScriptDialogManager manager(&client); |
| 68 base::DictionaryValue params; | 44 base::DictionaryValue params; |
| 69 params.SetString("message", "hi"); | 45 params.SetString("message", "hi"); |
| 70 ASSERT_EQ( | 46 ASSERT_EQ( |
| 71 kOk, | 47 kOk, |
| 72 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); | 48 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); |
| 73 ASSERT_EQ(kOk, manager.HandleDialog(false, NULL).code()); | 49 ASSERT_EQ(kOk, manager.HandleDialog(false, NULL).code()); |
| 74 ASSERT_FALSE(client.params_.HasKey("promptText")); | 50 ASSERT_FALSE(client.commands_[0].params.HasKey("promptText")); |
| 75 ASSERT_TRUE(client.params_.HasKey("accept")); | 51 ASSERT_TRUE(client.commands_[0].params.HasKey("accept")); |
| 76 } | 52 } |
| 77 | 53 |
| 78 TEST(JavaScriptDialogManager, ReconnectClearsStateAndSendsEnable) { | 54 TEST(JavaScriptDialogManager, ReconnectClearsStateAndSendsEnable) { |
| 79 RecorderDevToolsClient client; | 55 RecorderDevToolsClient client; |
| 80 JavaScriptDialogManager manager(&client); | 56 JavaScriptDialogManager manager(&client); |
| 81 base::DictionaryValue params; | 57 base::DictionaryValue params; |
| 82 params.SetString("message", "hi"); | 58 params.SetString("message", "hi"); |
| 83 ASSERT_EQ( | 59 ASSERT_EQ( |
| 84 kOk, | 60 kOk, |
| 85 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); | 61 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); |
| 86 ASSERT_TRUE(manager.IsDialogOpen()); | 62 ASSERT_TRUE(manager.IsDialogOpen()); |
| 87 std::string message; | 63 std::string message; |
| 88 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); | 64 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); |
| 89 | 65 |
| 90 ASSERT_TRUE(manager.OnConnected(&client).IsOk()); | 66 ASSERT_TRUE(manager.OnConnected(&client).IsOk()); |
| 91 ASSERT_EQ("Page.enable", client.method_); | 67 ASSERT_EQ("Page.enable", client.commands_[0].method); |
| 92 ASSERT_FALSE(manager.IsDialogOpen()); | 68 ASSERT_FALSE(manager.IsDialogOpen()); |
| 93 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); | 69 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); |
| 94 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); | 70 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); |
| 95 } | 71 } |
| 96 | 72 |
| 97 namespace { | 73 namespace { |
| 98 | 74 |
| 99 class FakeDevToolsClient : public StubDevToolsClient { | 75 class FakeDevToolsClient : public StubDevToolsClient { |
| 100 public: | 76 public: |
| 101 FakeDevToolsClient() : listener_(NULL), closing_count_(0) {} | 77 FakeDevToolsClient() : listener_(NULL), closing_count_(0) {} |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); | 176 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); |
| 201 ASSERT_EQ("hi", message); | 177 ASSERT_EQ("hi", message); |
| 202 | 178 |
| 203 ASSERT_EQ( | 179 ASSERT_EQ( |
| 204 kOk, | 180 kOk, |
| 205 manager.OnEvent(&client, "Page.javascriptDialogClosed", params).code()); | 181 manager.OnEvent(&client, "Page.javascriptDialogClosed", params).code()); |
| 206 ASSERT_FALSE(manager.IsDialogOpen()); | 182 ASSERT_FALSE(manager.IsDialogOpen()); |
| 207 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); | 183 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); |
| 208 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); | 184 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); |
| 209 } | 185 } |
| OLD | NEW |