test_navigation_observer.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 16// For browser_tests, which run on the UI thread, run a second 17// MessageLoop and quit when the navigation completes loading. 18class TestNavigationObserver : public NotificationObserver { 19 public: 20 // Create and register a new TestNavigationObserver against the |source|. 21 TestNavigationObserver(const NotificationSource& source, 22 int number_of_navigations); 23 // Like above but waits for one navigation. 24 explicit TestNavigationObserver(const NotificationSource& source); 25 26 virtual ~TestNavigationObserver(); 27 28 // Run |wait_loop_callback| until complete, then run |done_callback|. 29 void WaitForObservation(const base::Closure& wait_loop_callback, 30 const base::Closure& done_callback); 31 // Convenient version of the above that runs a nested message loop and waits. 32 void Wait(); 33 34 protected: 35 explicit TestNavigationObserver(int number_of_navigations); 36 37 // Register this TestNavigationObserver as an observer of the |source|. 38 void RegisterAsObserver(const NotificationSource& source); 39 40 // NotificationObserver: 41 virtual void Observe(int type, const NotificationSource& source, 42 const NotificationDetails& details) OVERRIDE; 43 44 private: 45 NotificationRegistrar registrar_; 46 47 // If true the navigation has started. 48 bool navigation_started_; 49 50 // The number of navigations that have been completed. 51 int navigations_completed_; 52 53 // The number of navigations to wait for. 54 int number_of_navigations_; 55 56 // |done_| will get set when this object observes a TabStripModel event. 57 bool done_; 58 59 // |done_callback_| will be set while |running_| is true and will be called 60 // when navigation completes. 61 base::Closure done_callback_; 62 63 // |running_| will be true during WaitForObservation until |done_| is true. 64 bool running_; 65 66 DISALLOW_COPY_AND_ASSIGN(TestNavigationObserver); 67}; 68 69} // namespace content 70 71#endif // CONTENT_PUBLIC_TEST_TEST_NAVIGATION_OBSERVER_H_ 72