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/browser/web_contents.h"
11 #include "content/public/test/test_utils.h"
13 TestMessageHandler::TestMessageHandler() : ok_(true) {
16 TestMessageHandler::~TestMessageHandler() {
19 void TestMessageHandler::SetError(const std::string
& message
) {
21 error_message_
= message
;
24 void TestMessageHandler::Reset() {
26 error_message_
.clear();
29 JavascriptTestObserver::JavascriptTestObserver(
30 content::WebContents
* web_contents
,
31 TestMessageHandler
* handler
)
37 content::NOTIFICATION_DOM_OPERATION_RESPONSE
,
38 content::Source
<content::WebContents
>(web_contents
));
41 JavascriptTestObserver::~JavascriptTestObserver() {
44 bool JavascriptTestObserver::Run() {
45 // Messages may have arrived before Run was called.
49 content::RunMessageLoop();
52 return handler_
->ok();
55 void JavascriptTestObserver::Reset() {
62 void JavascriptTestObserver::Observe(
64 const content::NotificationSource
& source
,
65 const content::NotificationDetails
& details
) {
66 CHECK(type
== content::NOTIFICATION_DOM_OPERATION_RESPONSE
);
67 content::Details
<content::DomOperationNotificationDetails
> dom_op_details(
69 // We might receive responses for other script execution, but we only
70 // care about the test finished message.
71 TestMessageHandler::MessageResponse response
=
72 handler_
->HandleMessage(dom_op_details
->json
);
74 if (response
== TestMessageHandler::DONE
) {
81 void JavascriptTestObserver::Continue() {
84 void JavascriptTestObserver::EndTest() {
88 base::MessageLoopForUI::current()->Quit();