OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/instant/instant_test_utils.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/interactive_test_utils.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" |
| 17 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/result_codes.h" |
| 21 #include "content/public/test/browser_test_utils.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 std::string WrapScript(const std::string& script) { |
| 26 return "domAutomationController.send(" + script + ")"; |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 // InstantTestModelObserver -------------------------------------------------- |
| 32 |
| 33 InstantTestModelObserver::InstantTestModelObserver( |
| 34 const InstantModel* model, |
| 35 chrome::search::Mode::Type desired_mode_type) |
| 36 : model_(model), |
| 37 desired_mode_type_(desired_mode_type) { |
| 38 model_->AddObserver(this); |
| 39 } |
| 40 |
| 41 InstantTestModelObserver::~InstantTestModelObserver() { |
| 42 model_->RemoveObserver(this); |
| 43 } |
| 44 |
| 45 void InstantTestModelObserver::WaitUntilDesiredPreviewState() { |
| 46 run_loop_.Run(); |
| 47 } |
| 48 |
| 49 void InstantTestModelObserver::PreviewStateChanged(const InstantModel& model) { |
| 50 if (model.mode().mode == desired_mode_type_) |
| 51 run_loop_.Quit(); |
| 52 } |
| 53 |
| 54 // InstantTestBase ----------------------------------------------------------- |
| 55 |
| 56 void InstantTestBase::SetupInstant() { |
| 57 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 58 switches::kInstantURL, instant_url_.spec()); |
| 59 SetupInstantUsingTemplateURL(); |
| 60 } |
| 61 |
| 62 void InstantTestBase::SetupInstantUsingTemplateURL() { |
| 63 TemplateURLService* service = |
| 64 TemplateURLServiceFactory::GetForProfile(browser()->profile()); |
| 65 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
| 66 |
| 67 TemplateURLData data; |
| 68 data.SetURL("http://does/not/exist?q={searchTerms}"); |
| 69 data.instant_url = instant_url_.spec(); |
| 70 data.safe_for_autoreplace = true; |
| 71 |
| 72 TemplateURL* template_url = new TemplateURL(browser()->profile(), data); |
| 73 service->Add(template_url); // Takes ownership of |template_url|. |
| 74 service->SetDefaultSearchProvider(template_url); |
| 75 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); |
| 76 } |
| 77 |
| 78 void InstantTestBase::KillInstantRenderView() { |
| 79 base::KillProcess( |
| 80 instant()->GetPreviewContents()->GetRenderProcessHost()->GetHandle(), |
| 81 content::RESULT_CODE_KILLED, |
| 82 false); |
| 83 } |
| 84 |
| 85 void InstantTestBase::FocusOmnibox() { |
| 86 // If the omnibox already has focus, just notify Instant. |
| 87 if (omnibox()->model()->has_focus()) { |
| 88 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
| 89 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
| 90 } else { |
| 91 browser()->window()->GetLocationBar()->FocusLocation(false); |
| 92 } |
| 93 } |
| 94 |
| 95 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() { |
| 96 content::WindowedNotificationObserver observer( |
| 97 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, |
| 98 content::NotificationService::AllSources()); |
| 99 FocusOmnibox(); |
| 100 observer.Wait(); |
| 101 } |
| 102 |
| 103 void InstantTestBase::SetOmniboxText(const std::string& text) { |
| 104 FocusOmnibox(); |
| 105 omnibox()->SetUserText(UTF8ToUTF16(text)); |
| 106 } |
| 107 |
| 108 void InstantTestBase::SetOmniboxTextAndWaitForInstantToShow( |
| 109 const std::string& text) { |
| 110 InstantTestModelObserver observer( |
| 111 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); |
| 112 SetOmniboxText(text); |
| 113 observer.WaitUntilDesiredPreviewState(); |
| 114 } |
| 115 |
| 116 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, |
| 117 const std::string& script, |
| 118 bool* result) { |
| 119 return content::ExecuteScriptAndExtractBool( |
| 120 contents, WrapScript(script), result); |
| 121 } |
| 122 |
| 123 bool InstantTestBase::GetIntFromJS(content::WebContents* contents, |
| 124 const std::string& script, |
| 125 int* result) { |
| 126 return content::ExecuteScriptAndExtractInt( |
| 127 contents, WrapScript(script), result); |
| 128 } |
| 129 |
| 130 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, |
| 131 const std::string& script, |
| 132 std::string* result) { |
| 133 return content::ExecuteScriptAndExtractString( |
| 134 contents, WrapScript(script), result); |
| 135 } |
| 136 |
| 137 bool InstantTestBase::ExecuteScript(const std::string& script) { |
| 138 return content::ExecuteScript(instant()->GetPreviewContents(), script); |
| 139 } |
| 140 |
| 141 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, |
| 142 bool expected) { |
| 143 bool actual = !expected; // Purposely start with a mis-match. |
| 144 // We can only use ASSERT_*() in a method that returns void, hence this |
| 145 // convoluted check. |
| 146 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && |
| 147 actual == expected; |
| 148 } |
OLD | NEW |