test_navigation_observer.cc 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#include "content/public/test/test_navigation_observer.h"
6
7#include "base/bind.h"
8#include "base/message_loop.h"
9#include "base/run_loop.h"
10#include "content/public/browser/notification_service.h"
11#include "content/public/browser/notification_types.h"
12#include "content/public/test/test_utils.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace content {
16
17TestNavigationObserver::TestNavigationObserver(
18    const NotificationSource& source,
19    int number_of_navigations)
20    : navigation_started_(false),
21      navigations_completed_(0),
22      number_of_navigations_(number_of_navigations),
23      done_(false),
24      running_(false) {
25  RegisterAsObserver(source);
26}
27
28TestNavigationObserver::TestNavigationObserver(
29    const NotificationSource& source)
30    : navigation_started_(false),
31      navigations_completed_(0),
32      number_of_navigations_(1),
33      done_(false),
34      running_(false) {
35  RegisterAsObserver(source);
36}
37
38TestNavigationObserver::~TestNavigationObserver() {
39}
40
41void TestNavigationObserver::WaitForObservation(
42    const base::Closure& wait_loop_callback,
43    const base::Closure& done_callback) {
44  if (done_)
45    return;
46
47  EXPECT_FALSE(running_);
48  running_ = true;
49  done_callback_ = done_callback;
50  wait_loop_callback.Run();
51  EXPECT_TRUE(done_);
52}
53
54void TestNavigationObserver::Wait() {
55  base::RunLoop run_loop;
56  WaitForObservation(
57      base::Bind(&base::RunLoop::Run, base::Unretained(&run_loop)),
58      GetQuitTaskForRunLoop(&run_loop));
59}
60
61TestNavigationObserver::TestNavigationObserver(
62    int number_of_navigations)
63    : navigation_started_(false),
64      navigations_completed_(0),
65      number_of_navigations_(number_of_navigations),
66      done_(false),
67      running_(false) {
68}
69
70void TestNavigationObserver::RegisterAsObserver(
71    const NotificationSource& source) {
72  // Register for events to know when we've finished loading the page and are
73  // ready to quit the current message loop to return control back to the
74  // waiting test.
75  registrar_.Add(this, NOTIFICATION_NAV_ENTRY_COMMITTED, source);
76  registrar_.Add(this, NOTIFICATION_LOAD_START, source);
77  registrar_.Add(this, NOTIFICATION_LOAD_STOP, source);
78}
79
80void TestNavigationObserver::Observe(
81    int type, const NotificationSource& source,
82    const NotificationDetails& details) {
83  switch (type) {
84    case NOTIFICATION_NAV_ENTRY_COMMITTED:
85    case NOTIFICATION_LOAD_START:
86      navigation_started_ = true;
87      break;
88    case NOTIFICATION_LOAD_STOP:
89      if (navigation_started_ &&
90          ++navigations_completed_ == number_of_navigations_) {
91        navigation_started_ = false;
92        done_ = true;
93        if (running_) {
94          running_ = false;
95          done_callback_.Run();
96        }
97      }
98      break;
99    default:
100      NOTREACHED();
101  }
102}
103
104}  // namespace content
105