Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: chrome/browser/debugger/devtools_sanity_unittest.cc

Issue 7274031: Wholesale move of debugger sources to content. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add OWNERS and DEPS. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "base/command_line.h"
6 #include "base/path_service.h"
7 #include "base/stringprintf.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/debugger/devtools_client_host.h"
10 #include "chrome/browser/debugger/devtools_manager.h"
11 #include "chrome/browser/debugger/devtools_window.h"
12 #include "chrome/browser/extensions/extension_host.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/test/in_process_browser_test.h"
18 #include "chrome/test/ui_test_utils.h"
19 #include "content/browser/renderer_host/render_view_host.h"
20 #include "content/browser/tab_contents/tab_contents.h"
21 #include "content/common/notification_registrar.h"
22 #include "content/common/notification_service.h"
23 #include "net/test/test_server.h"
24
25 namespace {
26
27 // Used to block until a dev tools client window's browser is closed.
28 class BrowserClosedObserver : public NotificationObserver {
29 public:
30 explicit BrowserClosedObserver(Browser* browser) {
31 registrar_.Add(this, NotificationType::BROWSER_CLOSED,
32 Source<Browser>(browser));
33 ui_test_utils::RunMessageLoop();
34 }
35
36 virtual void Observe(NotificationType type,
37 const NotificationSource& source,
38 const NotificationDetails& details) {
39 MessageLoopForUI::current()->Quit();
40 }
41
42 private:
43 NotificationRegistrar registrar_;
44 DISALLOW_COPY_AND_ASSIGN(BrowserClosedObserver);
45 };
46
47 // The delay waited in some cases where we don't have a notifications for an
48 // action we take.
49 const int kActionDelayMs = 500;
50
51 const char kDebuggerTestPage[] = "files/devtools/debugger_test_page.html";
52 const char kPauseWhenLoadingDevTools[] =
53 "files/devtools/pause_when_loading_devtools.html";
54 const char kPauseWhenScriptIsRunning[] =
55 "files/devtools/pause_when_script_is_running.html";
56 const char kPageWithContentScript[] =
57 "files/devtools/page_with_content_script.html";
58 const char kChunkedTestPage[] = "chunked";
59 const char kSlowTestPage[] =
60 "chunked?waitBeforeHeaders=100&waitBetweenChunks=100&chunksNumber=2";
61
62
63 class DevToolsSanityTest : public InProcessBrowserTest {
64 public:
65 DevToolsSanityTest() {
66 set_show_window(true);
67 EnableDOMAutomation();
68 }
69
70 protected:
71 void RunTest(const std::string& test_name, const std::string& test_page) {
72 OpenDevToolsWindow(test_page);
73 std::string result;
74
75 // At first check that JavaScript part of the front-end is loaded by
76 // checking that global variable uiTests exists(it's created after all js
77 // files have been loaded) and has runTest method.
78 ASSERT_TRUE(
79 ui_test_utils::ExecuteJavaScriptAndExtractString(
80 client_contents_->render_view_host(),
81 L"",
82 L"window.domAutomationController.send("
83 L"'' + (window.uiTests && (typeof uiTests.runTest)));",
84 &result));
85
86 if (result == "function") {
87 ASSERT_TRUE(
88 ui_test_utils::ExecuteJavaScriptAndExtractString(
89 client_contents_->render_view_host(),
90 L"",
91 UTF8ToWide(base::StringPrintf("uiTests.runTest('%s')",
92 test_name.c_str())),
93 &result));
94 EXPECT_EQ("[OK]", result);
95 } else {
96 FAIL() << "DevTools front-end is broken.";
97 }
98 CloseDevToolsWindow();
99 }
100
101 void OpenDevToolsWindow(const std::string& test_page) {
102 ASSERT_TRUE(test_server()->Start());
103 GURL url = test_server()->GetURL(test_page);
104 ui_test_utils::NavigateToURL(browser(), url);
105
106 inspected_rvh_ = GetInspectedTab()->render_view_host();
107 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
108 devtools_manager->OpenDevToolsWindow(inspected_rvh_);
109
110 DevToolsClientHost* client_host =
111 devtools_manager->GetDevToolsClientHostFor(inspected_rvh_);
112 window_ = client_host->AsDevToolsWindow();
113 RenderViewHost* client_rvh = window_->GetRenderViewHost();
114 client_contents_ = client_rvh->delegate()->GetAsTabContents();
115 ui_test_utils::WaitForNavigation(&client_contents_->controller());
116 }
117
118 TabContents* GetInspectedTab() {
119 return browser()->GetTabContentsAt(0);
120 }
121
122 void CloseDevToolsWindow() {
123 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
124 // UnregisterDevToolsClientHostFor may destroy window_ so store the browser
125 // first.
126 Browser* browser = window_->browser();
127 devtools_manager->UnregisterDevToolsClientHostFor(inspected_rvh_);
128
129 // Wait only when DevToolsWindow has a browser. For docked DevTools, this
130 // is NULL and we skip the wait.
131 if (browser)
132 BrowserClosedObserver close_observer(browser);
133 }
134
135 TabContents* client_contents_;
136 DevToolsWindow* window_;
137 RenderViewHost* inspected_rvh_;
138 };
139
140
141 class CancelableQuitTask : public Task {
142 public:
143 explicit CancelableQuitTask(const std::string& timeout_message)
144 : timeout_message_(timeout_message),
145 cancelled_(false) {
146 }
147
148 void cancel() {
149 cancelled_ = true;
150 }
151
152 virtual void Run() {
153 if (cancelled_) {
154 return;
155 }
156 FAIL() << timeout_message_;
157 MessageLoop::current()->Quit();
158 }
159
160 private:
161 std::string timeout_message_;
162 bool cancelled_;
163 };
164
165
166 // Base class for DevTools tests that test devtools functionality for
167 // extensions and content scripts.
168 class DevToolsExtensionDebugTest : public DevToolsSanityTest,
169 public NotificationObserver {
170 public:
171 DevToolsExtensionDebugTest() : DevToolsSanityTest() {
172 PathService::Get(chrome::DIR_TEST_DATA, &test_extensions_dir_);
173 test_extensions_dir_ = test_extensions_dir_.AppendASCII("devtools");
174 test_extensions_dir_ = test_extensions_dir_.AppendASCII("extensions");
175 }
176
177 protected:
178 // Load an extention from test\data\devtools\extensions\<extension_name>
179 void LoadExtension(const char* extension_name) {
180 FilePath path = test_extensions_dir_.AppendASCII(extension_name);
181 ASSERT_TRUE(LoadExtensionFromPath(path)) << "Failed to load extension.";
182 }
183
184 private:
185 bool LoadExtensionFromPath(const FilePath& path) {
186 ExtensionService* service = browser()->profile()->GetExtensionService();
187 size_t num_before = service->extensions()->size();
188 {
189 NotificationRegistrar registrar;
190 registrar.Add(this, NotificationType::EXTENSION_LOADED,
191 NotificationService::AllSources());
192 CancelableQuitTask* delayed_quit =
193 new CancelableQuitTask("Extension load timed out.");
194 MessageLoop::current()->PostDelayedTask(FROM_HERE, delayed_quit,
195 4*1000);
196 service->LoadExtension(path);
197 ui_test_utils::RunMessageLoop();
198 delayed_quit->cancel();
199 }
200 size_t num_after = service->extensions()->size();
201 if (num_after != (num_before + 1))
202 return false;
203
204 return WaitForExtensionHostsToLoad();
205 }
206
207 bool WaitForExtensionHostsToLoad() {
208 // Wait for all the extension hosts that exist to finish loading.
209 // NOTE: This assumes that the extension host list is not changing while
210 // this method is running.
211
212 NotificationRegistrar registrar;
213 registrar.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING,
214 NotificationService::AllSources());
215 CancelableQuitTask* delayed_quit =
216 new CancelableQuitTask("Extension host load timed out.");
217 MessageLoop::current()->PostDelayedTask(FROM_HERE, delayed_quit,
218 4*1000);
219
220 ExtensionProcessManager* manager =
221 browser()->profile()->GetExtensionProcessManager();
222 for (ExtensionProcessManager::const_iterator iter = manager->begin();
223 iter != manager->end();) {
224 if ((*iter)->did_stop_loading())
225 ++iter;
226 else
227 ui_test_utils::RunMessageLoop();
228 }
229
230 delayed_quit->cancel();
231 return true;
232 }
233
234 void Observe(NotificationType type,
235 const NotificationSource& source,
236 const NotificationDetails& details) {
237 switch (type.value) {
238 case NotificationType::EXTENSION_LOADED:
239 case NotificationType::EXTENSION_HOST_DID_STOP_LOADING:
240 MessageLoopForUI::current()->Quit();
241 break;
242 default:
243 NOTREACHED();
244 break;
245 }
246 }
247
248 FilePath test_extensions_dir_;
249 };
250
251 // Tests scripts panel showing.
252 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, TestShowScriptsTab) {
253 RunTest("testShowScriptsTab", kDebuggerTestPage);
254 }
255
256 // Tests that scripts tab is populated with inspected scripts even if it
257 // hadn't been shown by the moment inspected paged refreshed.
258 // @see http://crbug.com/26312
259 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest,
260 TestScriptsTabIsPopulatedOnInspectedPageRefresh) {
261 // Clear inspector settings to ensure that Elements will be
262 // current panel when DevTools window is open.
263 GetInspectedTab()->render_view_host()->delegate()->ClearInspectorSettings();
264 RunTest("testScriptsTabIsPopulatedOnInspectedPageRefresh",
265 kDebuggerTestPage);
266 }
267
268 // Tests that a content script is in the scripts list.
269 // This test is disabled, see bug 28961.
270 IN_PROC_BROWSER_TEST_F(DevToolsExtensionDebugTest,
271 TestContentScriptIsPresent) {
272 LoadExtension("simple_content_script");
273 RunTest("testContentScriptIsPresent", kPageWithContentScript);
274 }
275
276 // Tests that scripts are not duplicated after Scripts Panel switch.
277 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest,
278 TestNoScriptDuplicatesOnPanelSwitch) {
279 RunTest("testNoScriptDuplicatesOnPanelSwitch", kDebuggerTestPage);
280 }
281
282 // Tests that debugger works correctly if pause event occurs when DevTools
283 // frontend is being loaded.
284 // Flaky - http://crbug.com/69719.
285 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, FLAKY_TestPauseWhenLoadingDevTools) {
286 RunTest("testPauseWhenLoadingDevTools", kPauseWhenLoadingDevTools);
287 }
288
289 // Tests that pressing 'Pause' will pause script execution if the script
290 // is already running.
291 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, FLAKY_TestPauseWhenScriptIsRunning) {
292 RunTest("testPauseWhenScriptIsRunning", kPauseWhenScriptIsRunning);
293 }
294
295 // Tests network timing.
296 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, TestNetworkTiming) {
297 RunTest("testNetworkTiming", kSlowTestPage);
298 }
299
300 // Tests network size.
301 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, TestNetworkSize) {
302 RunTest("testNetworkSize", kChunkedTestPage);
303 }
304
305 // Tests raw headers text.
306 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, TestNetworkSyncSize) {
307 RunTest("testNetworkSyncSize", kChunkedTestPage);
308 }
309
310 // Tests raw headers text.
311 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, TestNetworkRawHeadersText) {
312 RunTest("testNetworkRawHeadersText", kChunkedTestPage);
313 }
314
315 IN_PROC_BROWSER_TEST_F(DevToolsSanityTest, TestPageWithNoJavaScript) {
316 OpenDevToolsWindow("about:blank");
317 std::string result;
318 ASSERT_TRUE(
319 ui_test_utils::ExecuteJavaScriptAndExtractString(
320 client_contents_->render_view_host(),
321 L"",
322 L"window.domAutomationController.send("
323 L"'' + (window.uiTests && (typeof uiTests.runTest)));",
324 &result));
325 ASSERT_EQ("function", result) << "DevTools front-end is broken.";
326 CloseDevToolsWindow();
327 }
328
329 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698