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

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

Issue 12386019: Instant: Use only one hidden WebContents per profile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 9 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/instant/instant_test_utils.h" 5 #include "chrome/browser/instant/instant_test_utils.h"
6 6
7 #include "base/command_line.h" 7 #include "base/path_service.h"
8 #include "base/prefs/pref_service.h" 8 #include "chrome/browser/instant/instant_controller.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/instant/instant_overlay_model.h"
10 #include "chrome/browser/instant/instant_preloader.h"
11 #include "chrome/browser/instant/instant_service.h"
12 #include "chrome/browser/instant/instant_service_factory.h"
10 #include "chrome/browser/search_engines/template_url_service.h" 13 #include "chrome/browser/search_engines/template_url_service.h"
11 #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/ui/browser.h"
16 #include "chrome/browser/ui/browser_instant_controller.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/omnibox/location_bar.h"
12 #include "chrome/browser/ui/omnibox/omnibox_view.h" 19 #include "chrome/browser/ui/omnibox/omnibox_view.h"
20 #include "chrome/common/chrome_constants.h"
13 #include "chrome/common/chrome_notification_types.h" 21 #include "chrome/common/chrome_notification_types.h"
14 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/pref_names.h" 23 #include "chrome/test/base/testing_profile.h"
16 #include "chrome/test/base/interactive_test_utils.h"
17 #include "chrome/test/base/ui_test_utils.h" 24 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/render_process_host.h" 25 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/result_codes.h" 27 #include "content/public/common/result_codes.h"
22 #include "content/public/test/browser_test_utils.h" 28 #include "content/public/test/browser_test_utils.h"
23 29
24 namespace { 30 namespace {
25 31
26 std::string WrapScript(const std::string& script) { 32 std::string WrapScript(const std::string& script) {
27 return "domAutomationController.send(" + script + ")"; 33 return "domAutomationController.send(" + script + ")";
28 } 34 }
29 35
30 } // namespace 36 } // namespace
31 37
32 // InstantTestModelObserver -------------------------------------------------- 38 // InstantTestOverlayModelObserver --------------------------------------------
33 39
34 InstantTestModelObserver::InstantTestModelObserver( 40 InstantTestOverlayModelObserver::InstantTestOverlayModelObserver(
35 InstantOverlayModel* model, 41 InstantController* controller,
36 chrome::search::Mode::Type desired_mode_type) 42 bool expected_to_show)
37 : model_(model), 43 : controller_(controller),
38 desired_mode_type_(desired_mode_type) { 44 expected_to_show_(expected_to_show) {
39 model_->AddObserver(this); 45 controller_->AddOverlayModelObserver(this);
40 } 46 }
41 47
42 InstantTestModelObserver::~InstantTestModelObserver() { 48 InstantTestOverlayModelObserver::~InstantTestOverlayModelObserver() {
43 model_->RemoveObserver(this); 49 controller_->RemoveOverlayModelObserver(this);
44 } 50 }
45 51
46 void InstantTestModelObserver::WaitForDesiredOverlayState() { 52 void InstantTestOverlayModelObserver::WaitForDesiredOverlayState() {
47 run_loop_.Run(); 53 run_loop_.Run();
48 } 54 }
49 55
50 void InstantTestModelObserver::OverlayStateChanged( 56 void InstantTestOverlayModelObserver::OverlayStateChanged(
51 const InstantOverlayModel& model) { 57 const InstantOverlayModel& model) {
52 if (model.mode().mode == desired_mode_type_) 58 if (expected_to_show_ == (model.contents() != NULL))
53 run_loop_.Quit(); 59 run_loop_.Quit();
54 } 60 }
55 61
56 // InstantTestBase ----------------------------------------------------------- 62 // InstantTestBase ------------------------------------------------------------
57 63
58 void InstantTestBase::SetupInstant(Browser* browser) { 64 InstantTestBase::InstantTestBase()
59 browser_ = browser; 65 : https_test_server_(
66 net::TestServer::TYPE_HTTPS,
67 net::BaseTestServer::SSLOptions(),
68 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
69 browser_(NULL) {
70 }
71
72 InstantTestBase::~InstantTestBase() {
73 }
74
75 // Set up the profile dir to explicitly null out any Instant URL. Without this,
76 // the dir ends up with a default "google.com" Instant URL which we might try
77 // to load at startup.
78 bool InstantTestBase::DisableInstantOnStartup() {
79 base::FilePath profile_dir;
80 if (!PathService::Get(chrome::DIR_USER_DATA, &profile_dir))
81 return false;
82
83 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
84 if (!file_util::CreateDirectory(profile_dir))
85 return false;
86
87 base::FilePath source_dir;
88 if (!PathService::Get(chrome::DIR_TEST_DATA, &source_dir))
89 return false;
90
91 source_dir = source_dir.AppendASCII("profiles").AppendASCII("instant");
92
93 if (!file_util::CopyFile(source_dir.AppendASCII("Preferences"),
94 profile_dir.Append(chrome::kPreferencesFilename)))
95 return false;
96
97 if (!file_util::CopyFile(source_dir.AppendASCII("Web Data"),
98 profile_dir.Append(chrome::kWebDataFilename)))
99 return false;
100
101 return true;
102 }
103
104 void InstantTestBase::SetupDefaultSearchProvider(
105 const TemplateURLData& template_data) {
60 TemplateURLService* service = 106 TemplateURLService* service =
61 TemplateURLServiceFactory::GetForProfile(browser_->profile()); 107 TemplateURLServiceFactory::GetForProfile(browser_->profile());
62 ui_test_utils::WaitForTemplateURLServiceToLoad(service); 108 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
63 109
64 TemplateURLData data; 110 TemplateURL* template_url =
65 // Necessary to use exact URL for both the main URL and the alternate URL for 111 new TemplateURL(browser_->profile(), template_data);
66 // search term extraction to work in InstantExtended.
67 data.SetURL(instant_url_.spec() + "q={searchTerms}");
68 data.instant_url = instant_url_.spec();
69 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
70 data.search_terms_replacement_key = "strk";
71
72 TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
73 service->Add(template_url); // Takes ownership of |template_url|. 112 service->Add(template_url); // Takes ownership of |template_url|.
74 service->SetDefaultSearchProvider(template_url); 113 service->SetDefaultSearchProvider(template_url);
75
76 browser_->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true);
77
78 // TODO(shishir): Fix this ugly hack.
79 instant()->SetInstantEnabled(false, true);
80 instant()->SetInstantEnabled(true, false);
81 } 114 }
82 115
83 void InstantTestBase::Init(const GURL& instant_url) { 116 InstantService* InstantTestBase::service() const {
84 instant_url_ = instant_url; 117 return InstantServiceFactory::GetForProfile(browser_->profile());
85 } 118 }
86 119
87 void InstantTestBase::KillInstantRenderView() { 120 InstantController* InstantTestBase::instant() const {
88 base::KillProcess( 121 return browser_->instant_controller()->instant();
89 instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(), 122 }
90 content::RESULT_CODE_KILLED, 123
91 false); 124 OmniboxView* InstantTestBase::omnibox() const {
125 return browser_->window()->GetLocationBar()->GetLocationEntry();
92 } 126 }
93 127
94 void InstantTestBase::FocusOmnibox() { 128 void InstantTestBase::FocusOmnibox() {
95 // If the omnibox already has focus, just notify Instant. 129 // If the omnibox already has focus, just notify Instant.
96 if (omnibox()->model()->has_focus()) { 130 if (omnibox()->model()->has_focus()) {
97 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, 131 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE,
98 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); 132 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL);
99 } else { 133 } else {
100 browser_->window()->GetLocationBar()->FocusLocation(false); 134 browser_->window()->GetLocationBar()->FocusLocation(false);
101 } 135 }
102 } 136 }
103 137
104 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() { 138 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() {
105 content::WindowedNotificationObserver observer( 139 content::WindowedNotificationObserver observer(
106 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, 140 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED,
107 content::NotificationService::AllSources()); 141 content::NotificationService::AllSources());
108 FocusOmnibox(); 142 FocusOmnibox();
109 observer.Wait(); 143 if (!service()->preloader()->supports_instant())
110 } 144 observer.Wait();
111
112 void InstantTestBase::FocusOmniboxAndWaitForInstantExtendedSupport() {
113 content::WindowedNotificationObserver ntp_observer(
114 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED,
115 content::NotificationService::AllSources());
116 content::WindowedNotificationObserver overlay_observer(
117 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED,
118 content::NotificationService::AllSources());
119 FocusOmnibox();
120 ntp_observer.Wait();
121 overlay_observer.Wait();
122 } 145 }
123 146
124 void InstantTestBase::SetOmniboxText(const std::string& text) { 147 void InstantTestBase::SetOmniboxText(const std::string& text) {
125 FocusOmnibox(); 148 FocusOmnibox();
126 omnibox()->SetUserText(UTF8ToUTF16(text)); 149 omnibox()->SetUserText(UTF8ToUTF16(text));
127 } 150 }
128 151
129 void InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow( 152 void InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow(
130 const std::string& text) { 153 const std::string& text) {
131 InstantTestModelObserver observer( 154 InstantTestOverlayModelObserver observer(instant(), true);
132 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS);
133 SetOmniboxText(text); 155 SetOmniboxText(text);
134 observer.WaitForDesiredOverlayState(); 156 observer.WaitForDesiredOverlayState();
135 } 157 }
136 158
137 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, 159 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
138 const std::string& script, 160 const std::string& script,
139 bool* result) { 161 bool* result) {
140 return content::ExecuteScriptAndExtractBool( 162 return content::ExecuteScriptAndExtractBool(
141 contents, WrapScript(script), result); 163 contents, WrapScript(script), result);
142 } 164 }
143 165
144 bool InstantTestBase::GetIntFromJS(content::WebContents* contents, 166 bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
145 const std::string& script, 167 const std::string& script,
146 int* result) { 168 int* result) {
147 return content::ExecuteScriptAndExtractInt( 169 return content::ExecuteScriptAndExtractInt(
148 contents, WrapScript(script), result); 170 contents, WrapScript(script), result);
149 } 171 }
150 172
151 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, 173 bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
152 const std::string& script, 174 const std::string& script,
153 std::string* result) { 175 std::string* result) {
154 return content::ExecuteScriptAndExtractString( 176 return content::ExecuteScriptAndExtractString(
155 contents, WrapScript(script), result); 177 contents, WrapScript(script), result);
156 } 178 }
157 179
158 bool InstantTestBase::ExecuteScript(const std::string& script) {
159 return content::ExecuteScript(instant()->GetOverlayContents(), script);
160 }
161
162 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, 180 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
163 bool expected) { 181 bool expected) {
164 bool actual = !expected; // Purposely start with a mis-match. 182 bool actual = !expected; // Purposely start with a mis-match.
165 // We can only use ASSERT_*() in a method that returns void, hence this 183 // We can only use ASSERT_*() in a method that returns void, hence this
166 // convoluted check. 184 // convoluted check.
167 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && 185 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) &&
168 actual == expected; 186 actual == expected;
169 } 187 }
170 188
171 bool InstantTestBase::HasUserInputInProgress() { 189 void InstantTestBase::KillOverlayRenderView() {
172 return omnibox()->model()->user_input_in_progress_; 190 base::KillProcess(
191 instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(),
192 content::RESULT_CODE_KILLED,
193 false);
173 } 194 }
174 195
175 bool InstantTestBase::HasTemporaryText() { 196 bool InstantTestBase::LoadImage(content::WebContents* contents,
176 return omnibox()->model()->has_temporary_text_; 197 const std::string& image_url,
177 }
178
179 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
180 const std::string& image,
181 bool* loaded) { 198 bool* loaded) {
182 std::string js_chrome = 199 std::string script =
183 "var img = document.createElement('img');" 200 "var img = document.createElement('img');"
184 "img.onerror = function() { domAutomationController.send(false); };" 201 "img.onerror = function() { domAutomationController.send(false); };"
185 "img.onload = function() { domAutomationController.send(true); };" 202 "img.onload = function() { domAutomationController.send(true); };"
186 "img.src = '" + image + "';"; 203 "img.src = '" + image_url + "';";
187 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); 204 return content::ExecuteScriptAndExtractBool(contents, script, loaded);
188 } 205 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698