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 3 // found in the LICENSE file. 4 5 #include <string> 6 7 #include "base/callback.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "base/values.h" 10 #include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h" 11 #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" 14 15 TEST(JavaScriptDialogManager, NoDialog) { 16 StubDevToolsClient client; 17 JavaScriptDialogManager manager(&client); 18 std::string message("HI"); 19 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); 20 ASSERT_FALSE(manager.IsDialogOpen()); 21 ASSERT_STREQ("HI", message.c_str()); 22 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); 23 } 24 25 namespace { 26 27 class RecorderDevToolsClient : public StubDevToolsClient { 28 public: 29 RecorderDevToolsClient() {} 30 virtual ~RecorderDevToolsClient() {} 31 32 // Overridden from StubDevToolsClient: 33 virtual 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) { 50 RecorderDevToolsClient client; 51 JavaScriptDialogManager manager(&client); 52 base::DictionaryValue params; 53 params.SetString("message", "hi"); 54 ASSERT_EQ( 55 kOk, 56 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); 57 std::string given_text("text"); 58 ASSERT_EQ(kOk, manager.HandleDialog(false, &given_text).code()); 59 std::string text; 60 client.params_.GetString("promptText", &text); 61 ASSERT_EQ(given_text, text); 62 ASSERT_TRUE(client.params_.HasKey("accept")); 63 } 64 65 TEST(JavaScriptDialogManager, HandleDialogNullPrompt) { 66 RecorderDevToolsClient client; 67 JavaScriptDialogManager manager(&client); 68 base::DictionaryValue params; 69 params.SetString("message", "hi"); 70 ASSERT_EQ( 71 kOk, 72 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); 73 ASSERT_EQ(kOk, manager.HandleDialog(false, NULL).code()); 74 ASSERT_FALSE(client.params_.HasKey("promptText")); 75 ASSERT_TRUE(client.params_.HasKey("accept")); 76 } 77 78 TEST(JavaScriptDialogManager, ReconnectClearsStateAndSendsEnable) { 79 RecorderDevToolsClient client; 80 JavaScriptDialogManager manager(&client); 81 base::DictionaryValue params; 82 params.SetString("message", "hi"); 83 ASSERT_EQ( 84 kOk, 85 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); 86 ASSERT_TRUE(manager.IsDialogOpen()); 87 std::string message; 88 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); 89 90 ASSERT_TRUE(manager.OnConnected(&client).IsOk()); 91 ASSERT_EQ("Page.enable", client.method_); 92 ASSERT_FALSE(manager.IsDialogOpen()); 93 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); 94 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); 95 } 96 97 namespace { 98 99 class FakeDevToolsClient : public StubDevToolsClient { 100 public: 101 FakeDevToolsClient() : listener_(NULL), closing_count_(0) {} 102 virtual ~FakeDevToolsClient() {} 103 104 void set_closing_count(int closing_count) { 105 closing_count_ = closing_count; 106 } 107 108 // Overridden from StubDevToolsClient: 109 virtual Status SendCommandAndGetResult( 110 const std::string& method, 111 const base::DictionaryValue& params, 112 scoped_ptr<base::DictionaryValue>* result) OVERRIDE { 113 while (closing_count_ > 0) { 114 base::DictionaryValue empty; 115 Status status = 116 listener_->OnEvent(this, "Page.javascriptDialogClosing", empty); 117 if (status.IsError()) 118 return status; 119 closing_count_--; 120 } 121 return Status(kOk); 122 } 123 virtual void AddListener(DevToolsEventListener* listener) OVERRIDE { 124 listener_ = listener; 125 } 126 127 private: 128 DevToolsEventListener* listener_; 129 int closing_count_; 130 }; 131 132 } // namespace 133 134 TEST(JavaScriptDialogManager, OneDialog) { 135 FakeDevToolsClient client; 136 JavaScriptDialogManager manager(&client); 137 base::DictionaryValue params; 138 params.SetString("message", "hi"); 139 ASSERT_FALSE(manager.IsDialogOpen()); 140 std::string message; 141 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); 142 143 ASSERT_EQ( 144 kOk, 145 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); 146 ASSERT_TRUE(manager.IsDialogOpen()); 147 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); 148 ASSERT_EQ("hi", message); 149 150 client.set_closing_count(1); 151 ASSERT_EQ(kOk, manager.HandleDialog(false, NULL).code()); 152 ASSERT_FALSE(manager.IsDialogOpen()); 153 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); 154 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); 155 } 156 157 TEST(JavaScriptDialogManager, TwoDialogs) { 158 FakeDevToolsClient client; 159 JavaScriptDialogManager manager(&client); 160 base::DictionaryValue params; 161 params.SetString("message", "1"); 162 ASSERT_EQ( 163 kOk, 164 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); 165 params.SetString("message", "2"); 166 ASSERT_EQ( 167 kOk, 168 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); 169 170 std::string message; 171 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); 172 ASSERT_TRUE(manager.IsDialogOpen()); 173 ASSERT_EQ("1", message); 174 175 ASSERT_EQ(kOk, manager.HandleDialog(false, NULL).code()); 176 ASSERT_TRUE(manager.IsDialogOpen()); 177 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); 178 ASSERT_EQ("2", message); 179 180 client.set_closing_count(2); 181 ASSERT_EQ(kOk, manager.HandleDialog(false, NULL).code()); 182 ASSERT_FALSE(manager.IsDialogOpen()); 183 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); 184 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); 185 } 186 187 TEST(JavaScriptDialogManager, OneDialogManualClose) { 188 StubDevToolsClient client; 189 JavaScriptDialogManager manager(&client); 190 base::DictionaryValue params; 191 params.SetString("message", "hi"); 192 ASSERT_FALSE(manager.IsDialogOpen()); 193 std::string message; 194 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); 195 196 ASSERT_EQ( 197 kOk, 198 manager.OnEvent(&client, "Page.javascriptDialogOpening", params).code()); 199 ASSERT_TRUE(manager.IsDialogOpen()); 200 ASSERT_EQ(kOk, manager.GetDialogMessage(&message).code()); 201 ASSERT_EQ("hi", message); 202 203 ASSERT_EQ( 204 kOk, 205 manager.OnEvent(&client, "Page.javascriptDialogClosing", params).code()); 206 ASSERT_FALSE(manager.IsDialogOpen()); 207 ASSERT_EQ(kNoAlertOpen, manager.GetDialogMessage(&message).code()); 208 ASSERT_EQ(kNoAlertOpen, manager.HandleDialog(false, NULL).code()); 209 } 210