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

Side by Side Diff: chrome/browser/ui/webui/web_ui_test_handler.cc

Issue 7576024: Provide ability for WebUIBrowserTests to run asynchronous tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments, fixed chrome.send passthrough, fixed assertion tests to use runTestFunction. Created 9 years, 4 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
« no previous file with comments | « chrome/browser/ui/webui/web_ui_test_handler.h ('k') | chrome/test/data/webui/assertions.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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/webui/web_ui_test_handler.h" 5 #include "chrome/browser/ui/webui/web_ui_test_handler.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/common/render_messages.h" 9 #include "chrome/common/render_messages.h"
10 #include "chrome/test/base/ui_test_utils.h" 10 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/browser/renderer_host/render_view_host.h" 11 #include "content/browser/renderer_host/render_view_host.h"
12 #include "content/browser/tab_contents/tab_contents.h" 12 #include "content/browser/tab_contents/tab_contents.h"
13 #include "content/common/notification_details.h" 13 #include "content/common/notification_details.h"
14 #include "content/common/notification_registrar.h" 14 #include "content/common/notification_registrar.h"
15 15
16 WebUITestHandler::WebUITestHandler()
17 : test_done_(false),
18 test_succeeded_(false),
19 run_test_done_(false),
20 run_test_succeeded_(false),
21 is_waiting_(false) {
22 }
23
16 void WebUITestHandler::PreloadJavaScript(const string16& js_text, 24 void WebUITestHandler::PreloadJavaScript(const string16& js_text,
17 RenderViewHost* preload_host) { 25 RenderViewHost* preload_host) {
18 DCHECK(preload_host); 26 DCHECK(preload_host);
19 preload_host->Send(new ViewMsg_WebUIJavaScript( 27 preload_host->Send(new ViewMsg_WebUIJavaScript(
20 preload_host->routing_id(), string16(), js_text, 0, 28 preload_host->routing_id(), string16(), js_text, 0,
21 false)); 29 false));
22 } 30 }
23 31
24 void WebUITestHandler::RunJavaScript(const string16& js_text) { 32 void WebUITestHandler::RunJavaScript(const string16& js_text) {
25 web_ui_->tab_contents()->render_view_host()->ExecuteJavascriptInWebFrame( 33 web_ui_->tab_contents()->render_view_host()->ExecuteJavascriptInWebFrame(
26 string16(), js_text); 34 string16(), js_text);
27 } 35 }
28 36
29 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { 37 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) {
38 test_succeeded_ = false;
39 run_test_succeeded_ = false;
30 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); 40 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host();
31 NotificationRegistrar notification_registrar; 41 NotificationRegistrar notification_registrar;
32 notification_registrar.Add( 42 notification_registrar.Add(
33 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, 43 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT,
34 Source<RenderViewHost>(rvh)); 44 Source<RenderViewHost>(rvh));
35 rvh->ExecuteJavascriptInWebFrameNotifyResult(string16(), js_text); 45 rvh->ExecuteJavascriptInWebFrameNotifyResult(string16(), js_text);
36 return WaitForResult(); 46 return WaitForResult();
37 } 47 }
38 48
49 void WebUITestHandler::RegisterMessages() {
50 web_ui_->RegisterMessageCallback("testResult", NewCallback(
51 this, &WebUITestHandler::HandleTestResult));
52 }
53
54 void WebUITestHandler::HandleTestResult(const ListValue* test_result) {
55 // Quit the message loop if |is_waiting_| so waiting process can get result or
56 // error. To ensure this gets done, do this before ASSERT* calls.
57 if (is_waiting_)
58 MessageLoopForUI::current()->Quit();
59
60 SCOPED_TRACE("WebUITestHandler::HandleTestResult");
61
62 EXPECT_FALSE(test_done_);
63 test_done_ = true;
64 test_succeeded_ = false;
65
66 ASSERT_TRUE(test_result->GetBoolean(0, &test_succeeded_));
67 if (!test_succeeded_) {
68 std::string message;
69 ASSERT_TRUE(test_result->GetString(1, &message));
70 LOG(ERROR) << message;
71 }
72 }
73
39 void WebUITestHandler::Observe(int type, 74 void WebUITestHandler::Observe(int type,
40 const NotificationSource& source, 75 const NotificationSource& source,
41 const NotificationDetails& details) { 76 const NotificationDetails& details) {
42 // Quit the message loop if we were waiting so Waiting process can get result 77 // Quit the message loop if |is_waiting_| so waiting process can get result or
43 // or error. To ensure this gets done, do this before ASSERT* calls. 78 // error. To ensure this gets done, do this before ASSERT* calls.
44 if (is_waiting_) 79 if (is_waiting_)
45 MessageLoopForUI::current()->Quit(); 80 MessageLoopForUI::current()->Quit();
46 81
82 ASSERT_EQ(content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, type);
83
47 SCOPED_TRACE("WebUITestHandler::Observe"); 84 SCOPED_TRACE("WebUITestHandler::Observe");
85
86 EXPECT_FALSE(run_test_done_);
87 run_test_done_ = true;
88 run_test_succeeded_ = false;
89
48 Value* value = Details<std::pair<int, Value*> >(details)->second; 90 Value* value = Details<std::pair<int, Value*> >(details)->second;
49 ListValue* list_value; 91 ASSERT_TRUE(value->GetAsBoolean(&run_test_succeeded_));
50 ASSERT_TRUE(value->GetAsList(&list_value));
51 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_));
52 if (!test_succeeded_) {
53 std::string message;
54 ASSERT_TRUE(list_value->GetString(1, &message));
55 LOG(ERROR) << message;
56 }
57 } 92 }
58 93
59 bool WebUITestHandler::WaitForResult() { 94 bool WebUITestHandler::WaitForResult() {
95 SCOPED_TRACE("WebUITestHandler::WaitForResult");
96 test_done_ = false;
97 run_test_done_ = false;
60 is_waiting_ = true; 98 is_waiting_ = true;
99
100 // Either sync test completion or the testDone() will cause message loop
101 // to quit.
61 ui_test_utils::RunMessageLoop(); 102 ui_test_utils::RunMessageLoop();
103
104 // Run a second message loop when not |run_test_done_| so that the sync test
105 // completes, or |run_test_succeeded_| but not |test_done_| so async tests
106 // complete.
107 if (!run_test_done_ || (run_test_succeeded_ && !test_done_)) {
108 ui_test_utils::RunMessageLoop();
109 }
110
62 is_waiting_ = false; 111 is_waiting_ = false;
63 return test_succeeded_; 112
113 // To succeed the test must execute as well as pass the test.
114 return run_test_succeeded_ && test_succeeded_;
64 } 115 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/web_ui_test_handler.h ('k') | chrome/test/data/webui/assertions.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698