OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_TEST_BASE_JAVASCRIPT_TEST_OBSERVER_H_ | 5 #ifndef CONTENT_PUBLIC_TEST_JAVASCRIPT_TEST_OBSERVER_H_ |
6 #define CHROME_TEST_BASE_JAVASCRIPT_TEST_OBSERVER_H_ | 6 #define CONTENT_PUBLIC_TEST_JAVASCRIPT_TEST_OBSERVER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "content/public/browser/notification_observer.h" | 11 #include "content/public/browser/notification_observer.h" |
12 #include "content/public/browser/notification_registrar.h" | 12 #include "content/public/browser/notification_registrar.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 class DictionaryValue; | 15 class DictionaryValue; |
16 } | 16 } |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 class WebContents; | 19 class WebContents; |
20 } | |
21 | 20 |
22 // Base class for handling a stream of automation messages produced by a | 21 // Base class for handling a stream of automation messages produced by a |
23 // JavascriptTestObserver. | 22 // JavascriptTestObserver. |
24 class TestMessageHandler { | 23 class TestMessageHandler { |
25 public: | 24 public: |
26 enum MessageResponse { | 25 enum MessageResponse { |
27 // Reset the timeout and keep running. | 26 // Reset the timeout and keep running. |
28 CONTINUE, | 27 CONTINUE, |
29 // Stop runnning. | 28 // Stop runnning. |
30 DONE | 29 DONE |
(...skipping 18 matching lines...) Expand all Loading... |
49 // Prepare the handler to be used or reused. | 48 // Prepare the handler to be used or reused. |
50 virtual void Reset(); | 49 virtual void Reset(); |
51 | 50 |
52 private: | 51 private: |
53 bool ok_; | 52 bool ok_; |
54 std::string error_message_; | 53 std::string error_message_; |
55 }; | 54 }; |
56 | 55 |
57 // This class captures a stream of automation messages coming from a Javascript | 56 // This class captures a stream of automation messages coming from a Javascript |
58 // test and dispatches them to a message handler. | 57 // test and dispatches them to a message handler. |
59 class JavascriptTestObserver : public content::NotificationObserver { | 58 class JavascriptTestObserver : public NotificationObserver { |
60 public: | 59 public: |
61 // The observer does not own any arguments passed to it. It is assumed that | 60 // The observer does not own any arguments passed to it. It is assumed that |
62 // the arguments will outlive all uses of the observer. | 61 // the arguments will outlive all uses of the observer. |
63 JavascriptTestObserver( | 62 JavascriptTestObserver(WebContents* web_contents, |
64 content::WebContents* web_contents, | 63 TestMessageHandler* handler); |
65 TestMessageHandler* handler); | |
66 | 64 |
67 virtual ~JavascriptTestObserver(); | 65 virtual ~JavascriptTestObserver(); |
68 | 66 |
69 // Pump the message loop until the message handler indicates the Javascript | 67 // 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 | 68 // test is done running. Return true if the test jig functioned correctly and |
71 // nothing timed out. | 69 // nothing timed out. |
72 bool Run(); | 70 bool Run(); |
73 | 71 |
74 // Prepare the observer to be used again. This method should NOT be called | 72 // Prepare the observer to be used again. This method should NOT be called |
75 // while Run() is pumping the message loop. | 73 // while Run() is pumping the message loop. |
76 void Reset(); | 74 void Reset(); |
77 | 75 |
78 virtual void Observe( | 76 virtual void Observe( |
79 int type, | 77 int type, |
80 const content::NotificationSource& source, | 78 const NotificationSource& source, |
81 const content::NotificationDetails& details) OVERRIDE; | 79 const NotificationDetails& details) OVERRIDE; |
82 | 80 |
83 private: | 81 private: |
84 // This message did not signal the end of a test, keep going. | 82 // This message did not signal the end of a test, keep going. |
85 void Continue(); | 83 void Continue(); |
86 | 84 |
87 // This was the last message we care about, stop listening for more messages. | 85 // This was the last message we care about, stop listening for more messages. |
88 void EndTest(); | 86 void EndTest(); |
89 | 87 |
90 TestMessageHandler* handler_; | 88 TestMessageHandler* handler_; |
91 bool running_; | 89 bool running_; |
92 bool finished_; | 90 bool finished_; |
93 content::NotificationRegistrar registrar_; | 91 NotificationRegistrar registrar_; |
94 | 92 |
95 DISALLOW_COPY_AND_ASSIGN(JavascriptTestObserver); | 93 DISALLOW_COPY_AND_ASSIGN(JavascriptTestObserver); |
96 }; | 94 }; |
97 | 95 |
98 #endif // CHROME_TEST_BASE_JAVASCRIPT_TEST_OBSERVER_H_ | 96 } // namespace content |
| 97 |
| 98 #endif // CONTENT_PUBLIC_TEST_JAVASCRIPT_TEST_OBSERVER_H_ |
OLD | NEW |