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

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

Issue 11233076: Fix spurious visibility events when committing Instant. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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 "base/stringprintf.h" 5 #include "base/stringprintf.h"
6 #include "chrome/browser/content_settings/host_content_settings_map.h" 6 #include "chrome/browser/content_settings/host_content_settings_map.h"
7 #include "chrome/browser/history/history_service_factory.h" 7 #include "chrome/browser/history/history_service_factory.h"
8 #include "chrome/browser/instant/instant_controller.h" 8 #include "chrome/browser/instant/instant_controller.h"
9 #include "chrome/browser/instant/instant_loader.h" 9 #include "chrome/browser/instant/instant_loader.h"
10 #include "chrome/browser/instant/instant_model.h" 10 #include "chrome/browser/instant/instant_model.h"
11 #include "chrome/browser/instant/instant_model_observer.h" 11 #include "chrome/browser/instant/instant_model_observer.h"
12 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/search_engines/template_url_service.h" 13 #include "chrome/browser/search_engines/template_url_service.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h" 14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/task_manager/task_manager.h" 15 #include "chrome/browser/task_manager/task_manager.h"
16 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" 16 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
17 #include "chrome/browser/ui/browser_commands.h" 17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_instant_controller.h" 18 #include "chrome/browser/ui/browser_instant_controller.h"
19 #include "chrome/browser/ui/browser_tabstrip.h" 19 #include "chrome/browser/ui/browser_tabstrip.h"
20 #include "chrome/browser/ui/browser_window.h" 20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/omnibox/location_bar.h" 21 #include "chrome/browser/ui/omnibox/location_bar.h"
22 #include "chrome/browser/ui/omnibox/omnibox_view.h" 22 #include "chrome/browser/ui/omnibox/omnibox_view.h"
23 #include "chrome/browser/ui/tab_contents/tab_contents.h" 23 #include "chrome/browser/ui/tab_contents/tab_contents.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/chrome_notification_types.h" 25 #include "chrome/common/chrome_notification_types.h"
25 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
26 #include "chrome/test/base/in_process_browser_test.h" 27 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chrome/test/base/ui_test_utils.h" 28 #include "chrome/test/base/ui_test_utils.h"
28 #include "content/public/browser/notification_service.h" 29 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
30 #include "content/public/test/browser_test_utils.h" 31 #include "content/public/test/browser_test_utils.h"
31 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
32 #include "ui/base/l10n/l10n_util.h" 33 #include "ui/base/l10n/l10n_util.h"
33 34
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 137
137 bool GetStringFromJS(content::RenderViewHost* rvh, 138 bool GetStringFromJS(content::RenderViewHost* rvh,
138 const std::string& script, 139 const std::string& script,
139 std::string* result) WARN_UNUSED_RESULT { 140 std::string* result) WARN_UNUSED_RESULT {
140 return content::ExecuteJavaScriptAndExtractString( 141 return content::ExecuteJavaScriptAndExtractString(
141 rvh, std::wstring(), WrapScript(script), result); 142 rvh, std::wstring(), WrapScript(script), result);
142 } 143 }
143 144
144 bool UpdateSearchState(TabContents* tab) WARN_UNUSED_RESULT { 145 bool UpdateSearchState(TabContents* tab) WARN_UNUSED_RESULT {
145 content::RenderViewHost* rvh = tab->web_contents()->GetRenderViewHost(); 146 content::RenderViewHost* rvh = tab->web_contents()->GetRenderViewHost();
146 return GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) && 147 return GetIntFromJS(rvh, "onvisibilitycalls", &onvisibilitycalls_) &&
148 GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) &&
147 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) && 149 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) &&
148 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) && 150 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) &&
149 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) && 151 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) &&
150 GetIntFromJS(rvh, "onfocuscalls", &onfocuscalls_) && 152 GetIntFromJS(rvh, "onfocuscalls", &onfocuscalls_) &&
151 GetIntFromJS(rvh, "onblurcalls", &onblurcalls_) && 153 GetIntFromJS(rvh, "onblurcalls", &onblurcalls_) &&
152 GetStringFromJS(rvh, "value", &value_) && 154 GetStringFromJS(rvh, "value", &value_) &&
153 GetBoolFromJS(rvh, "verbatim", &verbatim_) && 155 GetBoolFromJS(rvh, "verbatim", &verbatim_) &&
154 GetIntFromJS(rvh, "height", &height_); 156 GetIntFromJS(rvh, "height", &height_);
155 } 157 }
156 158
157 bool ExecuteScript(const std::string& script) WARN_UNUSED_RESULT { 159 bool ExecuteScript(const std::string& script) WARN_UNUSED_RESULT {
158 return content::ExecuteJavaScript( 160 return content::ExecuteJavaScript(
159 instant()->GetPreviewContents()->web_contents()->GetRenderViewHost(), 161 instant()->GetPreviewContents()->web_contents()->GetRenderViewHost(),
160 std::wstring(), UTF8ToWide(script)); 162 std::wstring(), UTF8ToWide(script));
161 } 163 }
162 164
163 bool CheckVisibilityIs(TabContents* tab, bool expected) WARN_UNUSED_RESULT { 165 bool CheckVisibilityIs(TabContents* tab, bool expected) WARN_UNUSED_RESULT {
164 bool actual = !expected; // Purposely start with a mis-match. 166 bool actual = !expected; // Purposely start with a mis-match.
165 // We can only use ASSERT_*() in a method that returns void, hence this 167 // We can only use ASSERT_*() in a method that returns void, hence this
166 // convoluted check. 168 // convoluted check.
167 return GetBoolFromJS(tab->web_contents()->GetRenderViewHost(), 169 return GetBoolFromJS(tab->web_contents()->GetRenderViewHost(),
168 "!document.webkitHidden", &actual) && 170 "!document.webkitHidden", &actual) &&
169 actual == expected; 171 actual == expected;
170 } 172 }
171 173
174 int onvisibilitycalls_;
172 int onchangecalls_; 175 int onchangecalls_;
173 int onsubmitcalls_; 176 int onsubmitcalls_;
174 int oncancelcalls_; 177 int oncancelcalls_;
175 int onresizecalls_; 178 int onresizecalls_;
176 int onfocuscalls_; 179 int onfocuscalls_;
177 int onblurcalls_; 180 int onblurcalls_;
178 181
179 std::string value_; 182 std::string value_;
180 bool verbatim_; 183 bool verbatim_;
181 int height_; 184 int height_;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 263
261 // Stash a reference to the preview, so we can refer to it after commit. 264 // Stash a reference to the preview, so we can refer to it after commit.
262 TabContents* preview_tab = instant()->GetPreviewContents(); 265 TabContents* preview_tab = instant()->GetPreviewContents();
263 EXPECT_TRUE(preview_tab); 266 EXPECT_TRUE(preview_tab);
264 267
265 // The state of the searchbox before the commit. 268 // The state of the searchbox before the commit.
266 EXPECT_TRUE(UpdateSearchState(preview_tab)); 269 EXPECT_TRUE(UpdateSearchState(preview_tab));
267 EXPECT_EQ("search", value_); 270 EXPECT_EQ("search", value_);
268 EXPECT_FALSE(verbatim_); 271 EXPECT_FALSE(verbatim_);
269 EXPECT_EQ(0, onsubmitcalls_); 272 EXPECT_EQ(0, onsubmitcalls_);
273 EXPECT_EQ(1, onvisibilitycalls_);
270 274
271 // Before the commit, the active tab is the NTP (i.e., not Instant). 275 // Before the commit, the active tab is the NTP (i.e., not Instant).
272 TabContents* active_tab = chrome::GetActiveTabContents(browser()); 276 TabContents* active_tab = chrome::GetActiveTabContents(browser());
273 EXPECT_NE(preview_tab, active_tab); 277 EXPECT_NE(preview_tab, active_tab);
274 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount()); 278 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount());
275 EXPECT_EQ(std::string(chrome::kAboutBlankURL), 279 EXPECT_EQ(std::string(chrome::kAboutBlankURL),
276 omnibox()->model()->PermanentURL().spec()); 280 omnibox()->model()->PermanentURL().spec());
277 281
278 // Commit the search by pressing Enter. 282 // Commit the search by pressing Enter.
279 browser()->window()->GetLocationBar()->AcceptInput(); 283 browser()->window()->GetLocationBar()->AcceptInput();
(...skipping 23 matching lines...) Expand all
303 std::string value; 307 std::string value;
304 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(), 308 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(),
305 "chrome.searchBox.value", &value)); 309 "chrome.searchBox.value", &value));
306 EXPECT_EQ("", value); 310 EXPECT_EQ("", value);
307 311
308 // However, the page should've correctly received the committed query. 312 // However, the page should've correctly received the committed query.
309 EXPECT_TRUE(UpdateSearchState(preview_tab)); 313 EXPECT_TRUE(UpdateSearchState(preview_tab));
310 EXPECT_EQ("search", value_); 314 EXPECT_EQ("search", value_);
311 EXPECT_TRUE(verbatim_); 315 EXPECT_TRUE(verbatim_);
312 EXPECT_EQ(1, onsubmitcalls_); 316 EXPECT_EQ(1, onsubmitcalls_);
317 EXPECT_EQ(1, onvisibilitycalls_);
313 } 318 }
314 319
315 // Test that the oncancel event is dispatched upon clicking on the preview. 320 // Test that the oncancel event is dispatched upon clicking on the preview.
316 IN_PROC_BROWSER_TEST_F(InstantTest, OnCancelEvent) { 321 IN_PROC_BROWSER_TEST_F(InstantTest, OnCancelEvent) {
317 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html")); 322 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
318 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 323 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
324 FocusOmniboxAndWaitForInstantSupport();
319 SetOmniboxTextAndWaitForInstantToShow("search"); 325 SetOmniboxTextAndWaitForInstantToShow("search");
320 326
321 // Stash a reference to the preview, so we can refer to it after commit. 327 // Stash a reference to the preview, so we can refer to it after commit.
322 TabContents* preview_tab = instant()->GetPreviewContents(); 328 TabContents* preview_tab = instant()->GetPreviewContents();
323 EXPECT_TRUE(preview_tab); 329 EXPECT_TRUE(preview_tab);
324 330
325 // The state of the searchbox before the commit. 331 // The state of the searchbox before the commit.
326 EXPECT_TRUE(UpdateSearchState(preview_tab)); 332 EXPECT_TRUE(UpdateSearchState(preview_tab));
327 EXPECT_EQ("search", value_); 333 EXPECT_EQ("search", value_);
328 EXPECT_FALSE(verbatim_); 334 EXPECT_FALSE(verbatim_);
329 EXPECT_EQ(0, oncancelcalls_); 335 EXPECT_EQ(0, oncancelcalls_);
336 EXPECT_EQ(1, onvisibilitycalls_);
330 337
331 // Before the commit, the active tab is the NTP (i.e., not Instant). 338 // Before the commit, the active tab is the NTP (i.e., not Instant).
332 TabContents* active_tab = chrome::GetActiveTabContents(browser()); 339 TabContents* active_tab = chrome::GetActiveTabContents(browser());
333 EXPECT_NE(preview_tab, active_tab); 340 EXPECT_NE(preview_tab, active_tab);
334 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount()); 341 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount());
335 EXPECT_EQ(std::string(chrome::kAboutBlankURL), 342 EXPECT_EQ(std::string(chrome::kAboutBlankURL),
336 omnibox()->model()->PermanentURL().spec()); 343 omnibox()->model()->PermanentURL().spec());
337 344
338 // Commit the search by clicking on the preview. 345 // Commit the search by clicking on the preview.
339 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER); 346 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
(...skipping 23 matching lines...) Expand all
363 std::string value; 370 std::string value;
364 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(), 371 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(),
365 "chrome.searchBox.value", &value)); 372 "chrome.searchBox.value", &value));
366 EXPECT_EQ("", value); 373 EXPECT_EQ("", value);
367 374
368 // However, the page should've correctly received the committed query. 375 // However, the page should've correctly received the committed query.
369 EXPECT_TRUE(UpdateSearchState(preview_tab)); 376 EXPECT_TRUE(UpdateSearchState(preview_tab));
370 EXPECT_EQ("search", value_); 377 EXPECT_EQ("search", value_);
371 EXPECT_TRUE(verbatim_); 378 EXPECT_TRUE(verbatim_);
372 EXPECT_EQ(1, oncancelcalls_); 379 EXPECT_EQ(1, oncancelcalls_);
380 EXPECT_EQ(1, onvisibilitycalls_);
373 } 381 }
374 382
375 // Test that the onreisze event is dispatched upon typing in the omnibox. 383 // Test that the onreisze event is dispatched upon typing in the omnibox.
376 IN_PROC_BROWSER_TEST_F(InstantTest, OnResizeEvent) { 384 IN_PROC_BROWSER_TEST_F(InstantTest, OnResizeEvent) {
377 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html")); 385 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
378 386
379 // This makes Instant load the preview, along with an initial onresize() (see 387 // This makes Instant load the preview, along with an initial onresize() (see
380 // SearchBoxExtension::PageSupportsInstant() for why). 388 // SearchBoxExtension::PageSupportsInstant() for why).
381 FocusOmniboxAndWaitForInstantSupport(); 389 FocusOmniboxAndWaitForInstantSupport();
382 390
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
866 base::Bind(&KeywordQueryDone, &run_loop3, &queries)); 874 base::Bind(&KeywordQueryDone, &run_loop3, &queries));
867 run_loop3.Run(); 875 run_loop3.Run();
868 ASSERT_TRUE(queries.size()); 876 ASSERT_TRUE(queries.size());
869 EXPECT_EQ(ASCIIToUTF16("search"), queries[0]); 877 EXPECT_EQ(ASCIIToUTF16("search"), queries[0]);
870 } 878 }
871 879
872 // Test that creating a new window hides any currently showing Instant preview. 880 // Test that creating a new window hides any currently showing Instant preview.
873 IN_PROC_BROWSER_TEST_F(InstantTest, NewWindowDismissesInstant) { 881 IN_PROC_BROWSER_TEST_F(InstantTest, NewWindowDismissesInstant) {
874 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html")); 882 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
875 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 883 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
884 FocusOmniboxAndWaitForInstantSupport();
876 SetOmniboxTextAndWaitForInstantToShow("search"); 885 SetOmniboxTextAndWaitForInstantToShow("search");
877 886
878 Browser* previous_window = browser(); 887 Browser* previous_window = browser();
879 EXPECT_TRUE(instant()->IsCurrent()); 888 EXPECT_TRUE(instant()->IsCurrent());
880 EXPECT_TRUE(instant()->model()->is_ready()); 889 EXPECT_TRUE(instant()->model()->is_ready());
881 890
882 InstantTestModelObserver observer(instant()->model()); 891 InstantTestModelObserver observer(instant()->model());
883 chrome::NewEmptyWindow(browser()->profile()); 892 chrome::NewEmptyWindow(browser()->profile());
884 observer.WaitUntilDisplayStateChanged(); 893 observer.WaitUntilDisplayStateChanged();
885 894
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 // Check that a d with a dot above and below it is completed regardless of 974 // Check that a d with a dot above and below it is completed regardless of
966 // how that is encoded. 975 // how that is encoded.
967 // U+1E0D = LATIN SMALL LETTER D WITH DOT BELOW 976 // U+1E0D = LATIN SMALL LETTER D WITH DOT BELOW
968 // U+1E0B = LATIN SMALL LETTER D WITH DOT ABOVE 977 // U+1E0B = LATIN SMALL LETTER D WITH DOT ABOVE
969 EXPECT_TRUE(ExecuteScript("suggestion = [ { value: '\\u1e0d\\u0307oh' } ]")); 978 EXPECT_TRUE(ExecuteScript("suggestion = [ { value: '\\u1e0d\\u0307oh' } ]"));
970 979
971 instant()->Hide(); 980 instant()->Hide();
972 SetOmniboxTextAndWaitForInstantToShow(WideToUTF8(L"\u1e0b\u0323")); 981 SetOmniboxTextAndWaitForInstantToShow(WideToUTF8(L"\u1e0b\u0323"));
973 EXPECT_EQ(WideToUTF16(L"\u1e0b\u0323oh"), omnibox()->GetText()); 982 EXPECT_EQ(WideToUTF16(L"\u1e0b\u0323oh"), omnibox()->GetText());
974 } 983 }
984
985 // Test that the preview can be committed onto a new tab.
986 IN_PROC_BROWSER_TEST_F(InstantTest, CommitInNewTab) {
987 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
988 FocusOmniboxAndWaitForInstantSupport();
989 SetOmniboxTextAndWaitForInstantToShow("search");
990
991 // Stash a reference to the preview, so we can refer to it after commit.
992 TabContents* preview_tab = instant()->GetPreviewContents();
993 EXPECT_TRUE(preview_tab);
994
995 // The state of the searchbox before the commit.
996 EXPECT_TRUE(UpdateSearchState(preview_tab));
997 EXPECT_EQ("search", value_);
998 EXPECT_FALSE(verbatim_);
999 EXPECT_EQ(0, onsubmitcalls_);
1000 EXPECT_EQ(1, onvisibilitycalls_);
1001
1002 // Before the commit, the active tab is the NTP (i.e., not Instant).
1003 TabContents* active_tab = chrome::GetActiveTabContents(browser());
1004 EXPECT_NE(preview_tab, active_tab);
1005 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount());
1006 EXPECT_EQ(std::string(chrome::kAboutBlankURL),
1007 omnibox()->model()->PermanentURL().spec());
1008
1009 // Commit the search by pressing Alt-Enter.
1010 omnibox()->model()->AcceptInput(NEW_FOREGROUND_TAB, false);
1011
1012 // After the commit, Instant should not be showing.
1013 EXPECT_FALSE(instant()->IsCurrent());
1014 EXPECT_FALSE(instant()->model()->is_ready());
1015
1016 // The old loader is deleted and a new one is created.
1017 EXPECT_TRUE(instant()->GetPreviewContents());
1018 EXPECT_NE(instant()->GetPreviewContents(), preview_tab);
1019
1020 // Check that we have two tabs and that the active tab is indeed what was once
1021 // the preview. The preview tab should have just one navigation entry, for the
1022 // Instant search that was committed.
1023 EXPECT_EQ(2, browser()->tab_strip_model()->count());
1024 EXPECT_EQ(preview_tab, chrome::GetActiveTabContents(browser()));
1025 EXPECT_EQ(1, preview_tab->web_contents()->GetController().GetEntryCount());
1026
1027 // Check that the omnibox contains the Instant URL we loaded.
1028 std::string instant_url = TemplateURLServiceFactory::GetForProfile(
1029 browser()->profile())->GetDefaultSearchProvider()->instant_url_ref().
1030 ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs(string16()));
1031 EXPECT_EQ(instant_url, omnibox()->model()->PermanentURL().spec());
1032
1033 // Check that the searchbox API values have been reset.
1034 std::string value;
1035 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(),
1036 "chrome.searchBox.value", &value));
1037 EXPECT_EQ("", value);
1038
1039 // However, the page should've correctly received the committed query.
1040 EXPECT_TRUE(UpdateSearchState(preview_tab));
1041 EXPECT_EQ("search", value_);
1042 EXPECT_TRUE(verbatim_);
1043 EXPECT_EQ(1, onsubmitcalls_);
1044 EXPECT_EQ(1, onvisibilitycalls_);
1045 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/instant/instant_controller.cc » ('j') | chrome/browser/instant/instant_controller.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698