Removing uses of X11 native key events.
[chromium-blink-merge.git] / content / public / test / javascript_test_observer.h
blob3b7b1f3c5ab38a3bb013ec474c908a7a9b4fb14b
1 // Copyright 2014 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 CONTENT_PUBLIC_TEST_JAVASCRIPT_TEST_OBSERVER_H_
6 #define CONTENT_PUBLIC_TEST_JAVASCRIPT_TEST_OBSERVER_H_
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "content/public/browser/notification_observer.h"
12 #include "content/public/browser/notification_registrar.h"
14 namespace base {
15 class DictionaryValue;
18 namespace content {
19 class WebContents;
21 // Base class for handling a stream of automation messages produced by a
22 // JavascriptTestObserver.
23 class TestMessageHandler {
24 public:
25 enum MessageResponse {
26 // Reset the timeout and keep running.
27 CONTINUE,
28 // Stop runnning.
29 DONE
32 TestMessageHandler();
33 virtual ~TestMessageHandler();
35 // Called when a message is received from the DOM automation controller.
36 virtual MessageResponse HandleMessage(const std::string& json) = 0;
38 void SetError(const std::string& message);
40 bool ok() const {
41 return ok_;
44 const std::string& error_message() const {
45 return error_message_;
48 // Prepare the handler to be used or reused.
49 virtual void Reset();
51 private:
52 bool ok_;
53 std::string error_message_;
56 // This class captures a stream of automation messages coming from a Javascript
57 // test and dispatches them to a message handler.
58 class JavascriptTestObserver : public NotificationObserver {
59 public:
60 // The observer does not own any arguments passed to it. It is assumed that
61 // the arguments will outlive all uses of the observer.
62 JavascriptTestObserver(WebContents* web_contents,
63 TestMessageHandler* handler);
65 virtual ~JavascriptTestObserver();
67 // Pump the message loop until the message handler indicates the Javascript
68 // test is done running. Return true if the test jig functioned correctly and
69 // nothing timed out.
70 bool Run();
72 // Prepare the observer to be used again. This method should NOT be called
73 // while Run() is pumping the message loop.
74 void Reset();
76 virtual void Observe(
77 int type,
78 const NotificationSource& source,
79 const NotificationDetails& details) OVERRIDE;
81 private:
82 // This message did not signal the end of a test, keep going.
83 void Continue();
85 // This was the last message we care about, stop listening for more messages.
86 void EndTest();
88 TestMessageHandler* handler_;
89 bool running_;
90 bool finished_;
91 NotificationRegistrar registrar_;
93 DISALLOW_COPY_AND_ASSIGN(JavascriptTestObserver);
96 } // namespace content
98 #endif // CONTENT_PUBLIC_TEST_JAVASCRIPT_TEST_OBSERVER_H_