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/test/ui_test_utils.h" | 9 #include "chrome/test/ui_test_utils.h" |
10 #include "content/browser/renderer_host/render_view_host.h" | 10 #include "content/browser/renderer_host/render_view_host.h" |
| 11 #include "content/common/notification_details.h" |
| 12 #include "content/common/notification_registrar.h" |
11 | 13 |
12 bool WebUITestHandler::RunJavascript(const std::string& js_test, | 14 bool WebUITestHandler::RunJavascript(const std::string& js_test, bool is_test) { |
13 bool is_test) { | 15 if (is_test) { |
14 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( | 16 NotificationRegistrar notification_registrar; |
15 string16(), UTF8ToUTF16(js_test)); | 17 notification_registrar.Add( |
16 | 18 this, NotificationType::EXECUTE_JAVASCRIPT_RESULT, |
17 if (is_test) | 19 Source<RenderViewHost>(web_ui_->GetRenderViewHost())); |
| 20 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult( |
| 21 string16(), UTF8ToUTF16(js_test)); |
18 return WaitForResult(); | 22 return WaitForResult(); |
19 else | 23 } else { |
| 24 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( |
| 25 string16(), UTF8ToUTF16(js_test)); |
20 return true; | 26 return true; |
| 27 } |
21 } | 28 } |
22 | 29 |
23 void WebUITestHandler::HandlePass(const ListValue* args) { | 30 void WebUITestHandler::Observe(NotificationType type, |
24 test_succeeded_ = true; | 31 const NotificationSource& source, |
25 if (is_waiting_) | 32 const NotificationDetails& details) { |
26 MessageLoopForUI::current()->Quit(); | 33 // Quit the message loop if we were waiting so Waiting process can get result |
27 } | 34 // or error. To ensure this gets done, do this before ASSERT* calls. |
28 | |
29 void WebUITestHandler::HandleFail(const ListValue* args) { | |
30 test_succeeded_ = false; | |
31 if (is_waiting_) | 35 if (is_waiting_) |
32 MessageLoopForUI::current()->Quit(); | 36 MessageLoopForUI::current()->Quit(); |
33 | 37 |
34 std::string message; | 38 SCOPED_TRACE("WebUITestHandler::Observe"); |
35 ASSERT_TRUE(args->GetString(0, &message)); | 39 Value* value = Details<std::pair<int, Value*> >(details)->second; |
36 LOG(ERROR) << message; | 40 ListValue* list_value; |
37 } | 41 ASSERT_TRUE(value->GetAsList(&list_value)); |
38 | 42 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_)); |
39 void WebUITestHandler::RegisterMessages() { | 43 if (!test_succeeded_) { |
40 web_ui_->RegisterMessageCallback("Pass", | 44 std::string message; |
41 NewCallback(this, &WebUITestHandler::HandlePass)); | 45 ASSERT_TRUE(list_value->GetString(1, &message)); |
42 web_ui_->RegisterMessageCallback("Fail", | 46 LOG(ERROR) << message; |
43 NewCallback(this, &WebUITestHandler::HandleFail)); | 47 } |
44 } | 48 } |
45 | 49 |
46 bool WebUITestHandler::WaitForResult() { | 50 bool WebUITestHandler::WaitForResult() { |
47 is_waiting_ = true; | 51 is_waiting_ = true; |
48 ui_test_utils::RunMessageLoop(); | 52 ui_test_utils::RunMessageLoop(); |
49 is_waiting_ = false; | 53 is_waiting_ = false; |
50 return test_succeeded_; | 54 return test_succeeded_; |
51 } | 55 } |
OLD | NEW |