Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2802)

Unified Diff: chrome/browser/ui/webui/web_ui_test_handler.cc

Issue 7576024: Provide ability for WebUIBrowserTests to run asynchronous tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Logic to handle multiple events coming on one RunMessageLoop() call. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..83b9fb206a6d1381da5802b98eb8e2422e000e6d 100644
--- a/chrome/browser/ui/webui/web_ui_test_handler.cc
+++ b/chrome/browser/ui/webui/web_ui_test_handler.cc
@@ -12,6 +12,14 @@
#include "content/common/notification_details.h"
#include "content/common/notification_registrar.h"
+WebUITestHandler::WebUITestHandler()
+ : test_succeeded_(false),
+ test_succeeded_async_(false),
+ is_waiting_(false),
+ test_done_(false),
+ test_done_async_(false) {
+}
+
void WebUITestHandler::PreloadJavaScript(const string16& js_text,
RenderViewHost* preload_host) {
DCHECK(preload_host);
@@ -25,14 +33,46 @@ 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;
+ test_succeeded_async_ = 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();
mmenke 2011/08/10 17:23:40 You could move this into HandleTestResult, since y
Sheridan Rawlins 2011/08/11 01:58:44 There are ASSERT_* calls in Observe which occur be
mmenke 2011/08/11 15:16:16 Err... Good point. On 2011/08/11 01:58:44, Sheri
+
+ EXPECT_FALSE(test_done_async_);
+ test_done_async_ = true;
+
+ SCOPED_TRACE("WebUITestHandler::HandleAsyncTestResult");
+ HandleTestResult(test_result, &test_succeeded_async_);
+}
+
+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,
@@ -43,21 +83,40 @@ void WebUITestHandler::Observe(int type,
if (is_waiting_)
MessageLoopForUI::current()->Quit();
+ EXPECT_FALSE(test_done_);
+ test_done_ = true;
+
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_ = false;
+ test_done_async_ = false;
is_waiting_ = true;
+
+ // Either sync test completion or the asyncTestDone() will cause message loop
+ // to quit.
ui_test_utils::RunMessageLoop();
+
+ // When the test |is_async|, run a second message loop when |test_done_async_|
+ // so that the sync test completes, or |test_succeeded_| so the async test
+ // completes.
mmenke 2011/08/10 17:23:40 This sentence isn't quite right. Maybe: When the
Sheridan Rawlins 2011/08/11 01:58:44 Done.
+ if (is_async && (!test_done_ || (test_succeeded_ && !test_done_async_))) {
+ ui_test_utils::RunMessageLoop();
+ }
+
+ // Verify that |test_done_async_| isn't set when !|is_async|. When |is_async|
+ // is true, |test_done_async_| may be false if the sync test failed first.
+ if (!is_async)
+ EXPECT_FALSE(test_done_async_);
+
is_waiting_ = false;
- return test_succeeded_;
+
+ // Both loops must succeed to pass.
mmenke 2011/08/10 17:23:40 nit: If !is_async, that's not quite true.
Sheridan Rawlins 2011/08/11 01:58:44 Done.
+ return test_succeeded_ && (!is_async || test_succeeded_async_);
}

Powered by Google App Engine
This is Rietveld 408576698