test_navigation_observer.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#ifndef CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_
6#define CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_
7
8#include "base/callback.h"
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
14namespace content {
15
16class JsInjectionReadyObserver;
17
18// For browser_tests, which run on the UI thread, run a second
19// MessageLoop and quit when the navigation completes loading. For
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.
23class TestNavigationObserver : public NotificationObserver {
24 public:
25  class RVHOSendJS;
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,
33                         JsInjectionReadyObserver* js_injection_ready_observer,
34                         int number_of_navigations);
35  // Like above but waits for one navigation.
36  explicit TestNavigationObserver(const NotificationSource& source);
37
38  virtual ~TestNavigationObserver();
39
40  // Run |wait_loop_callback| until complete, then run |done_callback|.
41  void WaitForObservation(const base::Closure& wait_loop_callback,
42                          const base::Closure& done_callback);
43  // Convenient version of the above that runs a nested message loop and waits.
44  void Wait();
45
46 protected:
47  // Note: |js_injection_ready_observer| is owned by the caller and should be
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
54  // Register this TestNavigationObserver as an observer of the |source|.
55  void RegisterAsObserver(const NotificationSource& source);
56
57 private:
58  // NotificationObserver:
59  virtual void Observe(int type, const NotificationSource& source,
60                       const NotificationDetails& details) OVERRIDE;
61
62  NotificationRegistrar registrar_;
63
64  // If true the navigation has started.
65  bool navigation_started_;
66
67  // The number of navigations that have been completed.
68  int navigations_completed_;
69
70  // The number of navigations to wait for.
71  int number_of_navigations_;
72
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.
78  bool done_;
79
80  // |done_callback_| will be set while |running_| is true and will be called
81  // when navigation completes.
82  base::Closure done_callback_;
83
84  // |running_| will be true during WaitForObservation until |done_| is true.
85  bool running_;
86
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);
92};
93
94}  // namespace content
95
96#endif  // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_
97