OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "base/test/scoped_command_line.h" | 7 #include "base/test/scoped_command_line.h" |
8 #include "base/test/scoped_feature_list.h" | 8 #include "base/test/scoped_feature_list.h" |
9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" | 10 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16(script)); | 104 tab->GetMainFrame()->ExecuteJavaScriptForTests(base::UTF8ToUTF16(script)); |
105 runner->Run(); | 105 runner->Run(); |
106 | 106 |
107 // The tab is closed while the dialog is up. | 107 // The tab is closed while the dialog is up. |
108 int tab_index = browser()->tab_strip_model()->GetIndexOfWebContents(tab); | 108 int tab_index = browser()->tab_strip_model()->GetIndexOfWebContents(tab); |
109 browser()->tab_strip_model()->CloseWebContentsAt(tab_index, | 109 browser()->tab_strip_model()->CloseWebContentsAt(tab_index, |
110 TabStripModel::CLOSE_NONE); | 110 TabStripModel::CLOSE_NONE); |
111 | 111 |
112 // No crash is good news. | 112 // No crash is good news. |
113 } | 113 } |
| 114 |
| 115 class JavaScriptCallbackHelper { |
| 116 public: |
| 117 JavaScriptDialogTabHelper::DialogClosedCallback GetCallback() { |
| 118 return base::Bind(&JavaScriptCallbackHelper::DialogClosed, |
| 119 base::Unretained(this)); |
| 120 } |
| 121 |
| 122 bool last_success() { return last_success_; } |
| 123 base::string16 last_input() { return last_input_; } |
| 124 |
| 125 private: |
| 126 void DialogClosed(bool success, const base::string16& user_input) { |
| 127 last_success_ = success; |
| 128 last_input_ = user_input; |
| 129 } |
| 130 |
| 131 bool last_success_; |
| 132 base::string16 last_input_; |
| 133 }; |
| 134 |
| 135 // Tests to make sure HandleJavaScriptDialog works correctly. |
| 136 IN_PROC_BROWSER_TEST_F(JavaScriptDialogTest, HandleJavaScriptDialog) { |
| 137 content::WebContents* tab = |
| 138 browser()->tab_strip_model()->GetActiveWebContents(); |
| 139 JavaScriptDialogTabHelper* js_helper = |
| 140 JavaScriptDialogTabHelper::FromWebContents(tab); |
| 141 |
| 142 JavaScriptCallbackHelper callback_helper; |
| 143 JavaScriptDialogTabHelper::DialogClosedCallback callback = |
| 144 callback_helper.GetCallback(); |
| 145 |
| 146 // alert |
| 147 bool did_suppress = false; |
| 148 js_helper->RunJavaScriptDialog( |
| 149 tab, GURL(), content::JAVASCRIPT_DIALOG_TYPE_ALERT, base::string16(), |
| 150 base::string16(), callback, &did_suppress); |
| 151 ASSERT_TRUE(js_helper->IsShowingDialogForTesting()); |
| 152 js_helper->HandleJavaScriptDialog(tab, true, nullptr); |
| 153 ASSERT_FALSE(js_helper->IsShowingDialogForTesting()); |
| 154 ASSERT_TRUE(callback_helper.last_success()); |
| 155 ASSERT_EQ(base::string16(), callback_helper.last_input()); |
| 156 |
| 157 // confirm |
| 158 for (auto response : {true, false}) { |
| 159 js_helper->RunJavaScriptDialog( |
| 160 tab, GURL(), content::JAVASCRIPT_DIALOG_TYPE_CONFIRM, base::string16(), |
| 161 base::string16(), callback, &did_suppress); |
| 162 ASSERT_TRUE(js_helper->IsShowingDialogForTesting()); |
| 163 js_helper->HandleJavaScriptDialog(tab, response, nullptr); |
| 164 ASSERT_FALSE(js_helper->IsShowingDialogForTesting()); |
| 165 ASSERT_EQ(response, callback_helper.last_success()); |
| 166 ASSERT_EQ(base::string16(), callback_helper.last_input()); |
| 167 } |
| 168 |
| 169 // prompt, cancel |
| 170 js_helper->RunJavaScriptDialog( |
| 171 tab, GURL(), content::JAVASCRIPT_DIALOG_TYPE_PROMPT, base::string16(), |
| 172 base::string16(), callback, &did_suppress); |
| 173 ASSERT_TRUE(js_helper->IsShowingDialogForTesting()); |
| 174 js_helper->HandleJavaScriptDialog(tab, false, nullptr); |
| 175 ASSERT_FALSE(js_helper->IsShowingDialogForTesting()); |
| 176 ASSERT_FALSE(callback_helper.last_success()); |
| 177 ASSERT_EQ(base::string16(), callback_helper.last_input()); |
| 178 |
| 179 base::string16 value1 = base::ASCIIToUTF16("abc"); |
| 180 base::string16 value2 = base::ASCIIToUTF16("123"); |
| 181 |
| 182 // prompt, ok + override |
| 183 js_helper->RunJavaScriptDialog( |
| 184 tab, GURL(), content::JAVASCRIPT_DIALOG_TYPE_PROMPT, base::string16(), |
| 185 value1, callback, &did_suppress); |
| 186 ASSERT_TRUE(js_helper->IsShowingDialogForTesting()); |
| 187 js_helper->HandleJavaScriptDialog(tab, true, &value2); |
| 188 ASSERT_FALSE(js_helper->IsShowingDialogForTesting()); |
| 189 ASSERT_TRUE(callback_helper.last_success()); |
| 190 ASSERT_EQ(value2, callback_helper.last_input()); |
| 191 |
| 192 // prompt, ok + no override |
| 193 js_helper->RunJavaScriptDialog( |
| 194 tab, GURL(), content::JAVASCRIPT_DIALOG_TYPE_PROMPT, base::string16(), |
| 195 value1, callback, &did_suppress); |
| 196 ASSERT_TRUE(js_helper->IsShowingDialogForTesting()); |
| 197 js_helper->HandleJavaScriptDialog(tab, true, nullptr); |
| 198 ASSERT_FALSE(js_helper->IsShowingDialogForTesting()); |
| 199 ASSERT_TRUE(callback_helper.last_success()); |
| 200 ASSERT_EQ(value1, callback_helper.last_input()); |
| 201 } |
OLD | NEW |