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/instant/instant_test_utils.h" | 5 #include "chrome/browser/instant/instant_test_utils.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/path_service.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "chrome/browser/instant/instant_controller.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/instant/instant_loader.h" |
| 10 #include "chrome/browser/instant/instant_model.h" |
| 11 #include "chrome/browser/instant/instant_service.h" |
| 12 #include "chrome/browser/instant/instant_service_factory.h" |
10 #include "chrome/browser/search_engines/template_url_service.h" | 13 #include "chrome/browser/search_engines/template_url_service.h" |
11 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
12 #include "chrome/common/pref_names.h" | 15 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/test/base/interactive_test_utils.h" | 16 #include "chrome/browser/ui/browser_instant_controller.h" |
| 17 #include "chrome/browser/ui/browser_window.h" |
| 18 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 19 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 20 #include "chrome/common/chrome_constants.h" |
| 21 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/test/base/testing_profile.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 23 #include "chrome/test/base/ui_test_utils.h" |
15 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
17 #include "content/public/common/result_codes.h" | 26 #include "content/public/common/result_codes.h" |
18 #include "content/public/test/browser_test_utils.h" | 27 #include "content/public/test/browser_test_utils.h" |
19 | 28 |
20 namespace { | 29 namespace { |
21 | 30 |
22 std::string WrapScript(const std::string& script) { | 31 std::string WrapScript(const std::string& script) { |
23 return "domAutomationController.send(" + script + ")"; | 32 return "domAutomationController.send(" + script + ")"; |
24 } | 33 } |
25 | 34 |
| 35 class InstantTestServiceObserver : public InstantServiceObserver { |
| 36 public: |
| 37 explicit InstantTestServiceObserver(InstantService* service) |
| 38 : service_(service) { |
| 39 service_->AddObserver(this); |
| 40 } |
| 41 |
| 42 virtual ~InstantTestServiceObserver() { |
| 43 service_->RemoveObserver(this); |
| 44 } |
| 45 |
| 46 void WaitForInstantSupport() { |
| 47 run_loop_.Run(); |
| 48 } |
| 49 |
| 50 private: |
| 51 // Overridden from InstantServiceObserver: |
| 52 virtual void InstantStatusChanged() OVERRIDE {} |
| 53 virtual void ThemeInfoChanged() OVERRIDE {} |
| 54 virtual void MostVisitedItemsChanged() OVERRIDE {} |
| 55 virtual void InstantSupportDecided() OVERRIDE { |
| 56 run_loop_.Quit(); |
| 57 } |
| 58 |
| 59 InstantService* const service_; |
| 60 base::RunLoop run_loop_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(InstantTestServiceObserver); |
| 63 }; |
| 64 |
26 } // namespace | 65 } // namespace |
27 | 66 |
28 // InstantTestModelObserver -------------------------------------------------- | 67 // InstantTestModelObserver --------------------------------------------------- |
29 | 68 |
30 InstantTestModelObserver::InstantTestModelObserver( | 69 InstantTestModelObserver::InstantTestModelObserver( |
31 InstantModel* model, | 70 InstantController* controller, |
32 chrome::search::Mode::Type desired_mode_type) | 71 const InstantModel* desired_model) |
33 : model_(model), | 72 : controller_(controller), |
34 desired_mode_type_(desired_mode_type) { | 73 desired_model_(desired_model) { |
35 model_->AddObserver(this); | 74 controller_->AddModelObserver(this); |
36 } | 75 } |
37 | 76 |
38 InstantTestModelObserver::~InstantTestModelObserver() { | 77 InstantTestModelObserver::~InstantTestModelObserver() { |
39 model_->RemoveObserver(this); | 78 controller_->RemoveModelObserver(this); |
40 } | 79 } |
41 | 80 |
42 void InstantTestModelObserver::WaitUntilDesiredPreviewState() { | 81 void InstantTestModelObserver::WaitForDesiredOverlayState() { |
43 run_loop_.Run(); | 82 run_loop_.Run(); |
44 } | 83 } |
45 | 84 |
46 void InstantTestModelObserver::PreviewStateChanged(const InstantModel& model) { | 85 void InstantTestModelObserver::OverlayStateChanged(const InstantModel& model) { |
47 if (model.mode().mode == desired_mode_type_) | 86 if (model.overlay() == desired_model_->overlay() && |
| 87 model.height() == desired_model_->height() && |
| 88 model.is_height_in_pixels() == desired_model_->is_height_in_pixels()) |
48 run_loop_.Quit(); | 89 run_loop_.Quit(); |
49 } | 90 } |
50 | 91 |
51 // InstantTestBase ----------------------------------------------------------- | 92 // InstantTestBase ------------------------------------------------------------ |
52 | 93 |
53 void InstantTestBase::SetupInstant() { | 94 InstantTestBase::InstantTestBase() |
| 95 : https_test_server_( |
| 96 net::TestServer::TYPE_HTTPS, |
| 97 net::BaseTestServer::SSLOptions(), |
| 98 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))), |
| 99 browser_(NULL) { |
| 100 } |
| 101 |
| 102 InstantTestBase::~InstantTestBase() { |
| 103 } |
| 104 |
| 105 // Set up the profile dir to explicitly null out any Instant URL. Without this, |
| 106 // the dir ends up with a default "google.com" Instant URL which we might try |
| 107 // to load at startup. |
| 108 bool InstantTestBase::DisableInstantOnStartup() { |
| 109 base::FilePath profile_dir; |
| 110 if (!PathService::Get(chrome::DIR_USER_DATA, &profile_dir)) |
| 111 return false; |
| 112 |
| 113 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); |
| 114 if (!file_util::CreateDirectory(profile_dir)) |
| 115 return false; |
| 116 |
| 117 base::FilePath source_dir; |
| 118 if (!PathService::Get(chrome::DIR_TEST_DATA, &source_dir)) |
| 119 return false; |
| 120 |
| 121 source_dir = source_dir.AppendASCII("profiles").AppendASCII("instant"); |
| 122 |
| 123 if (!file_util::CopyFile(source_dir.AppendASCII("Preferences"), |
| 124 profile_dir.Append(chrome::kPreferencesFilename))) |
| 125 return false; |
| 126 |
| 127 if (!file_util::CopyFile(source_dir.AppendASCII("Web Data"), |
| 128 profile_dir.Append(chrome::kWebDataFilename))) |
| 129 return false; |
| 130 |
| 131 return true; |
| 132 } |
| 133 |
| 134 void InstantTestBase::SetupDefaultSearchProvider( |
| 135 const TemplateURLData& template_data) { |
54 TemplateURLService* service = | 136 TemplateURLService* service = |
55 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | 137 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
56 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 138 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
57 | 139 |
58 TemplateURLData data; | 140 TemplateURL* template_url = |
59 // Necessary to use exact URL for both the main URL and the alternate URL for | 141 new TemplateURL(browser_->profile(), template_data); |
60 // search term extraction to work in InstantExtended. | |
61 data.SetURL(instant_url_.spec() + "q={searchTerms}"); | |
62 data.instant_url = instant_url_.spec(); | |
63 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | |
64 data.search_terms_replacement_key = "strk"; | |
65 | |
66 TemplateURL* template_url = new TemplateURL(browser()->profile(), data); | |
67 service->Add(template_url); // Takes ownership of |template_url|. | 142 service->Add(template_url); // Takes ownership of |template_url|. |
68 service->SetDefaultSearchProvider(template_url); | 143 service->SetDefaultSearchProvider(template_url); |
69 | |
70 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); | |
71 | |
72 // TODO(shishir): Fix this ugly hack. | |
73 instant()->SetInstantEnabled(false, true); | |
74 instant()->SetInstantEnabled(true, false); | |
75 } | 144 } |
76 | 145 |
77 void InstantTestBase::KillInstantRenderView() { | 146 InstantService* InstantTestBase::service() const { |
78 base::KillProcess( | 147 return InstantServiceFactory::GetForProfile(browser_->profile()); |
79 instant()->GetPreviewContents()->GetRenderProcessHost()->GetHandle(), | 148 } |
80 content::RESULT_CODE_KILLED, | 149 |
81 false); | 150 InstantController* InstantTestBase::instant() const { |
| 151 return browser_->instant_controller()->instant(); |
| 152 } |
| 153 |
| 154 OmniboxView* InstantTestBase::omnibox() const { |
| 155 return browser_->window()->GetLocationBar()->GetLocationEntry(); |
82 } | 156 } |
83 | 157 |
84 void InstantTestBase::FocusOmnibox() { | 158 void InstantTestBase::FocusOmnibox() { |
85 // If the omnibox already has focus, just notify Instant. | 159 // If the omnibox already has focus, just notify Instant. |
86 if (omnibox()->model()->has_focus()) { | 160 if (omnibox()->model()->has_focus()) { |
87 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | 161 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
88 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | 162 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
89 } else { | 163 } else { |
90 browser()->window()->GetLocationBar()->FocusLocation(false); | 164 browser_->window()->GetLocationBar()->FocusLocation(false); |
91 } | 165 } |
92 } | 166 } |
93 | 167 |
| 168 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() { |
| 169 InstantTestServiceObserver observer(service()); |
| 170 FocusOmnibox(); |
| 171 if (!service()->loader()->supports_instant()) |
| 172 observer.WaitForInstantSupport(); |
| 173 } |
| 174 |
94 void InstantTestBase::SetOmniboxText(const std::string& text) { | 175 void InstantTestBase::SetOmniboxText(const std::string& text) { |
95 FocusOmnibox(); | 176 FocusOmnibox(); |
96 omnibox()->SetUserText(UTF8ToUTF16(text)); | 177 omnibox()->SetUserText(UTF8ToUTF16(text)); |
97 } | 178 } |
98 | 179 |
99 void InstantTestBase::SetOmniboxTextAndWaitForInstantToShow( | 180 void InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow( |
100 const std::string& text) { | 181 const std::string& text) { |
101 InstantTestModelObserver observer( | 182 InstantModel desired_model(service()); |
102 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | 183 desired_model.SetOverlayState(service()->loader()->contents(), 100, false); |
| 184 InstantTestModelObserver observer(instant(), &desired_model); |
103 SetOmniboxText(text); | 185 SetOmniboxText(text); |
104 observer.WaitUntilDesiredPreviewState(); | 186 observer.WaitForDesiredOverlayState(); |
105 } | 187 } |
106 | 188 |
107 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, | 189 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, |
108 const std::string& script, | 190 const std::string& script, |
109 bool* result) { | 191 bool* result) { |
110 return content::ExecuteScriptAndExtractBool( | 192 return content::ExecuteScriptAndExtractBool( |
111 contents, WrapScript(script), result); | 193 contents, WrapScript(script), result); |
112 } | 194 } |
113 | 195 |
114 bool InstantTestBase::GetIntFromJS(content::WebContents* contents, | 196 bool InstantTestBase::GetIntFromJS(content::WebContents* contents, |
115 const std::string& script, | 197 const std::string& script, |
116 int* result) { | 198 int* result) { |
117 return content::ExecuteScriptAndExtractInt( | 199 return content::ExecuteScriptAndExtractInt( |
118 contents, WrapScript(script), result); | 200 contents, WrapScript(script), result); |
119 } | 201 } |
120 | 202 |
121 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, | 203 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, |
122 const std::string& script, | 204 const std::string& script, |
123 std::string* result) { | 205 std::string* result) { |
124 return content::ExecuteScriptAndExtractString( | 206 return content::ExecuteScriptAndExtractString( |
125 contents, WrapScript(script), result); | 207 contents, WrapScript(script), result); |
126 } | 208 } |
127 | 209 |
128 bool InstantTestBase::ExecuteScript(const std::string& script) { | |
129 return content::ExecuteScript(instant()->GetPreviewContents(), script); | |
130 } | |
131 | |
132 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, | 210 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, |
133 bool expected) { | 211 bool expected) { |
134 bool actual = !expected; // Purposely start with a mis-match. | 212 bool actual = !expected; // Purposely start with a mis-match. |
135 // We can only use ASSERT_*() in a method that returns void, hence this | 213 // We can only use ASSERT_*() in a method that returns void, hence this |
136 // convoluted check. | 214 // convoluted check. |
137 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && | 215 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && |
138 actual == expected; | 216 actual == expected; |
139 } | 217 } |
140 | 218 |
141 bool InstantTestBase::HasUserInputInProgress() { | 219 void InstantTestBase::KillOverlayRenderView() { |
142 return omnibox()->model()->user_input_in_progress_; | 220 base::KillProcess( |
| 221 instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(), |
| 222 content::RESULT_CODE_KILLED, |
| 223 false); |
143 } | 224 } |
144 | |
145 bool InstantTestBase::HasTemporaryText() { | |
146 return omnibox()->model()->has_temporary_text_; | |
147 } | |
OLD | NEW |