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 CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_ | 5 #ifndef CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_ |
6 #define CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_ | 6 #define CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.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 content { | 14 namespace content { |
15 | 15 |
16 class JsInjectionReadyObserver; | |
17 | |
18 // For browser_tests, which run on the UI thread, run a second | 16 // For browser_tests, which run on the UI thread, run a second |
19 // MessageLoop and quit when the navigation completes loading. For | 17 // MessageLoop and quit when the navigation completes loading. |
20 // WebUI tests that need to inject javascript, construct with a | |
21 // JsInjectionReadyObserver and this class will call its | |
22 // OnJsInjectionReady() at the appropriate time. | |
23 class TestNavigationObserver : public NotificationObserver { | 18 class TestNavigationObserver : public NotificationObserver { |
24 public: | 19 public: |
25 class RVHOSendJS; | 20 // Create and register a new TestNavigationObserver against the |source|. |
26 | |
27 // Create and register a new TestNavigationObserver against the | |
28 // |controller|. When |js_injection_ready_observer| is non-null, notify with | |
29 // OnEntryCommitted() after |number_of_navigations| navigations. | |
30 // Note: |js_injection_ready_observer| is owned by the caller and should be | |
31 // valid until this class is destroyed. | |
32 TestNavigationObserver(const NotificationSource& source, | 21 TestNavigationObserver(const NotificationSource& source, |
33 JsInjectionReadyObserver* js_injection_ready_observer, | |
34 int number_of_navigations); | 22 int number_of_navigations); |
35 // Like above but waits for one navigation. | 23 // Like above but waits for one navigation. |
36 explicit TestNavigationObserver(const NotificationSource& source); | 24 explicit TestNavigationObserver(const NotificationSource& source); |
37 | 25 |
38 virtual ~TestNavigationObserver(); | 26 virtual ~TestNavigationObserver(); |
39 | 27 |
40 // Run |wait_loop_callback| until complete, then run |done_callback|. | 28 // Run |wait_loop_callback| until complete, then run |done_callback|. |
41 void WaitForObservation(const base::Closure& wait_loop_callback, | 29 void WaitForObservation(const base::Closure& wait_loop_callback, |
42 const base::Closure& done_callback); | 30 const base::Closure& done_callback); |
43 // Convenient version of the above that runs a nested message loop and waits. | 31 // Convenient version of the above that runs a nested message loop and waits. |
44 void Wait(); | 32 void Wait(); |
45 | 33 |
46 protected: | 34 protected: |
47 // Note: |js_injection_ready_observer| is owned by the caller and should be | 35 explicit TestNavigationObserver(int number_of_navigations); |
48 // valid until this class is destroyed. Subclasses using this constructor MUST | |
49 // call RegisterAsObserver when a NavigationController becomes available. | |
50 explicit TestNavigationObserver( | |
51 JsInjectionReadyObserver* js_injection_ready_observer, | |
52 int number_of_navigations); | |
53 | 36 |
54 // Register this TestNavigationObserver as an observer of the |source|. | 37 // Register this TestNavigationObserver as an observer of the |source|. |
55 void RegisterAsObserver(const NotificationSource& source); | 38 void RegisterAsObserver(const NotificationSource& source); |
56 | 39 |
57 private: | |
58 // NotificationObserver: | 40 // NotificationObserver: |
59 virtual void Observe(int type, const NotificationSource& source, | 41 virtual void Observe(int type, const NotificationSource& source, |
60 const NotificationDetails& details) OVERRIDE; | 42 const NotificationDetails& details) OVERRIDE; |
61 | 43 |
| 44 private: |
62 NotificationRegistrar registrar_; | 45 NotificationRegistrar registrar_; |
63 | 46 |
64 // If true the navigation has started. | 47 // If true the navigation has started. |
65 bool navigation_started_; | 48 bool navigation_started_; |
66 | 49 |
67 // The number of navigations that have been completed. | 50 // The number of navigations that have been completed. |
68 int navigations_completed_; | 51 int navigations_completed_; |
69 | 52 |
70 // The number of navigations to wait for. | 53 // The number of navigations to wait for. |
71 int number_of_navigations_; | 54 int number_of_navigations_; |
72 | 55 |
73 // Observer to take some action when the page is ready for JavaScript | |
74 // injection. | |
75 JsInjectionReadyObserver* js_injection_ready_observer_; | |
76 | |
77 // |done_| will get set when this object observes a TabStripModel event. | 56 // |done_| will get set when this object observes a TabStripModel event. |
78 bool done_; | 57 bool done_; |
79 | 58 |
80 // |done_callback_| will be set while |running_| is true and will be called | 59 // |done_callback_| will be set while |running_| is true and will be called |
81 // when navigation completes. | 60 // when navigation completes. |
82 base::Closure done_callback_; | 61 base::Closure done_callback_; |
83 | 62 |
84 // |running_| will be true during WaitForObservation until |done_| is true. | 63 // |running_| will be true during WaitForObservation until |done_| is true. |
85 bool running_; | 64 bool running_; |
86 | 65 |
87 // |rvho_send_js_| will hold a RenderViewHostObserver subclass to allow | |
88 // JavaScript injection at the appropriate time. | |
89 scoped_ptr<RVHOSendJS> rvho_send_js_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(TestNavigationObserver); | 66 DISALLOW_COPY_AND_ASSIGN(TestNavigationObserver); |
92 }; | 67 }; |
93 | 68 |
94 } // namespace content | 69 } // namespace content |
95 | 70 |
96 #endif // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_ | 71 #endif // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_ |
OLD | NEW |