OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_INSTANT_TEST_UTILS_H_ |
| 6 #define CHROME_BROWSER_INSTANT_TEST_UTILS_H_ |
| 7 |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 9 #include "chrome/browser/history/history_service_factory.h" |
| 10 #include "chrome/browser/instant/instant_model_observer.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/search_engines/template_url_service.h" |
| 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 14 #include "chrome/browser/task_manager/task_manager.h" |
| 15 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
| 16 #include "chrome/browser/ui/browser_commands.h" |
| 17 #include "chrome/browser/ui/browser_instant_controller.h" |
| 18 #include "chrome/browser/ui/browser_tabstrip.h" |
| 19 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 21 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 23 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/test/base/in_process_browser_test.h" |
| 26 #include "chrome/test/base/interactive_test_utils.h" |
| 27 #include "chrome/test/base/ui_test_utils.h" |
| 28 #include "content/public/browser/notification_service.h" |
| 29 #include "content/public/browser/render_process_host.h" |
| 30 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/common/result_codes.h" |
| 32 #include "content/public/test/browser_test_utils.h" |
| 33 |
| 34 class InstantTestModelObserver : public InstantModelObserver { |
| 35 public: |
| 36 InstantTestModelObserver(const InstantModel* model, |
| 37 chrome::search::Mode::Type desired_mode_type) |
| 38 : model_(model), |
| 39 desired_mode_type_(desired_mode_type) { |
| 40 model_->AddObserver(this); |
| 41 } |
| 42 |
| 43 ~InstantTestModelObserver() { |
| 44 model_->RemoveObserver(this); |
| 45 } |
| 46 |
| 47 void WaitUntilDesiredPreviewState() { |
| 48 run_loop_.Run(); |
| 49 } |
| 50 |
| 51 // Overridden from InstantModelObserver: |
| 52 virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE { |
| 53 if (model.mode().mode == desired_mode_type_) |
| 54 run_loop_.Quit(); |
| 55 } |
| 56 |
| 57 private: |
| 58 const InstantModel* const model_; |
| 59 const chrome::search::Mode::Type desired_mode_type_; |
| 60 base::RunLoop run_loop_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(InstantTestModelObserver); |
| 63 }; |
| 64 |
| 65 class InstantTestBase : public InProcessBrowserTest { |
| 66 protected: |
| 67 void SetupInstant() { |
| 68 TemplateURLService* service = |
| 69 TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| 70 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
| 71 |
| 72 TemplateURLData data; |
| 73 data.SetURL("http://does/not/exist?q={searchTerms}"); |
| 74 data.instant_url = instant_url_.spec(); |
| 75 |
| 76 TemplateURL* template_url = new TemplateURL(browser()->profile(), data); |
| 77 service->Add(template_url); // Takes ownership of |template_url|. |
| 78 service->SetDefaultSearchProvider(template_url); |
| 79 |
| 80 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); |
| 81 instant()->SetTestingMode(); |
| 82 } |
| 83 |
| 84 InstantController* instant() { |
| 85 return browser()->instant_controller()->instant(); |
| 86 } |
| 87 |
| 88 OmniboxView* omnibox() { |
| 89 return browser()->window()->GetLocationBar()->GetLocationEntry(); |
| 90 } |
| 91 |
| 92 void KillInstantRenderView() { |
| 93 base::KillProcess( |
| 94 instant()->GetPreviewContents()->GetRenderProcessHost()->GetHandle(), |
| 95 content::RESULT_CODE_KILLED, |
| 96 false); |
| 97 } |
| 98 |
| 99 void FocusOmnibox() { |
| 100 // If the omnibox already has focus, just notify Instant. |
| 101 if (omnibox()->model()->has_focus()) { |
| 102 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
| 103 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
| 104 } |
| 105 else { |
| 106 browser()->window()->GetLocationBar()->FocusLocation(false); |
| 107 } |
| 108 } |
| 109 |
| 110 void FocusOmniboxAndWaitForInstantSupport() { |
| 111 content::WindowedNotificationObserver observer( |
| 112 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, |
| 113 content::NotificationService::AllSources()); |
| 114 FocusOmnibox(); |
| 115 observer.Wait(); |
| 116 } |
| 117 |
| 118 void SetOmniboxText(const std::string& text) { |
| 119 FocusOmnibox(); |
| 120 omnibox()->SetUserText(UTF8ToUTF16(text)); |
| 121 } |
| 122 |
| 123 void SetOmniboxTextAndWaitForInstantToShow(const std::string& text) { |
| 124 InstantTestModelObserver observer( |
| 125 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); |
| 126 SetOmniboxText(text); |
| 127 observer.WaitUntilDesiredPreviewState(); |
| 128 } |
| 129 |
| 130 std::wstring WrapScript(const std::string& script) const { |
| 131 return UTF8ToWide("domAutomationController.send(" + script + ")"); |
| 132 } |
| 133 |
| 134 bool GetBoolFromJS(content::RenderViewHost* rvh, |
| 135 const std::string& script, |
| 136 bool* result) WARN_UNUSED_RESULT { |
| 137 return content::ExecuteJavaScriptAndExtractBool(rvh, std::wstring(), |
| 138 WrapScript(script), result); |
| 139 } |
| 140 |
| 141 bool GetIntFromJS(content::RenderViewHost* rvh, |
| 142 const std::string& script, |
| 143 int* result) WARN_UNUSED_RESULT { |
| 144 return content::ExecuteJavaScriptAndExtractInt(rvh, std::wstring(), |
| 145 WrapScript(script), result); |
| 146 } |
| 147 |
| 148 bool GetStringFromJS(content::RenderViewHost* rvh, |
| 149 const std::string& script, |
| 150 std::string* result) WARN_UNUSED_RESULT { |
| 151 return content::ExecuteJavaScriptAndExtractString( |
| 152 rvh, std::wstring(), WrapScript(script), result); |
| 153 } |
| 154 |
| 155 bool ExecuteScript(const std::string& script) WARN_UNUSED_RESULT { |
| 156 return content::ExecuteJavaScript( |
| 157 instant()->GetPreviewContents()->GetRenderViewHost(), std::wstring(), |
| 158 UTF8ToWide(script)); |
| 159 } |
| 160 |
| 161 bool CheckVisibilityIs(content::WebContents* contents, |
| 162 bool expected) WARN_UNUSED_RESULT { |
| 163 bool actual = !expected; // Purposely start with a mis-match. |
| 164 // We can only use ASSERT_*() in a method that returns void, hence this |
| 165 // convoluted check. |
| 166 return GetBoolFromJS(contents->GetRenderViewHost(), |
| 167 "!document.webkitHidden", &actual) && |
| 168 actual == expected; |
| 169 } |
| 170 |
| 171 GURL instant_url_; |
| 172 }; |
| 173 |
| 174 #endif // CHROME_BROWSER_INSTANT_TEST_UTILS_H_ |
OLD | NEW |