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() | |
Paweł Hajdan Jr.
2011/08/05 18:17:14
Have you considered sharing code with extension te
Sheridan Rawlins
2011/08/05 18:51:12
David considered this some time ago. I don't reca
| |
16 : test_succeeded_(false), | |
17 is_waiting_(false), | |
18 test_done_async_(false), | |
19 test_succeeded_async_(false), | |
20 is_waiting_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) { |
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(); |
36 } | 44 } |
37 | 45 |
46 bool WebUITestHandler::WaitForAsyncResult() { | |
47 if (!test_done_async_) { | |
48 is_waiting_async_ = true; | |
49 ui_test_utils::RunMessageLoop(); | |
50 is_waiting_async_ = false; | |
51 } | |
52 return test_succeeded_async_; | |
53 }; | |
54 | |
55 void WebUITestHandler::RegisterMessages() { | |
56 web_ui_->RegisterMessageCallback("asyncTestResult", NewCallback( | |
57 this, &WebUITestHandler::HandleAsyncTestResult)); | |
58 } | |
59 | |
60 void WebUITestHandler::HandleAsyncTestResult(const ListValue* test_result) { | |
61 // Quit the message loop if we were waiting so Waiting process can get result | |
62 // or error. To ensure this gets done, do this before ASSERT* calls. | |
63 if (is_waiting_async_) | |
64 MessageLoopForUI::current()->Quit(); | |
65 | |
66 EXPECT_FALSE(test_done_async_); | |
67 test_done_async_ = true; | |
68 | |
69 SCOPED_TRACE("WebUITestHandler::HandleAsyncTestResult"); | |
70 HandleTestResult(test_result, &test_succeeded_async_); | |
71 } | |
72 | |
73 void WebUITestHandler::HandleTestResult(const ListValue* test_result, | |
74 bool* success) { | |
75 *success = false; | |
76 ASSERT_TRUE(test_result->GetBoolean(0, success)); | |
77 if (!success) { | |
78 std::string message; | |
79 ASSERT_TRUE(test_result->GetString(1, &message)); | |
80 LOG(ERROR) << message; | |
81 } | |
82 } | |
83 | |
38 void WebUITestHandler::Observe(int type, | 84 void WebUITestHandler::Observe(int type, |
39 const NotificationSource& source, | 85 const NotificationSource& source, |
40 const NotificationDetails& details) { | 86 const NotificationDetails& details) { |
41 // Quit the message loop if we were waiting so Waiting process can get result | 87 // 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. | 88 // or error. To ensure this gets done, do this before ASSERT* calls. |
43 if (is_waiting_) | 89 if (is_waiting_) |
44 MessageLoopForUI::current()->Quit(); | 90 MessageLoopForUI::current()->Quit(); |
45 | 91 |
46 SCOPED_TRACE("WebUITestHandler::Observe"); | 92 SCOPED_TRACE("WebUITestHandler::Observe"); |
47 Value* value = Details<std::pair<int, Value*> >(details)->second; | 93 Value* value = Details<std::pair<int, Value*> >(details)->second; |
48 ListValue* list_value; | 94 ListValue* test_result; |
49 ASSERT_TRUE(value->GetAsList(&list_value)); | 95 ASSERT_TRUE(value->GetAsList(&test_result)); |
50 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_)); | 96 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 } | 97 } |
57 | 98 |
58 bool WebUITestHandler::WaitForResult() { | 99 bool WebUITestHandler::WaitForResult() { |
59 is_waiting_ = true; | 100 is_waiting_ = true; |
60 ui_test_utils::RunMessageLoop(); | 101 ui_test_utils::RunMessageLoop(); |
61 is_waiting_ = false; | 102 is_waiting_ = false; |
62 return test_succeeded_; | 103 return test_succeeded_; |
63 } | 104 } |
OLD | NEW |