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_base.h" |
6 | |
7 #include <stddef.h> | |
8 | 6 |
9 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/chrome_notification_types.h" | |
12 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/search_engines/template_url_service_factory.h" | 10 #include "chrome/browser/search_engines/template_url_service_factory.h" |
14 #include "chrome/browser/ui/search/search_tab_helper.h" | 11 #include "chrome/browser/ui/browser.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" | 12 #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" | 13 #include "components/search_engines/template_url_service.h" |
21 #include "content/public/browser/notification_service.h" | |
22 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
23 #include "content/public/test/browser_test_utils.h" | |
24 | |
25 namespace { | |
26 | |
27 std::string WrapScript(const std::string& script) { | |
28 return "domAutomationController.send(" + script + ")"; | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 // InstantTestBase ----------------------------------------------------------- | |
34 | 15 |
35 InstantTestBase::InstantTestBase() | 16 InstantTestBase::InstantTestBase() |
36 : https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS), | 17 : browser_(nullptr), |
| 18 https_test_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
37 init_suggestions_url_(false) { | 19 init_suggestions_url_(false) { |
38 https_test_server_.ServeFilesFromSourceDirectory("chrome/test/data"); | 20 https_test_server_.ServeFilesFromSourceDirectory("chrome/test/data"); |
39 } | 21 } |
40 | 22 |
41 InstantTestBase::~InstantTestBase() {} | 23 InstantTestBase::~InstantTestBase() {} |
42 | 24 |
43 void InstantTestBase::SetupInstant(Browser* browser) { | 25 void InstantTestBase::SetupInstant(Browser* browser) { |
44 browser_ = browser; | 26 browser_ = browser; |
45 | 27 |
46 TemplateURLService* service = | 28 TemplateURLService* service = |
(...skipping 16 matching lines...) Expand all Loading... |
63 service->SetUserSelectedDefaultSearchProvider(template_url); | 45 service->SetUserSelectedDefaultSearchProvider(template_url); |
64 } | 46 } |
65 | 47 |
66 void InstantTestBase::Init(const GURL& instant_url, | 48 void InstantTestBase::Init(const GURL& instant_url, |
67 const GURL& ntp_url, | 49 const GURL& ntp_url, |
68 bool init_suggestions_url) { | 50 bool init_suggestions_url) { |
69 instant_url_ = instant_url; | 51 instant_url_ = instant_url; |
70 ntp_url_ = ntp_url; | 52 ntp_url_ = ntp_url; |
71 init_suggestions_url_ = init_suggestions_url; | 53 init_suggestions_url_ = init_suggestions_url; |
72 } | 54 } |
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), | |
111 result); | |
112 } | |
113 | |
114 bool InstantTestBase::GetIntFromJS(const content::ToRenderFrameHost& adapter, | |
115 const std::string& script, | |
116 int* result) { | |
117 return content::ExecuteScriptAndExtractInt(adapter, WrapScript(script), | |
118 result); | |
119 } | |
120 | |
121 bool InstantTestBase::GetDoubleFromJS(const content::ToRenderFrameHost& adapter, | |
122 const std::string& script, | |
123 double* result) { | |
124 return content::ExecuteScriptAndExtractDouble(adapter, WrapScript(script), | |
125 result); | |
126 } | |
127 | |
128 bool InstantTestBase::GetStringFromJS(const content::ToRenderFrameHost& adapter, | |
129 const std::string& script, | |
130 std::string* result) { | |
131 return content::ExecuteScriptAndExtractString(adapter, WrapScript(script), | |
132 result); | |
133 } | |
134 | |
135 std::string InstantTestBase::GetOmniboxText() { | |
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 |