Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/browser_keyevents_browsertest.cc

Issue 11753009: Simplify ExecuteJavaScript* functions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update prerender_browsertest.cc. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 void ClickOnView(ViewID vid) { 139 void ClickOnView(ViewID vid) {
140 ui_test_utils::ClickOnView(browser(), vid); 140 ui_test_utils::ClickOnView(browser(), vid);
141 } 141 }
142 142
143 // Set the suppress flag of an event specified by |type|. If |suppress| is 143 // Set the suppress flag of an event specified by |type|. If |suppress| is
144 // true then the web page will suppress all events with |type|. Following 144 // true then the web page will suppress all events with |type|. Following
145 // event types are supported: keydown, keypress, keyup and textInput. 145 // event types are supported: keydown, keypress, keyup and textInput.
146 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { 146 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) {
147 ASSERT_LT(tab_index, browser()->tab_count()); 147 ASSERT_LT(tab_index, browser()->tab_count());
148 bool actual; 148 bool actual;
149 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( 149 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
150 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 150 chrome::GetWebContentsAt(browser(), tab_index),
151 "",
152 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), 151 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)),
153 &actual)); 152 &actual));
154 ASSERT_EQ(!suppress, actual); 153 ASSERT_EQ(!suppress, actual);
155 } 154 }
156 155
157 void SuppressEvents(int tab_index, bool keydown, bool keypress, 156 void SuppressEvents(int tab_index, bool keydown, bool keypress,
158 bool keyup, bool textinput) { 157 bool keyup, bool textinput) {
159 ASSERT_NO_FATAL_FAILURE( 158 ASSERT_NO_FATAL_FAILURE(
160 SuppressEventByType(tab_index, L"keydown", keydown)); 159 SuppressEventByType(tab_index, L"keydown", keydown));
161 ASSERT_NO_FATAL_FAILURE( 160 ASSERT_NO_FATAL_FAILURE(
162 SuppressEventByType(tab_index, L"keypress", keypress)); 161 SuppressEventByType(tab_index, L"keypress", keypress));
163 ASSERT_NO_FATAL_FAILURE( 162 ASSERT_NO_FATAL_FAILURE(
164 SuppressEventByType(tab_index, L"keyup", keyup)); 163 SuppressEventByType(tab_index, L"keyup", keyup));
165 ASSERT_NO_FATAL_FAILURE( 164 ASSERT_NO_FATAL_FAILURE(
166 SuppressEventByType(tab_index, L"textInput", textinput)); 165 SuppressEventByType(tab_index, L"textInput", textinput));
167 } 166 }
168 167
169 void SuppressAllEvents(int tab_index, bool suppress) { 168 void SuppressAllEvents(int tab_index, bool suppress) {
170 SuppressEvents(tab_index, suppress, suppress, suppress, suppress); 169 SuppressEvents(tab_index, suppress, suppress, suppress, suppress);
171 } 170 }
172 171
173 void GetResultLength(int tab_index, int* length) { 172 void GetResultLength(int tab_index, int* length) {
174 ASSERT_LT(tab_index, browser()->tab_count()); 173 ASSERT_LT(tab_index, browser()->tab_count());
175 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractInt( 174 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
176 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 175 chrome::GetWebContentsAt(browser(), tab_index),
177 "",
178 kGetResultLengthJS, 176 kGetResultLengthJS,
179 length)); 177 length));
180 } 178 }
181 179
182 void CheckResult(int tab_index, int length, const char* const result[]) { 180 void CheckResult(int tab_index, int length, const char* const result[]) {
183 ASSERT_LT(tab_index, browser()->tab_count()); 181 ASSERT_LT(tab_index, browser()->tab_count());
184 int actual_length; 182 int actual_length;
185 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); 183 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length));
186 ASSERT_GE(actual_length, length); 184 ASSERT_GE(actual_length, length);
187 for (int i = 0; i < actual_length; ++i) { 185 for (int i = 0; i < actual_length; ++i) {
188 std::string actual; 186 std::string actual;
189 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( 187 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
190 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 188 chrome::GetWebContentsAt(browser(), tab_index),
191 "",
192 base::StringPrintf(kGetResultJS, i), 189 base::StringPrintf(kGetResultJS, i),
193 &actual)); 190 &actual));
194 191
195 // If more events were received than expected, then the additional events 192 // If more events were received than expected, then the additional events
196 // must be keyup events. 193 // must be keyup events.
197 if (i < length) 194 if (i < length)
198 ASSERT_STREQ(result[i], actual.c_str()); 195 ASSERT_STREQ(result[i], actual.c_str());
199 else 196 else
200 ASSERT_EQ('U', actual[0]); 197 ASSERT_EQ('U', actual[0]);
201 } 198 }
202 } 199 }
203 200
204 void CheckFocusedElement(int tab_index, const wchar_t* focused) { 201 void CheckFocusedElement(int tab_index, const wchar_t* focused) {
205 ASSERT_LT(tab_index, browser()->tab_count()); 202 ASSERT_LT(tab_index, browser()->tab_count());
206 std::string actual; 203 std::string actual;
207 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( 204 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
208 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 205 chrome::GetWebContentsAt(browser(), tab_index),
209 "",
210 kGetFocusedElementJS, 206 kGetFocusedElementJS,
211 &actual)); 207 &actual));
212 ASSERT_EQ(WideToUTF8(focused), actual); 208 ASSERT_EQ(WideToUTF8(focused), actual);
213 } 209 }
214 210
215 void SetFocusedElement(int tab_index, const wchar_t* focused) { 211 void SetFocusedElement(int tab_index, const wchar_t* focused) {
216 ASSERT_LT(tab_index, browser()->tab_count()); 212 ASSERT_LT(tab_index, browser()->tab_count());
217 bool actual; 213 bool actual;
218 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( 214 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
219 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 215 chrome::GetWebContentsAt(browser(), tab_index),
220 "",
221 base::StringPrintf(kSetFocusedElementJS, focused), 216 base::StringPrintf(kSetFocusedElementJS, focused),
222 &actual)); 217 &actual));
223 ASSERT_TRUE(actual); 218 ASSERT_TRUE(actual);
224 } 219 }
225 220
226 void CheckTextBoxValue(int tab_index, const wchar_t* id, 221 void CheckTextBoxValue(int tab_index, const wchar_t* id,
227 const wchar_t* value) { 222 const wchar_t* value) {
228 ASSERT_LT(tab_index, browser()->tab_count()); 223 ASSERT_LT(tab_index, browser()->tab_count());
229 std::string actual; 224 std::string actual;
230 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( 225 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
231 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 226 chrome::GetWebContentsAt(browser(), tab_index),
232 "",
233 base::StringPrintf(kGetTextBoxValueJS, id), 227 base::StringPrintf(kGetTextBoxValueJS, id),
234 &actual)); 228 &actual));
235 ASSERT_EQ(WideToUTF8(value), actual); 229 ASSERT_EQ(WideToUTF8(value), actual);
236 } 230 }
237 231
238 void SetTextBoxValue(int tab_index, const wchar_t* id, 232 void SetTextBoxValue(int tab_index, const wchar_t* id,
239 const wchar_t* value) { 233 const wchar_t* value) {
240 ASSERT_LT(tab_index, browser()->tab_count()); 234 ASSERT_LT(tab_index, browser()->tab_count());
241 std::string actual; 235 std::string actual;
242 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( 236 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
243 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 237 chrome::GetWebContentsAt(browser(), tab_index),
244 "",
245 base::StringPrintf(kSetTextBoxValueJS, id, value), 238 base::StringPrintf(kSetTextBoxValueJS, id, value),
246 &actual)); 239 &actual));
247 ASSERT_EQ(WideToUTF8(value), actual); 240 ASSERT_EQ(WideToUTF8(value), actual);
248 } 241 }
249 242
250 void StartTest(int tab_index, int result_length) { 243 void StartTest(int tab_index, int result_length) {
251 ASSERT_LT(tab_index, browser()->tab_count()); 244 ASSERT_LT(tab_index, browser()->tab_count());
252 bool actual; 245 bool actual;
253 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( 246 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
254 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), 247 chrome::GetWebContentsAt(browser(), tab_index),
255 "",
256 base::StringPrintf(kStartTestJS, result_length), 248 base::StringPrintf(kStartTestJS, result_length),
257 &actual)); 249 &actual));
258 ASSERT_TRUE(actual); 250 ASSERT_TRUE(actual);
259 } 251 }
260 252
261 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { 253 void TestKeyEvent(int tab_index, const KeyEventTestData& test) {
262 ASSERT_LT(tab_index, browser()->tab_count()); 254 ASSERT_LT(tab_index, browser()->tab_count());
263 ASSERT_EQ(tab_index, browser()->active_index()); 255 ASSERT_EQ(tab_index, browser()->active_index());
264 256
265 // Inform our testing web page that we are about to start testing a key 257 // Inform our testing web page that we are about to start testing a key
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); 884 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress));
893 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); 885 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
894 886
895 // Ctrl+Alt should have no effect. 887 // Ctrl+Alt should have no effect.
896 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); 888 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey));
897 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); 889 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
898 } 890 }
899 #endif 891 #endif
900 892
901 } // namespace 893 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698