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