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.
5 #ifndef CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_
6 #define CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/test/test_utils.h"
16 struct LoadCommittedDetails;
18 // For browser_tests, which run on the UI thread, run a second
19 // MessageLoop and quit when the navigation completes loading.
20 class TestNavigationObserver {
22 // Create and register a new TestNavigationObserver against the
24 TestNavigationObserver(WebContents* web_contents,
25 int number_of_navigations);
26 // Like above but waits for one navigation.
27 explicit TestNavigationObserver(WebContents* web_contents);
29 virtual ~TestNavigationObserver();
31 // Runs a nested message loop and blocks until the expected number of
32 // navigations are complete.
35 // Start/stop watching newly created WebContents.
36 void StartWatchingNewWebContents();
37 void StopWatchingNewWebContents();
40 // Register this TestNavigationObserver as an observer of the |web_contents|.
41 void RegisterAsObserver(WebContents* web_contents);
44 class TestWebContentsObserver;
46 // Callbacks for WebContents-related events.
47 void OnWebContentsCreated(WebContents* web_contents);
48 void OnWebContentsDestroyed(TestWebContentsObserver* observer,
49 WebContents* web_contents);
50 void OnNavigationEntryCommitted(
51 TestWebContentsObserver* observer,
52 WebContents* web_contents,
53 const LoadCommittedDetails& load_details);
54 void OnDidStartLoading(WebContents* web_contents);
55 void OnDidStopLoading(WebContents* web_contents);
57 // If true the navigation has started.
58 bool navigation_started_;
60 // The number of navigations that have been completed.
61 int navigations_completed_;
63 // The number of navigations to wait for.
64 int number_of_navigations_;
66 // The MessageLoopRunner used to spin the message loop.
67 scoped_refptr<MessageLoopRunner> message_loop_runner_;
69 // Callback invoked on WebContents creation.
70 WebContents::CreatedCallback web_contents_created_callback_;
72 // Living TestWebContentsObservers created by this observer.
73 std::set<TestWebContentsObserver*> web_contents_observers_;
75 DISALLOW_COPY_AND_ASSIGN(TestNavigationObserver);
78 } // namespace content
80 #endif // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_