OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/instant/instant_test_utils.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/search_engines/template_url_service.h" | |
11 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
12 #include "chrome/browser/ui/omnibox/omnibox_view.h" | |
13 #include "chrome/common/chrome_notification_types.h" | |
14 #include "chrome/common/chrome_switches.h" | |
15 #include "chrome/common/pref_names.h" | |
16 #include "chrome/test/base/interactive_test_utils.h" | |
17 #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" | |
20 #include "content/public/browser/web_contents.h" | |
21 #include "content/public/common/result_codes.h" | |
22 #include "content/public/test/browser_test_utils.h" | |
23 | |
24 namespace { | |
25 | |
26 std::string WrapScript(const std::string& script) { | |
27 return "domAutomationController.send(" + script + ")"; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 // InstantTestModelObserver -------------------------------------------------- | |
33 | |
34 InstantTestModelObserver::InstantTestModelObserver( | |
35 InstantOverlayModel* model, | |
36 chrome::search::Mode::Type desired_mode_type) | |
37 : model_(model), | |
38 desired_mode_type_(desired_mode_type) { | |
39 model_->AddObserver(this); | |
40 } | |
41 | |
42 InstantTestModelObserver::~InstantTestModelObserver() { | |
43 model_->RemoveObserver(this); | |
44 } | |
45 | |
46 void InstantTestModelObserver::WaitForDesiredOverlayState() { | |
47 run_loop_.Run(); | |
48 } | |
49 | |
50 void InstantTestModelObserver::OverlayStateChanged( | |
51 const InstantOverlayModel& model) { | |
52 if (model.mode().mode == desired_mode_type_) | |
53 run_loop_.Quit(); | |
54 } | |
55 | |
56 // InstantTestBase ----------------------------------------------------------- | |
57 | |
58 void InstantTestBase::SetupInstant(Browser* browser) { | |
59 browser_ = browser; | |
60 TemplateURLService* service = | |
61 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | |
62 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | |
63 | |
64 TemplateURLData data; | |
65 // Necessary to use exact URL for both the main URL and the alternate URL for | |
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|. | |
74 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 } | |
82 | |
83 void InstantTestBase::Init(const GURL& instant_url) { | |
84 instant_url_ = instant_url; | |
85 } | |
86 | |
87 void InstantTestBase::KillInstantRenderView() { | |
88 base::KillProcess( | |
89 instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(), | |
90 content::RESULT_CODE_KILLED, | |
91 false); | |
92 } | |
93 | |
94 void InstantTestBase::FocusOmnibox() { | |
95 // If the omnibox already has focus, just notify Instant. | |
96 if (omnibox()->model()->has_focus()) { | |
97 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | |
98 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | |
99 } else { | |
100 browser_->window()->GetLocationBar()->FocusLocation(false); | |
101 } | |
102 } | |
103 | |
104 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() { | |
105 content::WindowedNotificationObserver observer( | |
106 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, | |
107 content::NotificationService::AllSources()); | |
108 FocusOmnibox(); | |
109 observer.Wait(); | |
110 } | |
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 } | |
123 | |
124 void InstantTestBase::SetOmniboxText(const std::string& text) { | |
125 FocusOmnibox(); | |
126 omnibox()->SetUserText(UTF8ToUTF16(text)); | |
127 } | |
128 | |
129 void InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow( | |
130 const std::string& text) { | |
131 InstantTestModelObserver observer( | |
132 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | |
133 SetOmniboxText(text); | |
134 observer.WaitForDesiredOverlayState(); | |
135 } | |
136 | |
137 void InstantTestBase::SetOmniboxTextAndWaitForSuggestion( | |
138 const std::string& text) { | |
139 content::WindowedNotificationObserver observer( | |
140 chrome::NOTIFICATION_INSTANT_SET_SUGGESTION, | |
141 content::NotificationService::AllSources()); | |
142 SetOmniboxText(text); | |
143 observer.Wait(); | |
144 } | |
145 | |
146 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, | |
147 const std::string& script, | |
148 bool* result) { | |
149 return content::ExecuteScriptAndExtractBool( | |
150 contents, WrapScript(script), result); | |
151 } | |
152 | |
153 bool InstantTestBase::GetIntFromJS(content::WebContents* contents, | |
154 const std::string& script, | |
155 int* result) { | |
156 return content::ExecuteScriptAndExtractInt( | |
157 contents, WrapScript(script), result); | |
158 } | |
159 | |
160 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, | |
161 const std::string& script, | |
162 std::string* result) { | |
163 return content::ExecuteScriptAndExtractString( | |
164 contents, WrapScript(script), result); | |
165 } | |
166 | |
167 bool InstantTestBase::ExecuteScript(const std::string& script) { | |
168 return content::ExecuteScript(instant()->GetOverlayContents(), script); | |
169 } | |
170 | |
171 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, | |
172 bool expected) { | |
173 bool actual = !expected; // Purposely start with a mis-match. | |
174 // We can only use ASSERT_*() in a method that returns void, hence this | |
175 // convoluted check. | |
176 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && | |
177 actual == expected; | |
178 } | |
179 | |
180 bool InstantTestBase::HasUserInputInProgress() { | |
181 return omnibox()->model()->user_input_in_progress_; | |
182 } | |
183 | |
184 bool InstantTestBase::HasTemporaryText() { | |
185 return omnibox()->model()->has_temporary_text_; | |
186 } | |
187 | |
188 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh, | |
189 const std::string& image, | |
190 bool* loaded) { | |
191 std::string js_chrome = | |
192 "var img = document.createElement('img');" | |
193 "img.onerror = function() { domAutomationController.send(false); };" | |
194 "img.onload = function() { domAutomationController.send(true); };" | |
195 "img.src = '" + image + "';"; | |
196 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); | |
197 } | |
OLD | NEW |