| 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 <stddef.h> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "chrome/browser/chrome_notification_types.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 14 #include "chrome/browser/ui/search/search_tab_helper.h" | |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 16 #include "chrome/test/base/interactive_test_utils.h" | |
| 17 #include "chrome/test/base/search_test_utils.h" | |
| 18 #include "components/omnibox/browser/omnibox_edit_model.h" | |
| 19 #include "components/omnibox/browser/omnibox_view.h" | |
| 20 #include "components/search_engines/template_url_service.h" | |
| 21 #include "content/public/browser/notification_service.h" | |
| 22 #include "content/public/browser/web_contents.h" | |
| 23 #include "content/public/test/browser_test_utils.h" | |
| 24 | |
| 25 namespace { | 7 namespace { |
| 26 | 8 |
| 27 std::string WrapScript(const std::string& script) { | 9 std::string WrapScript(const std::string& script) { |
| 28 return "domAutomationController.send(" + script + ")"; | 10 return "domAutomationController.send(" + script + ")"; |
| 29 } | 11 } |
| 30 | 12 |
| 31 } // namespace | 13 } // namespace |
| 32 | 14 |
| 33 // InstantTestBase ----------------------------------------------------------- | 15 namespace instant_test_utils { |
| 34 | 16 |
| 35 InstantTestBase::InstantTestBase() | 17 bool GetBoolFromJS(const content::ToRenderFrameHost& adapter, |
| 36 : https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS), | 18 const std::string& script, |
| 37 init_suggestions_url_(false) { | 19 bool* result) { |
| 38 https_test_server_.ServeFilesFromSourceDirectory("chrome/test/data"); | |
| 39 } | |
| 40 | |
| 41 InstantTestBase::~InstantTestBase() {} | |
| 42 | |
| 43 void InstantTestBase::SetupInstant(Browser* browser) { | |
| 44 browser_ = browser; | |
| 45 | |
| 46 TemplateURLService* service = | |
| 47 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | |
| 48 search_test_utils::WaitForTemplateURLServiceToLoad(service); | |
| 49 | |
| 50 TemplateURLData data; | |
| 51 // Necessary to use exact URL for both the main URL and the alternate URL for | |
| 52 // search term extraction to work in InstantExtended. | |
| 53 data.SetShortName(base::ASCIIToUTF16("name")); | |
| 54 data.SetURL(instant_url_.spec() + "q={searchTerms}&is_search"); | |
| 55 data.instant_url = instant_url_.spec(); | |
| 56 data.new_tab_url = ntp_url_.spec(); | |
| 57 if (init_suggestions_url_) | |
| 58 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}"; | |
| 59 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | |
| 60 data.search_terms_replacement_key = "strk"; | |
| 61 | |
| 62 TemplateURL* template_url = service->Add(base::MakeUnique<TemplateURL>(data)); | |
| 63 service->SetUserSelectedDefaultSearchProvider(template_url); | |
| 64 } | |
| 65 | |
| 66 void InstantTestBase::Init(const GURL& instant_url, | |
| 67 const GURL& ntp_url, | |
| 68 bool init_suggestions_url) { | |
| 69 instant_url_ = instant_url; | |
| 70 ntp_url_ = ntp_url; | |
| 71 init_suggestions_url_ = init_suggestions_url; | |
| 72 } | |
| 73 | |
| 74 void InstantTestBase::FocusOmnibox() { | |
| 75 // If the omnibox already has focus, just notify SearchTabHelper. | |
| 76 if (omnibox()->model()->has_focus()) { | |
| 77 content::WebContents* active_tab = | |
| 78 browser_->tab_strip_model()->GetActiveWebContents(); | |
| 79 SearchTabHelper::FromWebContents(active_tab)->OmniboxFocusChanged( | |
| 80 OMNIBOX_FOCUS_VISIBLE, OMNIBOX_FOCUS_CHANGE_EXPLICIT); | |
| 81 } else { | |
| 82 browser_->window()->GetLocationBar()->FocusLocation(false); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 void InstantTestBase::SetOmniboxText(const std::string& text) { | |
| 87 FocusOmnibox(); | |
| 88 omnibox()->SetUserText(base::UTF8ToUTF16(text)); | |
| 89 } | |
| 90 | |
| 91 void InstantTestBase::PressEnterAndWaitForNavigation() { | |
| 92 content::WindowedNotificationObserver nav_observer( | |
| 93 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 94 content::NotificationService::AllSources()); | |
| 95 browser_->window()->GetLocationBar()->AcceptInput(); | |
| 96 nav_observer.Wait(); | |
| 97 } | |
| 98 | |
| 99 void InstantTestBase::PressEnterAndWaitForFrameLoad() { | |
| 100 content::WindowedNotificationObserver nav_observer( | |
| 101 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | |
| 102 content::NotificationService::AllSources()); | |
| 103 browser_->window()->GetLocationBar()->AcceptInput(); | |
| 104 nav_observer.Wait(); | |
| 105 } | |
| 106 | |
| 107 bool InstantTestBase::GetBoolFromJS(const content::ToRenderFrameHost& adapter, | |
| 108 const std::string& script, | |
| 109 bool* result) { | |
| 110 return content::ExecuteScriptAndExtractBool(adapter, WrapScript(script), | 20 return content::ExecuteScriptAndExtractBool(adapter, WrapScript(script), |
| 111 result); | 21 result); |
| 112 } | 22 } |
| 113 | 23 |
| 114 bool InstantTestBase::GetIntFromJS(const content::ToRenderFrameHost& adapter, | 24 bool GetIntFromJS(const content::ToRenderFrameHost& adapter, |
| 115 const std::string& script, | 25 const std::string& script, |
| 116 int* result) { | 26 int* result) { |
| 117 return content::ExecuteScriptAndExtractInt(adapter, WrapScript(script), | 27 return content::ExecuteScriptAndExtractInt(adapter, WrapScript(script), |
| 118 result); | 28 result); |
| 119 } | 29 } |
| 120 | 30 |
| 121 bool InstantTestBase::GetDoubleFromJS(const content::ToRenderFrameHost& adapter, | 31 bool GetDoubleFromJS(const content::ToRenderFrameHost& adapter, |
| 122 const std::string& script, | 32 const std::string& script, |
| 123 double* result) { | 33 double* result) { |
| 124 return content::ExecuteScriptAndExtractDouble(adapter, WrapScript(script), | 34 return content::ExecuteScriptAndExtractDouble(adapter, WrapScript(script), |
| 125 result); | 35 result); |
| 126 } | 36 } |
| 127 | 37 |
| 128 bool InstantTestBase::GetStringFromJS(const content::ToRenderFrameHost& adapter, | 38 bool GetStringFromJS(const content::ToRenderFrameHost& adapter, |
| 129 const std::string& script, | 39 const std::string& script, |
| 130 std::string* result) { | 40 std::string* result) { |
| 131 return content::ExecuteScriptAndExtractString(adapter, WrapScript(script), | 41 return content::ExecuteScriptAndExtractString(adapter, WrapScript(script), |
| 132 result); | 42 result); |
| 133 } | 43 } |
| 134 | 44 |
| 135 std::string InstantTestBase::GetOmniboxText() { | 45 } // namespace instant_test_utils |
| 136 return base::UTF16ToUTF8(omnibox()->GetText()); | |
| 137 } | |
| 138 | |
| 139 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh, | |
| 140 const std::string& image, | |
| 141 bool* loaded) { | |
| 142 std::string js_chrome = | |
| 143 "var img = document.createElement('img');" | |
| 144 "img.onerror = function() { domAutomationController.send(false); };" | |
| 145 "img.onload = function() { domAutomationController.send(true); };" | |
| 146 "img.src = '" + image + "';"; | |
| 147 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); | |
| 148 } | |
| OLD | NEW |