- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / test / test_navigation_observer.h
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/web_contents.h"
12 #include "content/public/test/test_utils.h"
13
14 namespace content {
15
16 struct LoadCommittedDetails;
17
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 {
21  public:
22   // Create and register a new TestNavigationObserver against the
23   // |web_contents|.
24   TestNavigationObserver(WebContents* web_contents,
25                          int number_of_navigations);
26   // Like above but waits for one navigation.
27   explicit TestNavigationObserver(WebContents* web_contents);
28
29   virtual ~TestNavigationObserver();
30
31   // Runs a nested message loop and blocks until the expected number of
32   // navigations are complete.
33   void Wait();
34
35   // Start/stop watching newly created WebContents.
36   void StartWatchingNewWebContents();
37   void StopWatchingNewWebContents();
38
39  protected:
40   // Register this TestNavigationObserver as an observer of the |web_contents|.
41   void RegisterAsObserver(WebContents* web_contents);
42
43  private:
44   class TestWebContentsObserver;
45
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);
56
57   // If true the navigation has started.
58   bool navigation_started_;
59
60   // The number of navigations that have been completed.
61   int navigations_completed_;
62
63   // The number of navigations to wait for.
64   int number_of_navigations_;
65
66   // The MessageLoopRunner used to spin the message loop.
67   scoped_refptr<MessageLoopRunner> message_loop_runner_;
68
69   // Callback invoked on WebContents creation.
70   WebContents::CreatedCallback web_contents_created_callback_;
71
72   // Living TestWebContentsObservers created by this observer.
73   std::set<TestWebContentsObserver*> web_contents_observers_;
74
75   DISALLOW_COPY_AND_ASSIGN(TestNavigationObserver);
76 };
77
78 }  // namespace content
79
80 #endif  // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_