test_navigation_observer.h revision 868fa2fe829687343ffae624259930155e16dbd8
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#include "content/public/browser/web_contents.h"
14
15namespace content {
16
17struct LoadCommittedDetails;
18
19// For browser_tests, which run on the UI thread, run a second
20// MessageLoop and quit when the navigation completes loading.
21class TestNavigationObserver : public NotificationObserver {
22 public:
23  // Create and register a new TestNavigationObserver against the
24  // |web_contents|.
25  TestNavigationObserver(WebContents* web_contents,
26                         int number_of_navigations);
27  // Like above but waits for one navigation.
28  explicit TestNavigationObserver(WebContents* web_contents);
29
30  virtual ~TestNavigationObserver();
31
32  // Run |wait_loop_callback| until complete, then run |done_callback|.
33  void WaitForObservation(const base::Closure& wait_loop_callback,
34                          const base::Closure& done_callback);
35  // Convenient version of the above that runs a nested message loop and waits.
36  void Wait();
37
38  // Start/stop watching newly created WebContents.
39  void StartWatchingNewWebContents();
40  void StopWatchingNewWebContents();
41
42 protected:
43  // Register this TestNavigationObserver as an observer of the |web_contents|.
44  void RegisterAsObserver(WebContents* web_contents);
45
46  // NotificationObserver:
47  virtual void Observe(int type, const NotificationSource& source,
48                       const NotificationDetails& details) OVERRIDE;
49
50 private:
51  class TestWebContentsObserver;
52
53  // Callbacks for WebContents-related events.
54  void OnWebContentsCreated(WebContents* web_contents);
55  void OnWebContentsDestroyed(TestWebContentsObserver* observer,
56                              WebContents* web_contents);
57  void OnNavigationEntryCommitted(
58      TestWebContentsObserver* observer,
59      WebContents* web_contents,
60      const LoadCommittedDetails& load_details);
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  // |done_| will get set when this object observes a TabStripModel event.
74  bool done_;
75
76  // |running_| will be true during WaitForObservation until |done_| is true.
77  bool running_;
78
79  // |done_callback_| will be set while |running_| is true and will be called
80  // when navigation completes.
81  base::Closure done_callback_;
82
83  // Callback invoked on WebContents creation.
84  WebContents::CreatedCallback web_contents_created_callback_;
85
86  // Living TestWebContentsObservers created by this observer.
87  std::set<TestWebContentsObserver*> web_contents_observers_;
88
89  DISALLOW_COPY_AND_ASSIGN(TestNavigationObserver);
90};
91
92}  // namespace content
93
94#endif  // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_
95