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

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: Added failure and test when asyncTestDone() is called for non-async tests. 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
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/common/notification_details.h" 12 #include "content/common/notification_details.h"
13 #include "content/common/notification_registrar.h" 13 #include "content/common/notification_registrar.h"
14 14
15 WebUITestHandler::WebUITestHandler()
16 : test_succeeded_(false),
17 is_waiting_(false),
18 test_done_async_(false) {
19 }
20
15 void WebUITestHandler::PreloadJavaScript(const string16& js_text, 21 void WebUITestHandler::PreloadJavaScript(const string16& js_text,
16 RenderViewHost* preload_host) { 22 RenderViewHost* preload_host) {
17 DCHECK(preload_host); 23 DCHECK(preload_host);
18 preload_host->Send(new ViewMsg_WebUIJavaScript( 24 preload_host->Send(new ViewMsg_WebUIJavaScript(
19 preload_host->routing_id(), string16(), js_text, 0, 25 preload_host->routing_id(), string16(), js_text, 0,
20 false)); 26 false));
21 } 27 }
22 28
23 void WebUITestHandler::RunJavaScript(const string16& js_text) { 29 void WebUITestHandler::RunJavaScript(const string16& js_text) {
24 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( 30 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
25 string16(), js_text); 31 string16(), js_text);
26 } 32 }
27 33
28 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { 34 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text,
35 bool is_async) {
36 test_succeeded_ = false;
29 NotificationRegistrar notification_registrar; 37 NotificationRegistrar notification_registrar;
30 notification_registrar.Add( 38 notification_registrar.Add(
31 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, 39 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT,
32 Source<RenderViewHost>(web_ui_->GetRenderViewHost())); 40 Source<RenderViewHost>(web_ui_->GetRenderViewHost()));
33 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult( 41 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult(
34 string16(), js_text); 42 string16(), js_text);
35 return WaitForResult(); 43 return WaitForResult(is_async);
44 }
45
46 void WebUITestHandler::RegisterMessages() {
47 web_ui_->RegisterMessageCallback("asyncTestResult", NewCallback(
48 this, &WebUITestHandler::HandleAsyncTestResult));
49 }
50
51 void WebUITestHandler::HandleAsyncTestResult(const ListValue* test_result) {
52 // Quit the message loop if we were waiting so Waiting process can get result
53 // or error. To ensure this gets done, do this before ASSERT* calls.
54 if (is_waiting_)
55 MessageLoopForUI::current()->Quit();
56
57 EXPECT_FALSE(test_done_async_);
58 test_done_async_ = true;
59
60 SCOPED_TRACE("WebUITestHandler::HandleAsyncTestResult");
61 HandleTestResult(test_result, &test_succeeded_);
mmenke 2011/08/08 16:38:25 What if an async failure is sent before a successf
Sheridan Rawlins 2011/08/10 01:58:53 Both quit the loop, so both cannot be received. If
mmenke 2011/08/10 04:17:53 The loop does not quit instantly. It may handle c
Sheridan Rawlins 2011/08/10 05:41:06 Yup. Had to add another boolean back in... Take
62 }
63
64 void WebUITestHandler::HandleTestResult(const ListValue* test_result,
65 bool* success) {
66 *success = false;
67 ASSERT_TRUE(test_result->GetBoolean(0, success));
68 if (!success) {
69 std::string message;
70 ASSERT_TRUE(test_result->GetString(1, &message));
71 LOG(ERROR) << message;
72 }
36 } 73 }
37 74
38 void WebUITestHandler::Observe(int type, 75 void WebUITestHandler::Observe(int type,
39 const NotificationSource& source, 76 const NotificationSource& source,
40 const NotificationDetails& details) { 77 const NotificationDetails& details) {
41 // Quit the message loop if we were waiting so Waiting process can get result 78 // Quit the message loop if we were waiting so Waiting process can get result
42 // or error. To ensure this gets done, do this before ASSERT* calls. 79 // or error. To ensure this gets done, do this before ASSERT* calls.
43 if (is_waiting_) 80 if (is_waiting_)
44 MessageLoopForUI::current()->Quit(); 81 MessageLoopForUI::current()->Quit();
45 82
46 SCOPED_TRACE("WebUITestHandler::Observe"); 83 SCOPED_TRACE("WebUITestHandler::Observe");
47 Value* value = Details<std::pair<int, Value*> >(details)->second; 84 Value* value = Details<std::pair<int, Value*> >(details)->second;
48 ListValue* list_value; 85 ListValue* test_result;
49 ASSERT_TRUE(value->GetAsList(&list_value)); 86 ASSERT_TRUE(value->GetAsList(&test_result));
50 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_)); 87 HandleTestResult(test_result, &test_succeeded_);
51 if (!test_succeeded_) {
52 std::string message;
53 ASSERT_TRUE(list_value->GetString(1, &message));
54 LOG(ERROR) << message;
55 }
56 } 88 }
57 89
58 bool WebUITestHandler::WaitForResult() { 90 bool WebUITestHandler::WaitForResult(bool is_async) {
91 SCOPED_TRACE("WebUITestHandler::WaitForResult");
92 test_done_async_ = false;
59 is_waiting_ = true; 93 is_waiting_ = true;
60 ui_test_utils::RunMessageLoop(); 94 ui_test_utils::RunMessageLoop();
95 if (test_succeeded_ && is_async && !test_done_async_) {
Sheridan Rawlins 2011/08/10 01:58:53 I think this should be (is_async && (test_don
96 test_succeeded_ = false;
97 ui_test_utils::RunMessageLoop();
98 }
99 EXPECT_EQ(is_async, test_done_async_);
61 is_waiting_ = false; 100 is_waiting_ = false;
62 return test_succeeded_; 101 return test_succeeded_;
63 } 102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698