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