| 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> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | |
| 10 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/browser/ui/search/search_tab_helper.h" | 14 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/test/base/interactive_test_utils.h" | 16 #include "chrome/test/base/interactive_test_utils.h" |
| 19 #include "chrome/test/base/search_test_utils.h" | 17 #include "chrome/test/base/search_test_utils.h" |
| 20 #include "components/omnibox/browser/omnibox_edit_model.h" | 18 #include "components/omnibox/browser/omnibox_edit_model.h" |
| 21 #include "components/prefs/pref_service.h" | 19 #include "components/omnibox/browser/omnibox_view.h" |
| 22 #include "components/search_engines/template_url_service.h" | 20 #include "components/search_engines/template_url_service.h" |
| 23 #include "components/variations/entropy_provider.h" | |
| 24 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/render_process_host.h" | |
| 26 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/common/result_codes.h" | |
| 28 #include "content/public/test/browser_test_utils.h" | 23 #include "content/public/test/browser_test_utils.h" |
| 29 | 24 |
| 30 namespace { | 25 namespace { |
| 31 | 26 |
| 32 std::string WrapScript(const std::string& script) { | 27 std::string WrapScript(const std::string& script) { |
| 33 return "domAutomationController.send(" + script + ")"; | 28 return "domAutomationController.send(" + script + ")"; |
| 34 } | 29 } |
| 35 | 30 |
| 36 } // namespace | 31 } // namespace |
| 37 | 32 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 61 data.new_tab_url = ntp_url_.spec(); | 56 data.new_tab_url = ntp_url_.spec(); |
| 62 if (init_suggestions_url_) | 57 if (init_suggestions_url_) |
| 63 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}"; | 58 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}"; |
| 64 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | 59 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); |
| 65 data.search_terms_replacement_key = "strk"; | 60 data.search_terms_replacement_key = "strk"; |
| 66 | 61 |
| 67 TemplateURL* template_url = service->Add(base::MakeUnique<TemplateURL>(data)); | 62 TemplateURL* template_url = service->Add(base::MakeUnique<TemplateURL>(data)); |
| 68 service->SetUserSelectedDefaultSearchProvider(template_url); | 63 service->SetUserSelectedDefaultSearchProvider(template_url); |
| 69 } | 64 } |
| 70 | 65 |
| 71 void InstantTestBase::SetInstantURL(const std::string& url) { | |
| 72 TemplateURLService* service = | |
| 73 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | |
| 74 search_test_utils::WaitForTemplateURLServiceToLoad(service); | |
| 75 | |
| 76 TemplateURLData data; | |
| 77 data.SetShortName(base::ASCIIToUTF16("name")); | |
| 78 data.SetURL(url); | |
| 79 data.instant_url = url; | |
| 80 | |
| 81 TemplateURL* template_url = service->Add(base::MakeUnique<TemplateURL>(data)); | |
| 82 service->SetUserSelectedDefaultSearchProvider(template_url); | |
| 83 } | |
| 84 | |
| 85 void InstantTestBase::Init(const GURL& instant_url, | 66 void InstantTestBase::Init(const GURL& instant_url, |
| 86 const GURL& ntp_url, | 67 const GURL& ntp_url, |
| 87 bool init_suggestions_url) { | 68 bool init_suggestions_url) { |
| 88 instant_url_ = instant_url; | 69 instant_url_ = instant_url; |
| 89 ntp_url_ = ntp_url; | 70 ntp_url_ = ntp_url; |
| 90 init_suggestions_url_ = init_suggestions_url; | 71 init_suggestions_url_ = init_suggestions_url; |
| 91 } | 72 } |
| 92 | 73 |
| 93 void InstantTestBase::FocusOmnibox() { | 74 void InstantTestBase::FocusOmnibox() { |
| 94 // If the omnibox already has focus, just notify SearchTabHelper. | 75 // If the omnibox already has focus, just notify SearchTabHelper. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 contents, WrapScript(script), result); | 118 contents, WrapScript(script), result); |
| 138 } | 119 } |
| 139 | 120 |
| 140 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, | 121 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, |
| 141 const std::string& script, | 122 const std::string& script, |
| 142 std::string* result) { | 123 std::string* result) { |
| 143 return content::ExecuteScriptAndExtractString( | 124 return content::ExecuteScriptAndExtractString( |
| 144 contents, WrapScript(script), result); | 125 contents, WrapScript(script), result); |
| 145 } | 126 } |
| 146 | 127 |
| 147 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, | |
| 148 bool expected) { | |
| 149 bool actual = !expected; // Purposely start with a mis-match. | |
| 150 // We can only use ASSERT_*() in a method that returns void, hence this | |
| 151 // convoluted check. | |
| 152 return GetBoolFromJS(contents, "!document.hidden", &actual) && | |
| 153 actual == expected; | |
| 154 } | |
| 155 | |
| 156 std::string InstantTestBase::GetOmniboxText() { | 128 std::string InstantTestBase::GetOmniboxText() { |
| 157 return base::UTF16ToUTF8(omnibox()->GetText()); | 129 return base::UTF16ToUTF8(omnibox()->GetText()); |
| 158 } | 130 } |
| 159 | 131 |
| 160 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh, | 132 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh, |
| 161 const std::string& image, | 133 const std::string& image, |
| 162 bool* loaded) { | 134 bool* loaded) { |
| 163 std::string js_chrome = | 135 std::string js_chrome = |
| 164 "var img = document.createElement('img');" | 136 "var img = document.createElement('img');" |
| 165 "img.onerror = function() { domAutomationController.send(false); };" | 137 "img.onerror = function() { domAutomationController.send(false); };" |
| 166 "img.onload = function() { domAutomationController.send(true); };" | 138 "img.onload = function() { domAutomationController.send(true); };" |
| 167 "img.src = '" + image + "';"; | 139 "img.src = '" + image + "';"; |
| 168 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); | 140 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); |
| 169 } | 141 } |
| 170 | |
| 171 base::string16 InstantTestBase::GetBlueText() { | |
| 172 size_t start = 0, end = 0; | |
| 173 omnibox()->GetSelectionBounds(&start, &end); | |
| 174 if (start > end) | |
| 175 std::swap(start, end); | |
| 176 return omnibox()->GetText().substr(start, end - start); | |
| 177 } | |
| OLD | NEW |