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

Side by Side Diff: chrome/browser/instant/instant_browsertest.cc

Issue 6310003: Support specified JSON as argument to setSuggestions(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome
Patch Set: Address comments Created 9 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
« no previous file with comments | « no previous file | chrome/browser/instant/instant_loader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/command_line.h" 5 #include "base/command_line.h"
6 #include "base/stringprintf.h" 6 #include "base/stringprintf.h"
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" 8 #include "chrome/browser/autocomplete/autocomplete_edit_view.h"
9 #include "chrome/browser/browser_list.h" 9 #include "chrome/browser/browser_list.h"
10 #include "chrome/browser/browser_window.h" 10 #include "chrome/browser/browser_window.h"
11 #include "chrome/browser/instant/instant_controller.h" 11 #include "chrome/browser/instant/instant_controller.h"
12 #include "chrome/browser/instant/instant_loader.h"
13 #include "chrome/browser/instant/instant_loader_manager.h"
12 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/renderer_host/render_view_host.h" 15 #include "chrome/browser/renderer_host/render_view_host.h"
14 #include "chrome/browser/renderer_host/render_widget_host_view.h" 16 #include "chrome/browser/renderer_host/render_widget_host_view.h"
15 #include "chrome/browser/search_engines/template_url.h" 17 #include "chrome/browser/search_engines/template_url.h"
16 #include "chrome/browser/search_engines/template_url_model.h" 18 #include "chrome/browser/search_engines/template_url_model.h"
17 #include "chrome/browser/tab_contents/tab_contents.h" 19 #include "chrome/browser/tab_contents/tab_contents.h"
18 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/omnibox/location_bar.h" 21 #include "chrome/browser/ui/omnibox/location_bar.h"
20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 22 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
21 #include "chrome/common/chrome_switches.h" 23 #include "chrome/common/chrome_switches.h"
22 #include "chrome/test/in_process_browser_test.h" 24 #include "chrome/test/in_process_browser_test.h"
23 #include "chrome/test/ui_test_utils.h" 25 #include "chrome/test/ui_test_utils.h"
24 26
27 #define EXPECT_STR_EQ(ascii, utf16) \
28 EXPECT_EQ(ASCIIToWide(ascii), UTF16ToWide(utf16))
29
25 class InstantTest : public InProcessBrowserTest { 30 class InstantTest : public InProcessBrowserTest {
26 public: 31 public:
27 InstantTest() 32 InstantTest()
28 : location_bar_(NULL), 33 : location_bar_(NULL),
29 preview_(NULL) { 34 preview_(NULL) {
30 set_show_window(true); 35 set_show_window(true);
31 EnableDOMAutomation(); 36 EnableDOMAutomation();
32 } 37 }
33 38
34 void SetupInstantProvider(const std::string& page) { 39 void SetupInstantProvider(const std::string& page) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 GetSearchStateAsString(preview_)); 109 GetSearchStateAsString(preview_));
105 } 110 }
106 111
107 void SetLocationBarText(const std::wstring& text) { 112 void SetLocationBarText(const std::wstring& text) {
108 ASSERT_NO_FATAL_FAILURE(FindLocationBar()); 113 ASSERT_NO_FATAL_FAILURE(FindLocationBar());
109 location_bar_->location_entry()->SetUserText(text); 114 location_bar_->location_entry()->SetUserText(text);
110 ui_test_utils::WaitForNotification( 115 ui_test_utils::WaitForNotification(
111 NotificationType::INSTANT_CONTROLLER_SHOWN); 116 NotificationType::INSTANT_CONTROLLER_SHOWN);
112 } 117 }
113 118
119 const string16& GetSuggestion() const {
120 return browser()->instant()->loader_manager_->
121 current_loader()->complete_suggested_text_;
122 }
123
114 void SendKey(app::KeyboardCode key) { 124 void SendKey(app::KeyboardCode key) {
115 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 125 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
116 browser(), key, false, false, false, false)); 126 browser(), key, false, false, false, false));
117 } 127 }
118 128
129 void SetSuggestionsJavascriptArgument(TabContents* tab_contents,
130 const std::string& argument) {
131 std::string script = StringPrintf(
132 "window.setSuggestionsArgument = %s;", argument.c_str());
133 ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
134 tab_contents->render_view_host(),
135 std::wstring(),
136 UTF8ToWide(script)));
137 }
138
119 bool GetStringFromJavascript(TabContents* tab_contents, 139 bool GetStringFromJavascript(TabContents* tab_contents,
120 const std::string& function, 140 const std::string& function,
121 std::string* result) { 141 std::string* result) {
122 std::string script = StringPrintf( 142 std::string script = StringPrintf(
123 "window.domAutomationController.send(%s)", function.c_str()); 143 "window.domAutomationController.send(%s)", function.c_str());
124 return ui_test_utils::ExecuteJavaScriptAndExtractString( 144 return ui_test_utils::ExecuteJavaScriptAndExtractString(
125 tab_contents->render_view_host(), 145 tab_contents->render_view_host(),
126 std::wstring(), UTF8ToWide(script), result); 146 std::wstring(), UTF8ToWide(script), result);
127 } 147 }
128 148
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 protected: 260 protected:
241 virtual void SetUpCommandLine(CommandLine* command_line) { 261 virtual void SetUpCommandLine(CommandLine* command_line) {
242 command_line->AppendSwitch(switches::kEnablePredictiveInstant); 262 command_line->AppendSwitch(switches::kEnablePredictiveInstant);
243 } 263 }
244 264
245 LocationBar* location_bar_; 265 LocationBar* location_bar_;
246 TabContents* preview_; 266 TabContents* preview_;
247 }; 267 };
248 268
249 // TODO(tonyg): Add the following tests: 269 // TODO(tonyg): Add the following tests:
250 // 1. Test that setSuggestions() works. 270 // - Test that the search box API is not populated for pages other than the
251 // 2. Test that the search box API is not populated for pages other than the 271 // default search provider.
252 // default search provider. 272 // - Test resize events.
253 // 3. Test resize events.
254 273
255 // Verify that the onchange event is dispatched upon typing in the box. 274 // Verify that the onchange event is dispatched upon typing in the box.
256 IN_PROC_BROWSER_TEST_F(InstantTest, OnChangeEvent) { 275 IN_PROC_BROWSER_TEST_F(InstantTest, OnChangeEvent) {
257 ASSERT_TRUE(test_server()->Start()); 276 ASSERT_TRUE(test_server()->Start());
258 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html")); 277 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
259 ASSERT_NO_FATAL_FAILURE(SetupLocationBar()); 278 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
260 ASSERT_NO_FATAL_FAILURE(SetupPreview()); 279 ASSERT_NO_FATAL_FAILURE(SetupPreview());
261 280
262 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc")); 281 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
263 282
264 // Check that the value is reflected and onchange is called. 283 // Check that the value is reflected and onchange is called.
265 EXPECT_EQ("true 0 0 1 1 a false abc false", 284 EXPECT_EQ("true 0 0 1 1 a false abc false",
266 GetSearchStateAsString(preview_)); 285 GetSearchStateAsString(preview_));
267 } 286 }
268 287
288 IN_PROC_BROWSER_TEST_F(InstantTest, SetSuggestionsArrayOfStrings) {
289 ASSERT_TRUE(test_server()->Start());
290 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
291 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
292 ASSERT_NO_FATAL_FAILURE(SetupPreview());
293
294 SetSuggestionsJavascriptArgument(preview_, "['abcde', 'unused']");
295 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
296 EXPECT_STR_EQ("abcde", GetSuggestion());
297 }
298
299 IN_PROC_BROWSER_TEST_F(InstantTest, SetSuggestionsEmptyArray) {
300 ASSERT_TRUE(test_server()->Start());
301 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
302 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
303 ASSERT_NO_FATAL_FAILURE(SetupPreview());
304
305 SetSuggestionsJavascriptArgument(preview_, "[]");
306 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
307 EXPECT_STR_EQ("", GetSuggestion());
308 }
309
310 IN_PROC_BROWSER_TEST_F(InstantTest, SetSuggestionsValidJson) {
311 ASSERT_TRUE(test_server()->Start());
312 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
313 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
314 ASSERT_NO_FATAL_FAILURE(SetupPreview());
315
316 SetSuggestionsJavascriptArgument(
317 preview_,
318 "{suggestions:[{value:'abcdefg'},{value:'unused'}]}");
319 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
320 EXPECT_STR_EQ("abcdefg", GetSuggestion());
321 }
322
323 IN_PROC_BROWSER_TEST_F(InstantTest, SetSuggestionsInvalidSuggestions) {
324 ASSERT_TRUE(test_server()->Start());
325 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
326 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
327 ASSERT_NO_FATAL_FAILURE(SetupPreview());
328
329 SetSuggestionsJavascriptArgument(
330 preview_,
331 "{suggestions:{value:'abcdefg'}}");
332 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
333 EXPECT_STR_EQ("", GetSuggestion());
334 }
335
336 IN_PROC_BROWSER_TEST_F(InstantTest, SetSuggestionsEmptyJson) {
337 ASSERT_TRUE(test_server()->Start());
338 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
339 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
340 ASSERT_NO_FATAL_FAILURE(SetupPreview());
341
342 SetSuggestionsJavascriptArgument(preview_, "{}");
343 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
344 EXPECT_STR_EQ("", GetSuggestion());
345 }
346
347 IN_PROC_BROWSER_TEST_F(InstantTest, SetSuggestionsEmptySuggestions) {
348 ASSERT_TRUE(test_server()->Start());
349 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
350 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
351 ASSERT_NO_FATAL_FAILURE(SetupPreview());
352
353 SetSuggestionsJavascriptArgument(preview_, "{suggestions:[]}");
354 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
355 EXPECT_STR_EQ("", GetSuggestion());
356 }
357
358 IN_PROC_BROWSER_TEST_F(InstantTest, SetSuggestionsEmptySuggestion) {
359 ASSERT_TRUE(test_server()->Start());
360 ASSERT_NO_FATAL_FAILURE(SetupInstantProvider("search.html"));
361 ASSERT_NO_FATAL_FAILURE(SetupLocationBar());
362 ASSERT_NO_FATAL_FAILURE(SetupPreview());
363
364 SetSuggestionsJavascriptArgument(preview_, "{suggestions:[{}]}");
365 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(L"abc"));
366 EXPECT_STR_EQ("", GetSuggestion());
367 }
368
269 // Verify instant preview is shown correctly for a non-search query. 369 // Verify instant preview is shown correctly for a non-search query.
270 IN_PROC_BROWSER_TEST_F(InstantTest, ShowPreviewNonSearch) { 370 IN_PROC_BROWSER_TEST_F(InstantTest, ShowPreviewNonSearch) {
271 ASSERT_TRUE(test_server()->Start()); 371 ASSERT_TRUE(test_server()->Start());
272 GURL url(test_server()->GetURL("files/instant/empty.html")); 372 GURL url(test_server()->GetURL("files/instant/empty.html"));
273 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(UTF8ToWide(url.spec()))); 373 ASSERT_NO_FATAL_FAILURE(SetLocationBarText(UTF8ToWide(url.spec())));
274 // The preview should be active and showing. 374 // The preview should be active and showing.
275 ASSERT_TRUE(browser()->instant()->is_active()); 375 ASSERT_TRUE(browser()->instant()->is_active());
276 ASSERT_TRUE(browser()->instant()->is_displayable()); 376 ASSERT_TRUE(browser()->instant()->is_displayable());
277 ASSERT_TRUE(browser()->instant()->IsCurrent()); 377 ASSERT_TRUE(browser()->instant()->IsCurrent());
278 ASSERT_TRUE(browser()->instant()->GetPreviewContents()); 378 ASSERT_TRUE(browser()->instant()->GetPreviewContents());
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 // Check that the preview contents have been committed. 647 // Check that the preview contents have been committed.
548 ASSERT_FALSE(browser()->instant()->GetPreviewContents()); 648 ASSERT_FALSE(browser()->instant()->GetPreviewContents());
549 ASSERT_FALSE(browser()->instant()->is_active()); 649 ASSERT_FALSE(browser()->instant()->is_active());
550 TabContents* contents = browser()->GetSelectedTabContents(); 650 TabContents* contents = browser()->GetSelectedTabContents();
551 ASSERT_TRUE(contents); 651 ASSERT_TRUE(contents);
552 652
553 // Check that the value is reflected and onsubmit is called. 653 // Check that the value is reflected and onsubmit is called.
554 EXPECT_EQ("true 1 0 2 2 a false abcdef true", 654 EXPECT_EQ("true 1 0 2 2 a false abcdef true",
555 GetSearchStateAsString(preview_)); 655 GetSearchStateAsString(preview_));
556 } 656 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/instant/instant_loader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698