Chromium Code Reviews| Index: chrome/browser/ui/webui/web_ui_test_handler.cc |
| diff --git a/chrome/browser/ui/webui/web_ui_test_handler.cc b/chrome/browser/ui/webui/web_ui_test_handler.cc |
| index 8712ad3ea47f1f13e5bba569833de8cc48c8474c..0c82b87b99f085c3e4460af44afb26bcbc43465a 100644 |
| --- a/chrome/browser/ui/webui/web_ui_test_handler.cc |
| +++ b/chrome/browser/ui/webui/web_ui_test_handler.cc |
| @@ -13,6 +13,14 @@ |
| #include "content/common/notification_details.h" |
| #include "content/common/notification_registrar.h" |
| +WebUITestHandler::WebUITestHandler() |
| + : test_done_(false), |
| + test_succeeded_(false), |
| + run_test_done_(false), |
| + run_test_succeeded_(false), |
| + is_waiting_(false) { |
| +} |
| + |
| void WebUITestHandler::PreloadJavaScript(const string16& js_text, |
| RenderViewHost* preload_host) { |
| DCHECK(preload_host); |
| @@ -27,6 +35,8 @@ void WebUITestHandler::RunJavaScript(const string16& js_text) { |
| } |
| bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { |
| + test_succeeded_ = false; |
| + run_test_succeeded_ = false; |
| RenderViewHost* rvh = web_ui_->tab_contents()->render_view_host(); |
| NotificationRegistrar notification_registrar; |
| notification_registrar.Add( |
| @@ -36,29 +46,70 @@ bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { |
| return WaitForResult(); |
| } |
| +void WebUITestHandler::RegisterMessages() { |
| + web_ui_->RegisterMessageCallback("testResult", NewCallback( |
| + this, &WebUITestHandler::HandleTestResult)); |
| +} |
| + |
| +void WebUITestHandler::HandleTestResult(const ListValue* test_result) { |
| + // Quit the message loop if |is_waiting_| so waiting process can get result or |
| + // error. To ensure this gets done, do this before ASSERT* calls. |
| + if (is_waiting_) |
| + MessageLoopForUI::current()->Quit(); |
| + |
| + SCOPED_TRACE("WebUITestHandler::HandleAsyncTestResult"); |
|
mmenke
2011/08/12 14:36:44
nit: Remove Async.
Sheridan Rawlins
2011/08/12 18:02:09
Done.
|
| + |
| + EXPECT_FALSE(test_done_); |
| + test_done_ = true; |
| + test_succeeded_ = false; |
| + |
| + ASSERT_TRUE(test_result->GetBoolean(0, &test_succeeded_)); |
| + if (!test_succeeded_) { |
| + std::string message; |
| + ASSERT_TRUE(test_result->GetString(1, &message)); |
| + LOG(ERROR) << message; |
| + } |
| +} |
| + |
| void WebUITestHandler::Observe(int type, |
| const NotificationSource& source, |
| const NotificationDetails& details) { |
| - // Quit the message loop if we were waiting so Waiting process can get result |
| - // or error. To ensure this gets done, do this before ASSERT* calls. |
| + // Quit the message loop if |is_waiting_| so waiting process can get result or |
| + // error. To ensure this gets done, do this before ASSERT* calls. |
| if (is_waiting_) |
| MessageLoopForUI::current()->Quit(); |
| + ASSERT_EQ(content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, type); |
| + |
| SCOPED_TRACE("WebUITestHandler::Observe"); |
| + |
| + EXPECT_FALSE(run_test_done_); |
| + run_test_done_ = true; |
| + run_test_succeeded_ = false; |
| + |
| Value* value = Details<std::pair<int, Value*> >(details)->second; |
| - ListValue* list_value; |
| - ASSERT_TRUE(value->GetAsList(&list_value)); |
| - ASSERT_TRUE(list_value->GetBoolean(0, &test_succeeded_)); |
| - if (!test_succeeded_) { |
| - std::string message; |
| - ASSERT_TRUE(list_value->GetString(1, &message)); |
| - LOG(ERROR) << message; |
| - } |
| + ASSERT_TRUE(value->GetAsBoolean(&run_test_succeeded_)); |
| } |
| bool WebUITestHandler::WaitForResult() { |
| + SCOPED_TRACE("WebUITestHandler::WaitForResult"); |
| + test_done_ = false; |
| + run_test_done_ = false; |
| is_waiting_ = true; |
| + |
| + // Either sync test completion or the testDone() will cause message loop |
| + // to quit. |
| ui_test_utils::RunMessageLoop(); |
| + |
| + // Run a second message loop when not |run_test_done_| so that the sync test |
| + // completes, or |run_test_succeeded_| but not |test_done_| so async tests |
| + // complete. |
| + if (!run_test_done_ || (run_test_succeeded_ && !test_done_)) { |
| + ui_test_utils::RunMessageLoop(); |
|
mmenke
2011/08/12 14:28:17
I had hoped we could get rid of this, but does see
Sheridan Rawlins
2011/08/12 18:02:09
Alas, this is the case given your earlier observat
|
| + } |
| + |
| is_waiting_ = false; |
| - return test_succeeded_; |
| + |
| + // To succeed the test must execute as well as pass the test. |
| + return run_test_succeeded_ && test_succeeded_; |
| } |