summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/prerender/prerender_dispatcher_unittest.cc
blob: c90512424d92bd3dcb3f01edbe74ff0c5b6b3446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/prerender/prerender_dispatcher.h"

#include <map>
#include <utility>

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace prerender {

namespace {

int g_next_prerender_id = 0;

}  // namespace

using blink::WebPrerender;

// Since we can't mock out blink::WebPrerender in chrome, this test can't test
// signalling to or from the WebKit side. Instead, it checks only that the
// messages received from the browser generate consistant state in the
// PrerenderDispatcher. Since prerenders couldn't even start or stop without the
// WebKit signalling, we can expect PrerenderBrowserTest to provide adequate
// coverage of this.
class PrerenderDispatcherTest : public testing::Test {
 public:
  PrerenderDispatcherTest() {}

  bool IsPrerenderURL(const GURL& url) const {
    return prerender_dispatcher_.IsPrerenderURL(url);
  }

  const std::map<int, WebPrerender>& prerenders() const {
    return prerender_dispatcher_.prerenders_;
  }

  int StartPrerender(const GURL& url) {
    DCHECK_EQ(0u, prerender_dispatcher_.prerenders_.count(g_next_prerender_id));
    prerender_dispatcher_.prerenders_[g_next_prerender_id] = WebPrerender();

    prerender_dispatcher_.OnPrerenderStart(g_next_prerender_id);
    prerender_dispatcher_.OnPrerenderAddAlias(url);
    return g_next_prerender_id++;
  }

  void AddAliasToPrerender(const GURL& url) {
    prerender_dispatcher_.OnPrerenderAddAlias(url);
  }

  void RemoveAliasFromPrerender(const GURL& url) {
    std::vector<GURL> urls;
    urls.push_back(url);
    prerender_dispatcher_.OnPrerenderRemoveAliases(urls);
  }

  void StopPrerender(int prerender_id) {
    prerender_dispatcher_.OnPrerenderStop(prerender_id);
  }

  int GetCountForURL(const GURL& url) const {
    return prerender_dispatcher_.running_prerender_urls_.count(url);
  }

 private:
  PrerenderDispatcher prerender_dispatcher_;
  DISALLOW_COPY_AND_ASSIGN(PrerenderDispatcherTest);
};

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherEmpty) {
  EXPECT_TRUE(prerenders().empty());
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherSingleAdd) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  StartPrerender(foo_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_EQ(1, GetCountForURL(foo_url));
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleAdd) {
  GURL foo_url = GURL("http://foo.com");
  GURL bar_url = GURL("http://bar.com");

  EXPECT_FALSE(IsPrerenderURL(foo_url));
  EXPECT_FALSE(IsPrerenderURL(bar_url));
  StartPrerender(foo_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_FALSE(IsPrerenderURL(bar_url));

  AddAliasToPrerender(foo_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_FALSE(IsPrerenderURL(bar_url));
  EXPECT_EQ(2, GetCountForURL(foo_url));

  StartPrerender(bar_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_TRUE(IsPrerenderURL(bar_url));
  EXPECT_EQ(2, GetCountForURL(foo_url));
  EXPECT_EQ(1, GetCountForURL(bar_url));
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherSingleRemove) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  int foo_id = StartPrerender(foo_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  StopPrerender(foo_id);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_EQ(1, GetCountForURL(foo_url));
  RemoveAliasFromPrerender(foo_url);
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherTooManyRemoves) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  int foo_id = StartPrerender(foo_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  StopPrerender(foo_id);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_EQ(1, GetCountForURL(foo_url));
  RemoveAliasFromPrerender(foo_url);
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
  RemoveAliasFromPrerender(foo_url);
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleRemoves) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  int foo_id = StartPrerender(foo_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  AddAliasToPrerender(foo_url);
  StopPrerender(foo_id);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_EQ(2, GetCountForURL(foo_url));
  RemoveAliasFromPrerender(foo_url);
  EXPECT_TRUE(IsPrerenderURL(foo_url));
  EXPECT_EQ(1, GetCountForURL(foo_url));
  RemoveAliasFromPrerender(foo_url);
  EXPECT_FALSE(IsPrerenderURL(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
}

}  // end namespace prerender