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