Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / search / instant_test_utils.cc
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/ui/search/instant_test_utils.h"
6
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search_engines/template_url_service.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/ui/omnibox/omnibox_view.h"
15 #include "chrome/browser/ui/search/search_tab_helper.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/interactive_test_utils.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "components/variations/entropy_provider.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/result_codes.h"
26 #include "content/public/test/browser_test_utils.h"
27
28 namespace {
29
30 std::string WrapScript(const std::string& script) {
31   return "domAutomationController.send(" + script + ")";
32 }
33
34 }  // namespace
35
36 // InstantTestBase -----------------------------------------------------------
37
38 InstantTestBase::InstantTestBase()
39     : https_test_server_(
40           net::SpawnedTestServer::TYPE_HTTPS,
41           net::BaseTestServer::SSLOptions(),
42           base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
43       init_suggestions_url_(false) {
44 }
45
46 InstantTestBase::~InstantTestBase() {}
47
48 void InstantTestBase::SetupInstant(Browser* browser) {
49   browser_ = browser;
50
51   TemplateURLService* service =
52       TemplateURLServiceFactory::GetForProfile(browser_->profile());
53   ui_test_utils::WaitForTemplateURLServiceToLoad(service);
54
55   TemplateURLData data;
56   // Necessary to use exact URL for both the main URL and the alternate URL for
57   // search term extraction to work in InstantExtended.
58   data.short_name = base::ASCIIToUTF16("name");
59   data.SetURL(instant_url_.spec() +
60               "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}");
61   data.instant_url = instant_url_.spec();
62   data.new_tab_url = ntp_url_.spec();
63   if (init_suggestions_url_)
64     data.suggestions_url = instant_url_.spec() + "#q={searchTerms}";
65   data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
66   data.search_terms_replacement_key = "strk";
67
68   TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
69   service->Add(template_url);  // Takes ownership of |template_url|.
70   service->SetUserSelectedDefaultSearchProvider(template_url);
71 }
72
73 void InstantTestBase::SetInstantURL(const std::string& url) {
74   TemplateURLService* service =
75       TemplateURLServiceFactory::GetForProfile(browser_->profile());
76   ui_test_utils::WaitForTemplateURLServiceToLoad(service);
77
78   TemplateURLData data;
79   data.short_name = base::ASCIIToUTF16("name");
80   data.SetURL(url);
81   data.instant_url = url;
82
83   TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
84   service->Add(template_url);  // Takes ownership of |template_url|.
85   service->SetUserSelectedDefaultSearchProvider(template_url);
86 }
87
88 void InstantTestBase::Init(const GURL& instant_url,
89                            const GURL& ntp_url,
90                            bool init_suggestions_url) {
91   instant_url_ = instant_url;
92   ntp_url_ = ntp_url;
93   init_suggestions_url_ = init_suggestions_url;
94 }
95
96 void InstantTestBase::FocusOmnibox() {
97   // If the omnibox already has focus, just notify SearchTabHelper.
98   if (omnibox()->model()->has_focus()) {
99     content::WebContents* active_tab =
100         browser_->tab_strip_model()->GetActiveWebContents();
101     SearchTabHelper::FromWebContents(active_tab)->OmniboxFocusChanged(
102         OMNIBOX_FOCUS_VISIBLE, OMNIBOX_FOCUS_CHANGE_EXPLICIT);
103   } else {
104     browser_->window()->GetLocationBar()->FocusLocation(false);
105   }
106 }
107
108 void InstantTestBase::SetOmniboxText(const std::string& text) {
109   FocusOmnibox();
110   omnibox()->SetUserText(base::UTF8ToUTF16(text));
111 }
112
113 void InstantTestBase::PressEnterAndWaitForNavigation() {
114   content::WindowedNotificationObserver nav_observer(
115       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
116       content::NotificationService::AllSources());
117   browser_->window()->GetLocationBar()->AcceptInput();
118   nav_observer.Wait();
119 }
120
121 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
122                                     const std::string& script,
123                                     bool* result) {
124   return content::ExecuteScriptAndExtractBool(
125       contents, WrapScript(script), result);
126 }
127
128 bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
129                                    const std::string& script,
130                                    int* result) {
131   return content::ExecuteScriptAndExtractInt(
132       contents, WrapScript(script), result);
133 }
134
135 bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
136                                       const std::string& script,
137                                       std::string* result) {
138   return content::ExecuteScriptAndExtractString(
139       contents, WrapScript(script), result);
140 }
141
142 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
143                                         bool expected) {
144   bool actual = !expected;  // Purposely start with a mis-match.
145   // We can only use ASSERT_*() in a method that returns void, hence this
146   // convoluted check.
147   return GetBoolFromJS(contents, "!document.hidden", &actual) &&
148       actual == expected;
149 }
150
151 std::string InstantTestBase::GetOmniboxText() {
152   return base::UTF16ToUTF8(omnibox()->GetText());
153 }
154
155 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
156                                 const std::string& image,
157                                 bool* loaded) {
158   std::string js_chrome =
159       "var img = document.createElement('img');"
160       "img.onerror = function() { domAutomationController.send(false); };"
161       "img.onload  = function() { domAutomationController.send(true); };"
162       "img.src = '" + image + "';";
163   return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded);
164 }
165
166 base::string16 InstantTestBase::GetBlueText() {
167   size_t start = 0, end = 0;
168   omnibox()->GetSelectionBounds(&start, &end);
169   if (start > end)
170     std::swap(start, end);
171   return omnibox()->GetText().substr(start, end - start);
172 }