OLD | NEW |
---|---|
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 test_succeeded_async_(false), | |
18 is_waiting_(false), | |
19 test_done_(false), | |
20 test_done_async_(false) { | |
21 } | |
22 | |
15 void WebUITestHandler::PreloadJavaScript(const string16& js_text, | 23 void WebUITestHandler::PreloadJavaScript(const string16& js_text, |
16 RenderViewHost* preload_host) { | 24 RenderViewHost* preload_host) { |
17 DCHECK(preload_host); | 25 DCHECK(preload_host); |
18 preload_host->Send(new ViewMsg_WebUIJavaScript( | 26 preload_host->Send(new ViewMsg_WebUIJavaScript( |
19 preload_host->routing_id(), string16(), js_text, 0, | 27 preload_host->routing_id(), string16(), js_text, 0, |
20 false)); | 28 false)); |
21 } | 29 } |
22 | 30 |
23 void WebUITestHandler::RunJavaScript(const string16& js_text) { | 31 void WebUITestHandler::RunJavaScript(const string16& js_text) { |
24 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( | 32 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( |
25 string16(), js_text); | 33 string16(), js_text); |
26 } | 34 } |
27 | 35 |
28 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { | 36 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text, |
37 bool is_async) { | |
38 test_succeeded_ = false; | |
39 test_succeeded_async_ = false; | |
29 NotificationRegistrar notification_registrar; | 40 NotificationRegistrar notification_registrar; |
30 notification_registrar.Add( | 41 notification_registrar.Add( |
31 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, | 42 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, |
32 Source<RenderViewHost>(web_ui_->GetRenderViewHost())); | 43 Source<RenderViewHost>(web_ui_->GetRenderViewHost())); |
33 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult( | 44 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult( |
34 string16(), js_text); | 45 string16(), js_text); |
35 return WaitForResult(); | 46 return WaitForResult(is_async); |
47 } | |
48 | |
49 void WebUITestHandler::RegisterMessages() { | |
50 web_ui_->RegisterMessageCallback("asyncTestResult", NewCallback( | |
51 this, &WebUITestHandler::HandleAsyncTestResult)); | |
52 } | |
53 | |
54 void WebUITestHandler::HandleAsyncTestResult(const ListValue* test_result) { | |
55 // Quit the message loop if we were waiting so Waiting process can get result | |
56 // or error. To ensure this gets done, do this before ASSERT* calls. | |
57 if (is_waiting_) | |
58 MessageLoopForUI::current()->Quit(); | |
mmenke
2011/08/10 17:23:40
You could move this into HandleTestResult, since y
Sheridan Rawlins
2011/08/11 01:58:44
There are ASSERT_* calls in Observe which occur be
mmenke
2011/08/11 15:16:16
Err... Good point.
On 2011/08/11 01:58:44, Sheri
| |
59 | |
60 EXPECT_FALSE(test_done_async_); | |
61 test_done_async_ = true; | |
62 | |
63 SCOPED_TRACE("WebUITestHandler::HandleAsyncTestResult"); | |
64 HandleTestResult(test_result, &test_succeeded_async_); | |
65 } | |
66 | |
67 void WebUITestHandler::HandleTestResult(const ListValue* test_result, | |
68 bool* success) { | |
69 *success = false; | |
70 ASSERT_TRUE(test_result->GetBoolean(0, success)); | |
71 if (!success) { | |
72 std::string message; | |
73 ASSERT_TRUE(test_result->GetString(1, &message)); | |
74 LOG(ERROR) << message; | |
75 } | |
36 } | 76 } |
37 | 77 |
38 void WebUITestHandler::Observe(int type, | 78 void WebUITestHandler::Observe(int type, |
39 const NotificationSource& source, | 79 const NotificationSource& source, |
40 const NotificationDetails& details) { | 80 const NotificationDetails& details) { |
41 // Quit the message loop if we were waiting so Waiting process can get result | 81 // 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. | 82 // or error. To ensure this gets done, do this before ASSERT* calls. |
43 if (is_waiting_) | 83 if (is_waiting_) |
44 MessageLoopForUI::current()->Quit(); | 84 MessageLoopForUI::current()->Quit(); |
45 | 85 |
86 EXPECT_FALSE(test_done_); | |
87 test_done_ = true; | |
88 | |
46 SCOPED_TRACE("WebUITestHandler::Observe"); | 89 SCOPED_TRACE("WebUITestHandler::Observe"); |
47 Value* value = Details<std::pair<int, Value*> >(details)->second; | 90 Value* value = Details<std::pair<int, Value*> >(details)->second; |
48 ListValue* list_value; | 91 ListValue* test_result; |
49 ASSERT_TRUE(value->GetAsList(&list_value)); | 92 ASSERT_TRUE(value->GetAsList(&test_result)); |
50 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_)); | 93 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 } | 94 } |
57 | 95 |
58 bool WebUITestHandler::WaitForResult() { | 96 bool WebUITestHandler::WaitForResult(bool is_async) { |
97 SCOPED_TRACE("WebUITestHandler::WaitForResult"); | |
98 test_done_ = false; | |
99 test_done_async_ = false; | |
59 is_waiting_ = true; | 100 is_waiting_ = true; |
101 | |
102 // Either sync test completion or the asyncTestDone() will cause message loop | |
103 // to quit. | |
60 ui_test_utils::RunMessageLoop(); | 104 ui_test_utils::RunMessageLoop(); |
105 | |
106 // When the test |is_async|, run a second message loop when |test_done_async_| | |
107 // so that the sync test completes, or |test_succeeded_| so the async test | |
108 // completes. | |
mmenke
2011/08/10 17:23:40
This sentence isn't quite right. Maybe:
When the
Sheridan Rawlins
2011/08/11 01:58:44
Done.
| |
109 if (is_async && (!test_done_ || (test_succeeded_ && !test_done_async_))) { | |
110 ui_test_utils::RunMessageLoop(); | |
111 } | |
112 | |
113 // Verify that |test_done_async_| isn't set when !|is_async|. When |is_async| | |
114 // is true, |test_done_async_| may be false if the sync test failed first. | |
115 if (!is_async) | |
116 EXPECT_FALSE(test_done_async_); | |
117 | |
61 is_waiting_ = false; | 118 is_waiting_ = false; |
62 return test_succeeded_; | 119 |
120 // Both loops must succeed to pass. | |
mmenke
2011/08/10 17:23:40
nit: If !is_async, that's not quite true.
Sheridan Rawlins
2011/08/11 01:58:44
Done.
| |
121 return test_succeeded_ && (!is_async || test_succeeded_async_); | |
63 } | 122 } |
OLD | NEW |