OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 void WebUITestHandler::RunJavaScript(const string16& js_text) { | 38 void WebUITestHandler::RunJavaScript(const string16& js_text) { |
39 web_ui()->GetWebContents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame( | 39 web_ui()->GetWebContents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame( |
40 string16(), js_text); | 40 string16(), js_text); |
41 } | 41 } |
42 | 42 |
43 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { | 43 bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { |
44 test_succeeded_ = false; | 44 test_succeeded_ = false; |
45 run_test_succeeded_ = false; | 45 run_test_succeeded_ = false; |
46 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); | 46 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
47 content::NotificationRegistrar notification_registrar; | 47 rvh->ExecuteJavascriptInWebFrameCallbackResult( |
48 notification_registrar.Add( | 48 string16(), // frame_xpath |
49 this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, | 49 js_text, |
50 content::Source<RenderViewHost>(rvh)); | 50 base::Bind(&WebUITestHandler::JavaScriptComplete, |
51 rvh->ExecuteJavascriptInWebFrameNotifyResult(string16(), js_text); | 51 base::Unretained(this))); |
52 return WaitForResult(); | 52 return WaitForResult(); |
53 } | 53 } |
54 | 54 |
55 void WebUITestHandler::RegisterMessages() { | 55 void WebUITestHandler::RegisterMessages() { |
56 web_ui()->RegisterMessageCallback("testResult", | 56 web_ui()->RegisterMessageCallback("testResult", |
57 base::Bind(&WebUITestHandler::HandleTestResult, base::Unretained(this))); | 57 base::Bind(&WebUITestHandler::HandleTestResult, base::Unretained(this))); |
58 } | 58 } |
59 | 59 |
60 void WebUITestHandler::HandleTestResult(const ListValue* test_result) { | 60 void WebUITestHandler::HandleTestResult(const ListValue* test_result) { |
61 // Quit the message loop if |is_waiting_| so waiting process can get result or | 61 // Quit the message loop if |is_waiting_| so waiting process can get result or |
62 // error. To ensure this gets done, do this before ASSERT* calls. | 62 // error. To ensure this gets done, do this before ASSERT* calls. |
63 if (is_waiting_) | 63 if (is_waiting_) |
64 MessageLoopForUI::current()->Quit(); | 64 MessageLoopForUI::current()->Quit(); |
65 | 65 |
66 SCOPED_TRACE("WebUITestHandler::HandleTestResult"); | 66 SCOPED_TRACE("WebUITestHandler::HandleTestResult"); |
67 | 67 |
68 EXPECT_FALSE(test_done_); | 68 EXPECT_FALSE(test_done_); |
69 test_done_ = true; | 69 test_done_ = true; |
70 test_succeeded_ = false; | 70 test_succeeded_ = false; |
71 | 71 |
72 ASSERT_TRUE(test_result->GetBoolean(0, &test_succeeded_)); | 72 ASSERT_TRUE(test_result->GetBoolean(0, &test_succeeded_)); |
73 if (!test_succeeded_) { | 73 if (!test_succeeded_) { |
74 std::string message; | 74 std::string message; |
75 ASSERT_TRUE(test_result->GetString(1, &message)); | 75 ASSERT_TRUE(test_result->GetString(1, &message)); |
76 LOG(ERROR) << message; | 76 LOG(ERROR) << message; |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 void WebUITestHandler::Observe(int type, | 80 void WebUITestHandler::JavaScriptComplete(const base::Value* result) { |
81 const content::NotificationSource& source, | |
82 const content::NotificationDetails& details) { | |
83 // Quit the message loop if |is_waiting_| so waiting process can get result or | 81 // Quit the message loop if |is_waiting_| so waiting process can get result or |
84 // error. To ensure this gets done, do this before ASSERT* calls. | 82 // error. To ensure this gets done, do this before ASSERT* calls. |
85 if (is_waiting_) | 83 if (is_waiting_) |
86 MessageLoopForUI::current()->Quit(); | 84 MessageLoopForUI::current()->Quit(); |
87 | 85 |
88 ASSERT_EQ(content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, type); | 86 SCOPED_TRACE("WebUITestHandler::JavaScriptComplete"); |
89 | |
90 SCOPED_TRACE("WebUITestHandler::Observe"); | |
91 | 87 |
92 EXPECT_FALSE(run_test_done_); | 88 EXPECT_FALSE(run_test_done_); |
93 run_test_done_ = true; | 89 run_test_done_ = true; |
94 run_test_succeeded_ = false; | 90 run_test_succeeded_ = false; |
95 | 91 |
96 Value* value = content::Details<std::pair<int, Value*> >(details)->second; | 92 ASSERT_TRUE(result->GetAsBoolean(&run_test_succeeded_)); |
97 ASSERT_TRUE(value->GetAsBoolean(&run_test_succeeded_)); | |
98 } | 93 } |
99 | 94 |
100 bool WebUITestHandler::WaitForResult() { | 95 bool WebUITestHandler::WaitForResult() { |
101 SCOPED_TRACE("WebUITestHandler::WaitForResult"); | 96 SCOPED_TRACE("WebUITestHandler::WaitForResult"); |
102 test_done_ = false; | 97 test_done_ = false; |
103 run_test_done_ = false; | 98 run_test_done_ = false; |
104 is_waiting_ = true; | 99 is_waiting_ = true; |
105 | 100 |
106 // Either sync test completion or the testDone() will cause message loop | 101 // Either sync test completion or the testDone() will cause message loop |
107 // to quit. | 102 // to quit. |
108 content::RunMessageLoop(); | 103 content::RunMessageLoop(); |
109 | 104 |
110 // Run a second message loop when not |run_test_done_| so that the sync test | 105 // Run a second message loop when not |run_test_done_| so that the sync test |
111 // completes, or |run_test_succeeded_| but not |test_done_| so async tests | 106 // completes, or |run_test_succeeded_| but not |test_done_| so async tests |
112 // complete. | 107 // complete. |
113 if (!run_test_done_ || (run_test_succeeded_ && !test_done_)) { | 108 if (!run_test_done_ || (run_test_succeeded_ && !test_done_)) { |
114 content::RunMessageLoop(); | 109 content::RunMessageLoop(); |
115 } | 110 } |
116 | 111 |
117 is_waiting_ = false; | 112 is_waiting_ = false; |
118 | 113 |
119 // To succeed the test must execute as well as pass the test. | 114 // To succeed the test must execute as well as pass the test. |
120 return run_test_succeeded_ && test_succeeded_; | 115 return run_test_succeeded_ && test_succeeded_; |
121 } | 116 } |
OLD | NEW |