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 14b407fa43b14fe7a8bdac81af2a560ec979c991..449530231c63a808753f059bef0000b9209114a9 100644 |
| --- a/chrome/browser/ui/webui/web_ui_test_handler.cc |
| +++ b/chrome/browser/ui/webui/web_ui_test_handler.cc |
| @@ -12,6 +12,12 @@ |
| #include "content/common/notification_details.h" |
| #include "content/common/notification_registrar.h" |
| +WebUITestHandler::WebUITestHandler() |
| + : test_succeeded_(false), |
| + is_waiting_(false), |
| + test_done_async_(false) { |
| +} |
| + |
| void WebUITestHandler::PreloadJavaScript(const string16& js_text, |
| RenderViewHost* preload_host) { |
| DCHECK(preload_host); |
| @@ -25,14 +31,45 @@ void WebUITestHandler::RunJavaScript(const string16& js_text) { |
| string16(), js_text); |
| } |
| -bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text) { |
| +bool WebUITestHandler::RunJavaScriptTestWithResult(const string16& js_text, |
| + bool is_async) { |
| + test_succeeded_ = false; |
| NotificationRegistrar notification_registrar; |
| notification_registrar.Add( |
| this, content::NOTIFICATION_EXECUTE_JAVASCRIPT_RESULT, |
| Source<RenderViewHost>(web_ui_->GetRenderViewHost())); |
| web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrameNotifyResult( |
| string16(), js_text); |
| - return WaitForResult(); |
| + return WaitForResult(is_async); |
| +} |
| + |
| +void WebUITestHandler::RegisterMessages() { |
| + web_ui_->RegisterMessageCallback("asyncTestResult", NewCallback( |
| + this, &WebUITestHandler::HandleAsyncTestResult)); |
| +} |
| + |
| +void WebUITestHandler::HandleAsyncTestResult(const ListValue* test_result) { |
| + // 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. |
| + if (is_waiting_) |
| + MessageLoopForUI::current()->Quit(); |
| + |
| + EXPECT_FALSE(test_done_async_); |
| + test_done_async_ = true; |
| + |
| + SCOPED_TRACE("WebUITestHandler::HandleAsyncTestResult"); |
| + HandleTestResult(test_result, &test_succeeded_); |
|
mmenke
2011/08/08 16:38:25
What if an async failure is sent before a successf
Sheridan Rawlins
2011/08/10 01:58:53
Both quit the loop, so both cannot be received. If
mmenke
2011/08/10 04:17:53
The loop does not quit instantly. It may handle c
Sheridan Rawlins
2011/08/10 05:41:06
Yup. Had to add another boolean back in... Take
|
| +} |
| + |
| +void WebUITestHandler::HandleTestResult(const ListValue* test_result, |
| + bool* success) { |
| + *success = false; |
| + ASSERT_TRUE(test_result->GetBoolean(0, success)); |
| + if (!success) { |
| + std::string message; |
| + ASSERT_TRUE(test_result->GetString(1, &message)); |
| + LOG(ERROR) << message; |
| + } |
| } |
| void WebUITestHandler::Observe(int type, |
| @@ -45,19 +82,21 @@ void WebUITestHandler::Observe(int type, |
| SCOPED_TRACE("WebUITestHandler::Observe"); |
| 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; |
| - } |
| + ListValue* test_result; |
| + ASSERT_TRUE(value->GetAsList(&test_result)); |
| + HandleTestResult(test_result, &test_succeeded_); |
| } |
| -bool WebUITestHandler::WaitForResult() { |
| +bool WebUITestHandler::WaitForResult(bool is_async) { |
| + SCOPED_TRACE("WebUITestHandler::WaitForResult"); |
| + test_done_async_ = false; |
| is_waiting_ = true; |
| ui_test_utils::RunMessageLoop(); |
| + if (test_succeeded_ && is_async && !test_done_async_) { |
|
Sheridan Rawlins
2011/08/10 01:58:53
I think this should be
(is_async && (test_don
|
| + test_succeeded_ = false; |
| + ui_test_utils::RunMessageLoop(); |
| + } |
| + EXPECT_EQ(is_async, test_done_async_); |
| is_waiting_ = false; |
| return test_succeeded_; |
| } |