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" | |
13 #include "chrome/browser/search/instant_service_factory.h" | |
14 #include "chrome/browser/search_engines/template_url_service.h" | 12 #include "chrome/browser/search_engines/template_url_service.h" |
15 #include "chrome/browser/search_engines/template_url_service_factory.h" | 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |
16 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 14 #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" | 15 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
21 #include "chrome/test/base/interactive_test_utils.h" | 17 #include "chrome/test/base/interactive_test_utils.h" |
22 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
23 #include "components/variations/entropy_provider.h" | 19 #include "components/variations/entropy_provider.h" |
24 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
26 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
27 #include "content/public/common/result_codes.h" | 23 #include "content/public/common/result_codes.h" |
28 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
(...skipping 14 matching lines...) Expand all Loading... |
43 net::BaseTestServer::SSLOptions(), | 39 net::BaseTestServer::SSLOptions(), |
44 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))), | 40 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))), |
45 init_suggestions_url_(false) { | 41 init_suggestions_url_(false) { |
46 } | 42 } |
47 | 43 |
48 InstantTestBase::~InstantTestBase() {} | 44 InstantTestBase::~InstantTestBase() {} |
49 | 45 |
50 void InstantTestBase::SetupInstant(Browser* browser) { | 46 void InstantTestBase::SetupInstant(Browser* browser) { |
51 browser_ = browser; | 47 browser_ = browser; |
52 | 48 |
53 // TODO(samarth): update tests to work with cacheable NTP and remove this. | |
54 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
55 "InstantExtended", "Group1 use_cacheable_ntp:0")); | |
56 | |
57 TemplateURLService* service = | 49 TemplateURLService* service = |
58 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | 50 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
59 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 51 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
60 | 52 |
61 TemplateURLData data; | 53 TemplateURLData data; |
62 // Necessary to use exact URL for both the main URL and the alternate URL for | 54 // Necessary to use exact URL for both the main URL and the alternate URL for |
63 // search term extraction to work in InstantExtended. | 55 // search term extraction to work in InstantExtended. |
64 data.short_name = base::ASCIIToUTF16("name"); | 56 data.short_name = base::ASCIIToUTF16("name"); |
65 data.SetURL(instant_url_.spec() + | 57 data.SetURL(instant_url_.spec() + |
66 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}"); | 58 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}"); |
67 data.instant_url = instant_url_.spec(); | 59 data.instant_url = instant_url_.spec(); |
| 60 data.new_tab_url = ntp_url_.spec(); |
68 if (init_suggestions_url_) | 61 if (init_suggestions_url_) |
69 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}"; | 62 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}"; |
70 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | 63 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); |
71 data.search_terms_replacement_key = "strk"; | 64 data.search_terms_replacement_key = "strk"; |
72 | 65 |
73 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); | 66 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); |
74 service->Add(template_url); // Takes ownership of |template_url|. | 67 service->Add(template_url); // Takes ownership of |template_url|. |
75 service->SetDefaultSearchProvider(template_url); | 68 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 } | 69 } |
82 | 70 |
83 void InstantTestBase::SetInstantURL(const std::string& url) { | 71 void InstantTestBase::SetInstantURL(const std::string& url) { |
84 TemplateURLService* service = | 72 TemplateURLService* service = |
85 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | 73 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
86 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 74 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
87 | 75 |
88 TemplateURLData data; | 76 TemplateURLData data; |
89 data.short_name = base::ASCIIToUTF16("name"); | 77 data.short_name = base::ASCIIToUTF16("name"); |
90 data.SetURL(url); | 78 data.SetURL(url); |
91 data.instant_url = url; | 79 data.instant_url = url; |
92 | 80 |
93 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); | 81 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); |
94 service->Add(template_url); // Takes ownership of |template_url|. | 82 service->Add(template_url); // Takes ownership of |template_url|. |
95 service->SetDefaultSearchProvider(template_url); | 83 service->SetDefaultSearchProvider(template_url); |
96 } | 84 } |
97 | 85 |
98 void InstantTestBase::Init(const GURL& instant_url, bool init_suggestions_url) { | 86 void InstantTestBase::Init(const GURL& instant_url, |
| 87 const GURL& ntp_url, |
| 88 bool init_suggestions_url) { |
99 instant_url_ = instant_url; | 89 instant_url_ = instant_url; |
| 90 ntp_url_ = ntp_url; |
100 init_suggestions_url_ = init_suggestions_url; | 91 init_suggestions_url_ = init_suggestions_url; |
101 } | 92 } |
102 | 93 |
103 void InstantTestBase::FocusOmnibox() { | 94 void InstantTestBase::FocusOmnibox() { |
104 // If the omnibox already has focus, just notify Instant. | 95 // If the omnibox already has focus, just notify Instant. |
105 if (omnibox()->model()->has_focus()) { | 96 if (omnibox()->model()->has_focus()) { |
106 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | 97 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
107 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | 98 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
108 } else { | 99 } else { |
109 browser_->window()->GetLocationBar()->FocusLocation(false); | 100 browser_->window()->GetLocationBar()->FocusLocation(false); |
110 } | 101 } |
111 } | 102 } |
112 | 103 |
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) { | 104 void InstantTestBase::SetOmniboxText(const std::string& text) { |
128 FocusOmnibox(); | 105 FocusOmnibox(); |
129 omnibox()->SetUserText(base::UTF8ToUTF16(text)); | 106 omnibox()->SetUserText(base::UTF8ToUTF16(text)); |
130 } | 107 } |
131 | 108 |
132 void InstantTestBase::PressEnterAndWaitForNavigation() { | 109 void InstantTestBase::PressEnterAndWaitForNavigation() { |
133 content::WindowedNotificationObserver nav_observer( | 110 content::WindowedNotificationObserver nav_observer( |
134 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 111 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
135 content::NotificationService::AllSources()); | 112 content::NotificationService::AllSources()); |
136 browser_->window()->GetLocationBar()->AcceptInput(); | 113 browser_->window()->GetLocationBar()->AcceptInput(); |
(...skipping 14 matching lines...) Expand all Loading... |
151 contents, WrapScript(script), result); | 128 contents, WrapScript(script), result); |
152 } | 129 } |
153 | 130 |
154 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, | 131 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, |
155 const std::string& script, | 132 const std::string& script, |
156 std::string* result) { | 133 std::string* result) { |
157 return content::ExecuteScriptAndExtractString( | 134 return content::ExecuteScriptAndExtractString( |
158 contents, WrapScript(script), result); | 135 contents, WrapScript(script), result); |
159 } | 136 } |
160 | 137 |
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, | 138 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, |
170 bool expected) { | 139 bool expected) { |
171 bool actual = !expected; // Purposely start with a mis-match. | 140 bool actual = !expected; // Purposely start with a mis-match. |
172 // We can only use ASSERT_*() in a method that returns void, hence this | 141 // We can only use ASSERT_*() in a method that returns void, hence this |
173 // convoluted check. | 142 // convoluted check. |
174 return GetBoolFromJS(contents, "!document.hidden", &actual) && | 143 return GetBoolFromJS(contents, "!document.hidden", &actual) && |
175 actual == expected; | 144 actual == expected; |
176 } | 145 } |
177 | 146 |
178 std::string InstantTestBase::GetOmniboxText() { | 147 std::string InstantTestBase::GetOmniboxText() { |
(...skipping 11 matching lines...) Expand all Loading... |
190 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); | 159 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); |
191 } | 160 } |
192 | 161 |
193 base::string16 InstantTestBase::GetBlueText() { | 162 base::string16 InstantTestBase::GetBlueText() { |
194 size_t start = 0, end = 0; | 163 size_t start = 0, end = 0; |
195 omnibox()->GetSelectionBounds(&start, &end); | 164 omnibox()->GetSelectionBounds(&start, &end); |
196 if (start > end) | 165 if (start > end) |
197 std::swap(start, end); | 166 std::swap(start, end); |
198 return omnibox()->GetText().substr(start, end - start); | 167 return omnibox()->GetText().substr(start, end - start); |
199 } | 168 } |
OLD | NEW |