1 // Copyright (c) 2012 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 #ifndef CHROME_TEST_BASE_JAVASCRIPT_TEST_OBSERVER_H_
6 #define CHROME_TEST_BASE_JAVASCRIPT_TEST_OBSERVER_H_
10 #include "base/compiler_specific.h"
11 #include "content/public/browser/notification_observer.h"
12 #include "content/public/browser/notification_registrar.h"
15 class DictionaryValue
;
22 // Base class for handling a stream of automation messages produced by a
23 // JavascriptTestObserver.
24 class TestMessageHandler
{
26 enum MessageResponse
{
27 // Reset the timeout and keep running.
34 virtual ~TestMessageHandler();
36 // Called when a message is received from the DOM automation controller.
37 virtual MessageResponse
HandleMessage(const std::string
& json
) = 0;
39 void SetError(const std::string
& message
);
45 const std::string
& error_message() const {
46 return error_message_
;
49 // Prepare the handler to be used or reused.
54 std::string error_message_
;
57 // This class captures a stream of automation messages coming from a Javascript
58 // test and dispatches them to a message handler.
59 class JavascriptTestObserver
: public content::NotificationObserver
{
61 // The observer does not own any arguments passed to it. It is assumed that
62 // the arguments will outlive all uses of the observer.
63 JavascriptTestObserver(
64 content::RenderViewHost
* render_view_host
,
65 TestMessageHandler
* handler
);
67 virtual ~JavascriptTestObserver();
69 // Pump the message loop until the message handler indicates the Javascript
70 // test is done running. Return true if the test jig functioned correctly and
74 // Prepare the observer to be used again. This method should NOT be called
75 // while Run() is pumping the message loop.
80 const content::NotificationSource
& source
,
81 const content::NotificationDetails
& details
) OVERRIDE
;
84 // This message did not signal the end of a test, keep going.
87 // This was the last message we care about, stop listening for more messages.
90 TestMessageHandler
* handler_
;
93 content::NotificationRegistrar registrar_
;
95 DISALLOW_COPY_AND_ASSIGN(JavascriptTestObserver
);
98 #endif // CHROME_TEST_BASE_JAVASCRIPT_TEST_OBSERVER_H_