OLD | NEW |
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/ui/search/instant_test_utils.h" | 5 #include "chrome/browser/ui/search/instant_test_utils.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/search_engines/template_url_service.h" | 11 #include "chrome/browser/search_engines/template_url_service.h" |
12 #include "chrome/browser/search_engines/template_url_service_factory.h" | 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
13 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 13 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
14 #include "chrome/browser/ui/search/instant_ntp.h" | 14 #include "chrome/browser/ui/search/instant_ntp.h" |
15 #include "chrome/browser/ui/search/instant_overlay.h" | |
16 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
19 #include "chrome/test/base/interactive_test_utils.h" | 18 #include "chrome/test/base/interactive_test_utils.h" |
20 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
21 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
24 #include "content/public/common/result_codes.h" | 23 #include "content/public/common/result_codes.h" |
25 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
26 | 25 |
27 namespace { | 26 namespace { |
28 | 27 |
29 std::string WrapScript(const std::string& script) { | 28 std::string WrapScript(const std::string& script) { |
30 return "domAutomationController.send(" + script + ")"; | 29 return "domAutomationController.send(" + script + ")"; |
31 } | 30 } |
32 | 31 |
33 } // namespace | 32 } // namespace |
34 | 33 |
35 // InstantTestModelObserver -------------------------------------------------- | |
36 | |
37 InstantTestModelObserver::InstantTestModelObserver( | |
38 InstantOverlayModel* model, | |
39 SearchMode::Type expected_mode_type) | |
40 : model_(model), | |
41 expected_mode_type_(expected_mode_type), | |
42 observed_mode_type_(static_cast<SearchMode::Type>(-1)) { | |
43 model_->AddObserver(this); | |
44 } | |
45 | |
46 InstantTestModelObserver::~InstantTestModelObserver() { | |
47 model_->RemoveObserver(this); | |
48 } | |
49 | |
50 SearchMode::Type InstantTestModelObserver::WaitForExpectedOverlayState() { | |
51 run_loop_.Run(); | |
52 return observed_mode_type_; | |
53 } | |
54 | |
55 void InstantTestModelObserver::OverlayStateChanged( | |
56 const InstantOverlayModel& model) { | |
57 observed_mode_type_ = model.mode().mode; | |
58 run_loop_.Quit(); | |
59 } | |
60 | |
61 // InstantTestBase ----------------------------------------------------------- | 34 // InstantTestBase ----------------------------------------------------------- |
62 | 35 |
63 void InstantTestBase::SetupInstant(Browser* browser) { | 36 void InstantTestBase::SetupInstant(Browser* browser) { |
64 browser_ = browser; | 37 browser_ = browser; |
65 TemplateURLService* service = | 38 TemplateURLService* service = |
66 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | 39 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
67 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 40 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
68 | 41 |
69 TemplateURLData data; | 42 TemplateURLData data; |
70 // Necessary to use exact URL for both the main URL and the alternate URL for | 43 // Necessary to use exact URL for both the main URL and the alternate URL for |
71 // search term extraction to work in InstantExtended. | 44 // search term extraction to work in InstantExtended. |
72 data.SetURL(instant_url_.spec() + | 45 data.SetURL(instant_url_.spec() + |
73 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}"); | 46 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}"); |
74 data.instant_url = instant_url_.spec(); | 47 data.instant_url = instant_url_.spec(); |
75 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | 48 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); |
76 data.search_terms_replacement_key = "strk"; | 49 data.search_terms_replacement_key = "strk"; |
77 | 50 |
78 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); | 51 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); |
79 service->Add(template_url); // Takes ownership of |template_url|. | 52 service->Add(template_url); // Takes ownership of |template_url|. |
80 service->SetDefaultSearchProvider(template_url); | 53 service->SetDefaultSearchProvider(template_url); |
81 | 54 |
82 // TODO(shishir): Fix this ugly hack. | 55 instant()->ReloadStaleNTP(); |
83 instant()->SetInstantEnabled(false, true); | |
84 instant()->SetInstantEnabled(true, false); | |
85 } | 56 } |
86 | 57 |
87 void InstantTestBase::SetInstantURL(const std::string& url) { | 58 void InstantTestBase::SetInstantURL(const std::string& url) { |
88 TemplateURLService* service = | 59 TemplateURLService* service = |
89 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | 60 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
90 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 61 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
91 | 62 |
92 TemplateURLData data; | 63 TemplateURLData data; |
93 data.SetURL(url); | 64 data.SetURL(url); |
94 data.instant_url = url; | 65 data.instant_url = url; |
95 | 66 |
96 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); | 67 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); |
97 service->Add(template_url); // Takes ownership of |template_url|. | 68 service->Add(template_url); // Takes ownership of |template_url|. |
98 service->SetDefaultSearchProvider(template_url); | 69 service->SetDefaultSearchProvider(template_url); |
99 } | 70 } |
100 | 71 |
101 void InstantTestBase::Init(const GURL& instant_url) { | 72 void InstantTestBase::Init(const GURL& instant_url) { |
102 instant_url_ = instant_url; | 73 instant_url_ = instant_url; |
103 } | 74 } |
104 | 75 |
105 void InstantTestBase::KillInstantRenderView() { | |
106 base::KillProcess( | |
107 instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(), | |
108 content::RESULT_CODE_KILLED, | |
109 false); | |
110 } | |
111 | |
112 void InstantTestBase::FocusOmnibox() { | 76 void InstantTestBase::FocusOmnibox() { |
113 // If the omnibox already has focus, just notify Instant. | 77 // If the omnibox already has focus, just notify Instant. |
114 if (omnibox()->model()->has_focus()) { | 78 if (omnibox()->model()->has_focus()) { |
115 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | 79 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
116 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | 80 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
117 } else { | 81 } else { |
118 browser_->window()->GetLocationBar()->FocusLocation(false); | 82 browser_->window()->GetLocationBar()->FocusLocation(false); |
119 } | 83 } |
120 } | 84 } |
121 | 85 |
122 void InstantTestBase::FocusOmniboxAndWaitForInstantNTPSupport() { | 86 void InstantTestBase::FocusOmniboxAndWaitForInstantNTPSupport() { |
123 content::WindowedNotificationObserver ntp_observer( | 87 content::WindowedNotificationObserver ntp_observer( |
124 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, | 88 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, |
125 content::NotificationService::AllSources()); | 89 content::NotificationService::AllSources()); |
126 FocusOmnibox(); | 90 FocusOmnibox(); |
127 if (!instant()->ntp() || !instant()->ntp()->supports_instant()) | 91 if (!instant()->ntp() || !instant()->ntp()->supports_instant()) |
128 ntp_observer.Wait(); | 92 ntp_observer.Wait(); |
129 } | 93 } |
130 | 94 |
131 void InstantTestBase::SetOmniboxText(const std::string& text) { | 95 void InstantTestBase::SetOmniboxText(const std::string& text) { |
132 FocusOmnibox(); | 96 FocusOmnibox(); |
133 omnibox()->SetUserText(UTF8ToUTF16(text)); | 97 omnibox()->SetUserText(UTF8ToUTF16(text)); |
134 } | 98 } |
135 | 99 |
136 bool InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow( | |
137 const std::string& text) { | |
138 // The order of events may be: | |
139 // { hide, show } or just { show } depending on the order things | |
140 // flow in from GWS and Chrome's response to hiding the infobar and/or | |
141 // bookmark bar. Note, the GWS response is relevant because of the | |
142 // Instant "MANUAL_*" tests. | |
143 InstantTestModelObserver first_observer( | |
144 instant()->model(), SearchMode::MODE_DEFAULT); | |
145 SetOmniboxText(text); | |
146 | |
147 SearchMode::Type observed = first_observer.WaitForExpectedOverlayState(); | |
148 if (observed == SearchMode::MODE_DEFAULT) { | |
149 InstantTestModelObserver second_observer( | |
150 instant()->model(), SearchMode::MODE_SEARCH_SUGGESTIONS); | |
151 observed = second_observer.WaitForExpectedOverlayState(); | |
152 } | |
153 EXPECT_EQ(SearchMode::MODE_SEARCH_SUGGESTIONS, observed); | |
154 return observed == SearchMode::MODE_SEARCH_SUGGESTIONS; | |
155 } | |
156 | |
157 void InstantTestBase::SetOmniboxTextAndWaitForSuggestion( | |
158 const std::string& text) { | |
159 content::WindowedNotificationObserver observer( | |
160 chrome::NOTIFICATION_INSTANT_SET_SUGGESTION, | |
161 content::NotificationService::AllSources()); | |
162 SetOmniboxText(text); | |
163 observer.Wait(); | |
164 } | |
165 | |
166 void InstantTestBase::PressEnterAndWaitForNavigation() { | 100 void InstantTestBase::PressEnterAndWaitForNavigation() { |
167 content::WindowedNotificationObserver nav_observer( | 101 content::WindowedNotificationObserver nav_observer( |
168 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 102 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
169 content::NotificationService::AllSources()); | 103 content::NotificationService::AllSources()); |
170 browser_->window()->GetLocationBar()->AcceptInput(); | 104 browser_->window()->GetLocationBar()->AcceptInput(); |
171 nav_observer.Wait(); | 105 nav_observer.Wait(); |
172 } | 106 } |
173 | 107 |
174 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, | 108 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, |
175 const std::string& script, | 109 const std::string& script, |
(...skipping 10 matching lines...) Expand all Loading... |
186 } | 120 } |
187 | 121 |
188 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, | 122 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, |
189 const std::string& script, | 123 const std::string& script, |
190 std::string* result) { | 124 std::string* result) { |
191 return content::ExecuteScriptAndExtractString( | 125 return content::ExecuteScriptAndExtractString( |
192 contents, WrapScript(script), result); | 126 contents, WrapScript(script), result); |
193 } | 127 } |
194 | 128 |
195 bool InstantTestBase::ExecuteScript(const std::string& script) { | 129 bool InstantTestBase::ExecuteScript(const std::string& script) { |
196 return content::ExecuteScript(instant()->GetOverlayContents(), script); | 130 return content::ExecuteScript(instant()->GetNTPContents(), script); |
197 } | 131 } |
198 | 132 |
199 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, | 133 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, |
200 bool expected) { | 134 bool expected) { |
201 bool actual = !expected; // Purposely start with a mis-match. | 135 bool actual = !expected; // Purposely start with a mis-match. |
202 // We can only use ASSERT_*() in a method that returns void, hence this | 136 // We can only use ASSERT_*() in a method that returns void, hence this |
203 // convoluted check. | 137 // convoluted check. |
204 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && | 138 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && |
205 actual == expected; | 139 actual == expected; |
206 } | 140 } |
(...skipping 25 matching lines...) Expand all Loading... |
232 size_t start = 0, end = 0; | 166 size_t start = 0, end = 0; |
233 omnibox()->GetSelectionBounds(&start, &end); | 167 omnibox()->GetSelectionBounds(&start, &end); |
234 if (start > end) | 168 if (start > end) |
235 std::swap(start, end); | 169 std::swap(start, end); |
236 return omnibox()->GetText().substr(start, end - start); | 170 return omnibox()->GetText().substr(start, end - start); |
237 } | 171 } |
238 | 172 |
239 string16 InstantTestBase::GetGrayText() { | 173 string16 InstantTestBase::GetGrayText() { |
240 return omnibox()->GetInstantSuggestion(); | 174 return omnibox()->GetInstantSuggestion(); |
241 } | 175 } |
OLD | NEW |