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/browser/tab_contents/tab_contents.h" |
12 #include "content/common/notification_details.h" | 13 #include "content/common/notification_details.h" |
13 #include "content/common/notification_registrar.h" | 14 #include "content/common/notification_registrar.h" |
14 | 15 |
15 void WebUITestHandler::PreloadJavaScript(const string16& js_text, | 16 void WebUITestHandler::PreloadJavaScript(const string16& js_text, |
16 RenderViewHost* preload_host) { | 17 RenderViewHost* preload_host) { |
17 DCHECK(preload_host); | 18 DCHECK(preload_host); |
18 preload_host->Send(new ViewMsg_WebUIJavaScript( | 19 preload_host->Send(new ViewMsg_WebUIJavaScript( |
19 preload_host->routing_id(), string16(), js_text, 0, | 20 preload_host->routing_id(), string16(), js_text, 0, |
20 false)); | 21 false)); |
21 } | 22 } |
22 | 23 |
23 void WebUITestHandler::RunJavaScript(const string16& js_text) { | 24 void WebUITestHandler::RunJavaScript(const string16& js_text) { |
24 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( | 25 web_ui_->tab_contents()->render_view_host()->ExecuteJavascriptInWebFrame( |
25 string16(), js_text); | 26 string16(), js_text); |
26 } | 27 } |
27 | 28 |
28 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { | 29 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { |
| 30 RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); |
29 NotificationRegistrar notification_registrar; | 31 NotificationRegistrar notification_registrar; |
30 notification_registrar.Add( | 32 notification_registrar.Add( |
31 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, | 33 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, |
32 Source<RenderViewHost>(web_ui_->GetRenderViewHost())); | 34 Source<RenderViewHost>(rvh)); |
33 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult( | 35 rvh->ExecuteJavascriptInWebFrameNotifyResult(string16(), js_text); |
34 string16(), js_text); | |
35 return WaitForResult(); | 36 return WaitForResult(); |
36 } | 37 } |
37 | 38 |
38 void WebUITestHandler::Observe(int type, | 39 void WebUITestHandler::Observe(int type, |
39 const NotificationSource& source, | 40 const NotificationSource& source, |
40 const NotificationDetails& details) { | 41 const NotificationDetails& details) { |
41 // Quit the message loop if we were waiting so Waiting process can get result | 42 // 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. | 43 // or error. To ensure this gets done, do this before ASSERT* calls. |
43 if (is_waiting_) | 44 if (is_waiting_) |
44 MessageLoopForUI::current()->Quit(); | 45 MessageLoopForUI::current()->Quit(); |
45 | 46 |
46 SCOPED_TRACE("WebUITestHandler::Observe"); | 47 SCOPED_TRACE("WebUITestHandler::Observe"); |
47 Value* value = Details<std::pair<int, Value*> >(details)->second; | 48 Value* value = Details<std::pair<int, Value*> >(details)->second; |
48 ListValue* list_value; | 49 ListValue* list_value; |
49 ASSERT_TRUE(value->GetAsList(&list_value)); | 50 ASSERT_TRUE(value->GetAsList(&list_value)); |
50 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_)); | 51 ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_)); |
51 if (!test_succeeded_) { | 52 if (!test_succeeded_) { |
52 std::string message; | 53 std::string message; |
53 ASSERT_TRUE(list_value->GetString(1, &message)); | 54 ASSERT_TRUE(list_value->GetString(1, &message)); |
54 LOG(ERROR) << message; | 55 LOG(ERROR) << message; |
55 } | 56 } |
56 } | 57 } |
57 | 58 |
58 bool WebUITestHandler::WaitForResult() { | 59 bool WebUITestHandler::WaitForResult() { |
59 is_waiting_ = true; | 60 is_waiting_ = true; |
60 ui_test_utils::RunMessageLoop(); | 61 ui_test_utils::RunMessageLoop(); |
61 is_waiting_ = false; | 62 is_waiting_ = false; |
62 return test_succeeded_; | 63 return test_succeeded_; |
63 } | 64 } |
OLD | NEW |