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