OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
sreeram
2013/01/17 23:54:18
2012 -> 2013
samarth
2013/01/18 00:06:45
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_TEST_UTILS_H_ | |
6 #define CHROME_BROWSER_INSTANT_INSTANT_TEST_UTILS_H_ | |
7 | |
8 #include "base/command_line.h" | |
9 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
10 #include "chrome/browser/history/history_service_factory.h" | |
11 #include "chrome/browser/instant/instant_model_observer.h" | |
12 #include "chrome/browser/prefs/pref_service.h" | |
13 #include "chrome/browser/search_engines/template_url_service.h" | |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
15 #include "chrome/browser/task_manager/task_manager.h" | |
16 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | |
17 #include "chrome/browser/ui/browser_commands.h" | |
18 #include "chrome/browser/ui/browser_instant_controller.h" | |
19 #include "chrome/browser/ui/browser_tabstrip.h" | |
20 #include "chrome/browser/ui/browser_window.h" | |
21 #include "chrome/browser/ui/omnibox/location_bar.h" | |
22 #include "chrome/browser/ui/omnibox/omnibox_view.h" | |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
24 #include "chrome/common/chrome_notification_types.h" | |
25 #include "chrome/common/chrome_switches.h" | |
26 #include "chrome/common/pref_names.h" | |
27 #include "chrome/test/base/in_process_browser_test.h" | |
28 #include "chrome/test/base/interactive_test_utils.h" | |
29 #include "chrome/test/base/ui_test_utils.h" | |
30 #include "content/public/browser/notification_service.h" | |
31 #include "content/public/browser/render_process_host.h" | |
32 #include "content/public/browser/web_contents.h" | |
33 #include "content/public/common/result_codes.h" | |
34 #include "content/public/test/browser_test_utils.h" | |
35 | |
36 class InstantTestModelObserver : public InstantModelObserver { | |
37 public: | |
38 InstantTestModelObserver(const InstantModel* model, | |
39 chrome::search::Mode::Type desired_mode_type) | |
40 : model_(model), | |
41 desired_mode_type_(desired_mode_type) { | |
42 model_->AddObserver(this); | |
43 } | |
44 | |
45 ~InstantTestModelObserver() { | |
46 model_->RemoveObserver(this); | |
47 } | |
48 | |
49 void WaitUntilDesiredPreviewState() { | |
50 run_loop_.Run(); | |
51 } | |
52 | |
53 // Overridden from InstantModelObserver: | |
54 virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE { | |
55 if (model.mode().mode == desired_mode_type_) | |
56 run_loop_.Quit(); | |
57 } | |
58 | |
59 private: | |
60 const InstantModel* const model_; | |
61 const chrome::search::Mode::Type desired_mode_type_; | |
62 base::RunLoop run_loop_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(InstantTestModelObserver); | |
65 }; | |
66 | |
67 class InstantTestBase : public InProcessBrowserTest { | |
68 protected: | |
69 void SetupInstant() { | |
70 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
71 switches::kInstantURL, instant_url_.spec()); | |
72 SetupInstantUsingTemplateURL(); | |
73 } | |
74 | |
75 void SetupInstantUsingTemplateURL() { | |
76 TemplateURLService* service = | |
77 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | |
78 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | |
79 | |
80 TemplateURLData data; | |
81 data.SetURL("http://does/not/exist?q={searchTerms}"); | |
82 data.instant_url = instant_url_.spec(); | |
83 | |
84 TemplateURL* template_url = new TemplateURL(browser()->profile(), data); | |
85 service->Add(template_url); // Takes ownership of |template_url|. | |
86 service->SetDefaultSearchProvider(template_url); | |
87 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); | |
88 } | |
89 | |
90 InstantController* instant() { | |
91 return browser()->instant_controller()->instant(); | |
92 } | |
93 | |
94 OmniboxView* omnibox() { | |
95 return browser()->window()->GetLocationBar()->GetLocationEntry(); | |
96 } | |
97 | |
98 void KillInstantRenderView() { | |
99 base::KillProcess( | |
100 instant()->GetPreviewContents()->GetRenderProcessHost()->GetHandle(), | |
101 content::RESULT_CODE_KILLED, | |
102 false); | |
103 } | |
104 | |
105 void FocusOmnibox() { | |
106 // If the omnibox already has focus, just notify Instant. | |
107 if (omnibox()->model()->has_focus()) { | |
108 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | |
109 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | |
110 } else { | |
111 browser()->window()->GetLocationBar()->FocusLocation(false); | |
112 } | |
113 } | |
114 | |
115 void FocusOmniboxAndWaitForInstantSupport() { | |
116 content::WindowedNotificationObserver observer( | |
117 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, | |
118 content::NotificationService::AllSources()); | |
119 FocusOmnibox(); | |
120 observer.Wait(); | |
121 } | |
122 | |
123 void SetOmniboxText(const std::string& text) { | |
124 FocusOmnibox(); | |
125 omnibox()->SetUserText(UTF8ToUTF16(text)); | |
126 } | |
127 | |
128 void SetOmniboxTextAndWaitForInstantToShow(const std::string& text) { | |
129 InstantTestModelObserver observer( | |
130 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | |
131 SetOmniboxText(text); | |
132 observer.WaitUntilDesiredPreviewState(); | |
133 } | |
134 | |
135 std::string WrapScript(const std::string& script) const { | |
136 return "domAutomationController.send(" + script + ")"; | |
137 } | |
138 | |
139 bool GetBoolFromJS(content::RenderViewHost* rvh, | |
140 const std::string& script, | |
141 bool* result) WARN_UNUSED_RESULT { | |
142 return content::ExecuteScriptAndExtractBool( | |
143 rvh, WrapScript(script), result); | |
144 } | |
145 | |
146 bool GetIntFromJS(content::RenderViewHost* rvh, | |
147 const std::string& script, | |
148 int* result) WARN_UNUSED_RESULT { | |
149 return content::ExecuteScriptAndExtractInt( | |
150 rvh, WrapScript(script), result); | |
151 } | |
152 | |
153 bool GetStringFromJS(content::RenderViewHost* rvh, | |
154 const std::string& script, | |
155 std::string* result) WARN_UNUSED_RESULT { | |
156 return content::ExecuteScriptAndExtractString( | |
157 rvh, WrapScript(script), result); | |
158 } | |
159 | |
160 bool ExecuteScript(const std::string& script) WARN_UNUSED_RESULT { | |
161 return content::ExecuteScript( | |
162 instant()->GetPreviewContents()->GetRenderViewHost(), script); | |
163 } | |
164 | |
165 bool CheckVisibilityIs(content::WebContents* contents, | |
166 bool expected) WARN_UNUSED_RESULT { | |
167 bool actual = !expected; // Purposely start with a mis-match. | |
168 // We can only use ASSERT_*() in a method that returns void, hence this | |
169 // convoluted check. | |
170 return GetBoolFromJS(contents->GetRenderViewHost(), | |
171 "!document.webkitHidden", &actual) && | |
172 actual == expected; | |
173 } | |
174 | |
175 GURL instant_url_; | |
176 }; | |
177 | |
178 #endif // CHROME_BROWSER_INSTANT_INSTANT_TEST_UTILS_H_ | |
OLD | NEW |