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/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/search/instant_service.h" | 12 #include "chrome/browser/search/instant_service.h" |
kmadhusu
2014/01/09 23:31:43
You can remove this and instant_service_factory.h
samarth
2014/01/10 05:13:32
Done.
| |
13 #include "chrome/browser/search/instant_service_factory.h" | 13 #include "chrome/browser/search/instant_service_factory.h" |
14 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
16 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 16 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
17 #include "chrome/browser/ui/search/instant_ntp.h" | |
18 #include "chrome/browser/ui/search/instant_ntp_prerenderer.h" | |
19 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
21 #include "chrome/test/base/interactive_test_utils.h" | 19 #include "chrome/test/base/interactive_test_utils.h" |
22 #include "chrome/test/base/ui_test_utils.h" | 20 #include "chrome/test/base/ui_test_utils.h" |
23 #include "components/variations/entropy_provider.h" | 21 #include "components/variations/entropy_provider.h" |
24 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
26 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
27 #include "content/public/common/result_codes.h" | 25 #include "content/public/common/result_codes.h" |
28 #include "content/public/test/browser_test_utils.h" | 26 #include "content/public/test/browser_test_utils.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}"); | 64 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}"); |
67 data.instant_url = instant_url_.spec(); | 65 data.instant_url = instant_url_.spec(); |
68 if (init_suggestions_url_) | 66 if (init_suggestions_url_) |
69 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}"; | 67 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}"; |
70 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | 68 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); |
71 data.search_terms_replacement_key = "strk"; | 69 data.search_terms_replacement_key = "strk"; |
72 | 70 |
73 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); | 71 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); |
74 service->Add(template_url); // Takes ownership of |template_url|. | 72 service->Add(template_url); // Takes ownership of |template_url|. |
75 service->SetDefaultSearchProvider(template_url); | 73 service->SetDefaultSearchProvider(template_url); |
76 | |
77 InstantService* instant_service = | |
78 InstantServiceFactory::GetForProfile(browser_->profile()); | |
79 ASSERT_NE(static_cast<InstantService*>(NULL), instant_service); | |
80 instant_service->ntp_prerenderer()->ReloadInstantNTP(); | |
81 } | 74 } |
82 | 75 |
83 void InstantTestBase::SetInstantURL(const std::string& url) { | 76 void InstantTestBase::SetInstantURL(const std::string& url) { |
84 TemplateURLService* service = | 77 TemplateURLService* service = |
85 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | 78 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
86 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 79 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
87 | 80 |
88 TemplateURLData data; | 81 TemplateURLData data; |
89 data.short_name = base::ASCIIToUTF16("name"); | 82 data.short_name = base::ASCIIToUTF16("name"); |
90 data.SetURL(url); | 83 data.SetURL(url); |
(...skipping 12 matching lines...) Expand all Loading... | |
103 void InstantTestBase::FocusOmnibox() { | 96 void InstantTestBase::FocusOmnibox() { |
104 // If the omnibox already has focus, just notify Instant. | 97 // If the omnibox already has focus, just notify Instant. |
105 if (omnibox()->model()->has_focus()) { | 98 if (omnibox()->model()->has_focus()) { |
106 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | 99 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
107 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | 100 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
108 } else { | 101 } else { |
109 browser_->window()->GetLocationBar()->FocusLocation(false); | 102 browser_->window()->GetLocationBar()->FocusLocation(false); |
110 } | 103 } |
111 } | 104 } |
112 | 105 |
113 void InstantTestBase::FocusOmniboxAndWaitForInstantNTPSupport() { | |
114 content::WindowedNotificationObserver ntp_observer( | |
115 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, | |
116 content::NotificationService::AllSources()); | |
117 FocusOmnibox(); | |
118 | |
119 InstantService* instant_service = | |
120 InstantServiceFactory::GetForProfile(browser_->profile()); | |
121 ASSERT_NE(static_cast<InstantService*>(NULL), instant_service); | |
122 if (!instant_service->ntp_prerenderer()->ntp() || | |
123 !instant_service->ntp_prerenderer()->ntp()->supports_instant()) | |
124 ntp_observer.Wait(); | |
125 } | |
126 | |
127 void InstantTestBase::SetOmniboxText(const std::string& text) { | 106 void InstantTestBase::SetOmniboxText(const std::string& text) { |
128 FocusOmnibox(); | 107 FocusOmnibox(); |
129 omnibox()->SetUserText(base::UTF8ToUTF16(text)); | 108 omnibox()->SetUserText(base::UTF8ToUTF16(text)); |
130 } | 109 } |
131 | 110 |
132 void InstantTestBase::PressEnterAndWaitForNavigation() { | 111 void InstantTestBase::PressEnterAndWaitForNavigation() { |
133 content::WindowedNotificationObserver nav_observer( | 112 content::WindowedNotificationObserver nav_observer( |
134 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 113 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
135 content::NotificationService::AllSources()); | 114 content::NotificationService::AllSources()); |
136 browser_->window()->GetLocationBar()->AcceptInput(); | 115 browser_->window()->GetLocationBar()->AcceptInput(); |
(...skipping 14 matching lines...) Expand all Loading... | |
151 contents, WrapScript(script), result); | 130 contents, WrapScript(script), result); |
152 } | 131 } |
153 | 132 |
154 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, | 133 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, |
155 const std::string& script, | 134 const std::string& script, |
156 std::string* result) { | 135 std::string* result) { |
157 return content::ExecuteScriptAndExtractString( | 136 return content::ExecuteScriptAndExtractString( |
158 contents, WrapScript(script), result); | 137 contents, WrapScript(script), result); |
159 } | 138 } |
160 | 139 |
161 bool InstantTestBase::ExecuteScript(const std::string& script) { | |
162 InstantService* instant_service = | |
163 InstantServiceFactory::GetForProfile(browser_instant()->profile()); | |
164 if (!instant_service) | |
165 return false; | |
166 return content::ExecuteScript(instant_service->GetNTPContents(), script); | |
167 } | |
168 | |
169 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, | 140 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, |
170 bool expected) { | 141 bool expected) { |
171 bool actual = !expected; // Purposely start with a mis-match. | 142 bool actual = !expected; // Purposely start with a mis-match. |
172 // We can only use ASSERT_*() in a method that returns void, hence this | 143 // We can only use ASSERT_*() in a method that returns void, hence this |
173 // convoluted check. | 144 // convoluted check. |
174 return GetBoolFromJS(contents, "!document.hidden", &actual) && | 145 return GetBoolFromJS(contents, "!document.hidden", &actual) && |
175 actual == expected; | 146 actual == expected; |
176 } | 147 } |
177 | 148 |
178 std::string InstantTestBase::GetOmniboxText() { | 149 std::string InstantTestBase::GetOmniboxText() { |
(...skipping 11 matching lines...) Expand all Loading... | |
190 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); | 161 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); |
191 } | 162 } |
192 | 163 |
193 base::string16 InstantTestBase::GetBlueText() { | 164 base::string16 InstantTestBase::GetBlueText() { |
194 size_t start = 0, end = 0; | 165 size_t start = 0, end = 0; |
195 omnibox()->GetSelectionBounds(&start, &end); | 166 omnibox()->GetSelectionBounds(&start, &end); |
196 if (start > end) | 167 if (start > end) |
197 std::swap(start, end); | 168 std::swap(start, end); |
198 return omnibox()->GetText().substr(start, end - start); | 169 return omnibox()->GetText().substr(start, end - start); |
199 } | 170 } |
OLD | NEW |