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
3 // found in the LICENSE file.
5 #include "chrome/test/base/javascript_test_observer.h"
7 #include "content/public/browser/dom_operation_notification_details.h"
8 #include "content/public/browser/notification_types.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/test/test_utils.h"
12 TestMessageHandler::TestMessageHandler() : ok_(true) {
15 TestMessageHandler::~TestMessageHandler() {
18 void TestMessageHandler::SetError(const std::string& message) {
20 error_message_ = message;
23 void TestMessageHandler::Reset() {
25 error_message_.clear();
28 JavascriptTestObserver::JavascriptTestObserver(
29 content::RenderViewHost* render_view_host,
30 TestMessageHandler* handler)
35 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
36 content::Source<content::RenderViewHost>(render_view_host));
39 JavascriptTestObserver::~JavascriptTestObserver() {
42 bool JavascriptTestObserver::Run() {
43 // Messages may have arrived before Run was called.
47 content::RunMessageLoop();
50 return handler_->ok();
53 void JavascriptTestObserver::Reset() {
60 void JavascriptTestObserver::Observe(
62 const content::NotificationSource& source,
63 const content::NotificationDetails& details) {
64 CHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE);
65 content::Details<content::DomOperationNotificationDetails> dom_op_details(
67 // We might receive responses for other script execution, but we only
68 // care about the test finished message.
69 TestMessageHandler::MessageResponse response =
70 handler_->HandleMessage(dom_op_details->json);
72 if (response == TestMessageHandler::DONE) {
79 void JavascriptTestObserver::Continue() {
82 void JavascriptTestObserver::EndTest() {
86 base::MessageLoopForUI::current()->Quit();