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 #include "chrome/renderer/prerender/prerender_dispatcher.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "testing/gtest/include/gtest/gtest.h"
19 int g_next_prerender_id
= 0;
23 using blink::WebPrerender
;
25 // Since we can't mock out blink::WebPrerender in chrome, this test can't test
26 // signalling to or from the WebKit side. Instead, it checks only that the
27 // messages received from the browser generate consistant state in the
28 // PrerenderDispatcher. Since prerenders couldn't even start or stop without the
29 // WebKit signalling, we can expect PrerenderBrowserTest to provide adequate
31 class PrerenderDispatcherTest
: public testing::Test
{
33 PrerenderDispatcherTest() {}
35 bool IsPrerenderURL(const GURL
& url
) const {
36 return prerender_dispatcher_
.IsPrerenderURL(url
);
39 const std::map
<int, WebPrerender
>& prerenders() const {
40 return prerender_dispatcher_
.prerenders_
;
43 int StartPrerender(const GURL
& url
) {
44 DCHECK_EQ(0u, prerender_dispatcher_
.prerenders_
.count(g_next_prerender_id
));
45 prerender_dispatcher_
.prerenders_
[g_next_prerender_id
] = WebPrerender();
47 prerender_dispatcher_
.OnPrerenderStart(g_next_prerender_id
);
48 prerender_dispatcher_
.OnPrerenderAddAlias(url
);
49 return g_next_prerender_id
++;
52 void AddAliasToPrerender(const GURL
& url
) {
53 prerender_dispatcher_
.OnPrerenderAddAlias(url
);
56 void RemoveAliasFromPrerender(const GURL
& url
) {
57 std::vector
<GURL
> urls
;
59 prerender_dispatcher_
.OnPrerenderRemoveAliases(urls
);
62 void StopPrerender(int prerender_id
) {
63 prerender_dispatcher_
.OnPrerenderStop(prerender_id
);
66 int GetCountForURL(const GURL
& url
) const {
67 return prerender_dispatcher_
.running_prerender_urls_
.count(url
);
71 PrerenderDispatcher prerender_dispatcher_
;
72 DISALLOW_COPY_AND_ASSIGN(PrerenderDispatcherTest
);
75 TEST_F(PrerenderDispatcherTest
, PrerenderDispatcherEmpty
) {
76 EXPECT_TRUE(prerenders().empty());
79 TEST_F(PrerenderDispatcherTest
, PrerenderDispatcherSingleAdd
) {
80 GURL foo_url
= GURL("http://foo.com");
81 EXPECT_FALSE(IsPrerenderURL(foo_url
));
82 StartPrerender(foo_url
);
83 EXPECT_TRUE(IsPrerenderURL(foo_url
));
84 EXPECT_EQ(1, GetCountForURL(foo_url
));
87 TEST_F(PrerenderDispatcherTest
, PrerenderDispatcherMultipleAdd
) {
88 GURL foo_url
= GURL("http://foo.com");
89 GURL bar_url
= GURL("http://bar.com");
91 EXPECT_FALSE(IsPrerenderURL(foo_url
));
92 EXPECT_FALSE(IsPrerenderURL(bar_url
));
93 StartPrerender(foo_url
);
94 EXPECT_TRUE(IsPrerenderURL(foo_url
));
95 EXPECT_FALSE(IsPrerenderURL(bar_url
));
97 AddAliasToPrerender(foo_url
);
98 EXPECT_TRUE(IsPrerenderURL(foo_url
));
99 EXPECT_FALSE(IsPrerenderURL(bar_url
));
100 EXPECT_EQ(2, GetCountForURL(foo_url
));
102 StartPrerender(bar_url
);
103 EXPECT_TRUE(IsPrerenderURL(foo_url
));
104 EXPECT_TRUE(IsPrerenderURL(bar_url
));
105 EXPECT_EQ(2, GetCountForURL(foo_url
));
106 EXPECT_EQ(1, GetCountForURL(bar_url
));
109 TEST_F(PrerenderDispatcherTest
, PrerenderDispatcherSingleRemove
) {
110 GURL foo_url
= GURL("http://foo.com");
111 EXPECT_FALSE(IsPrerenderURL(foo_url
));
112 int foo_id
= StartPrerender(foo_url
);
113 EXPECT_TRUE(IsPrerenderURL(foo_url
));
114 StopPrerender(foo_id
);
115 EXPECT_TRUE(IsPrerenderURL(foo_url
));
116 EXPECT_EQ(1, GetCountForURL(foo_url
));
117 RemoveAliasFromPrerender(foo_url
);
118 EXPECT_FALSE(IsPrerenderURL(foo_url
));
119 EXPECT_EQ(0, GetCountForURL(foo_url
));
122 TEST_F(PrerenderDispatcherTest
, PrerenderDispatcherTooManyRemoves
) {
123 GURL foo_url
= GURL("http://foo.com");
124 EXPECT_FALSE(IsPrerenderURL(foo_url
));
125 int foo_id
= StartPrerender(foo_url
);
126 EXPECT_TRUE(IsPrerenderURL(foo_url
));
127 StopPrerender(foo_id
);
128 EXPECT_TRUE(IsPrerenderURL(foo_url
));
129 EXPECT_EQ(1, GetCountForURL(foo_url
));
130 RemoveAliasFromPrerender(foo_url
);
131 EXPECT_FALSE(IsPrerenderURL(foo_url
));
132 EXPECT_EQ(0, GetCountForURL(foo_url
));
133 RemoveAliasFromPrerender(foo_url
);
134 EXPECT_FALSE(IsPrerenderURL(foo_url
));
135 EXPECT_EQ(0, GetCountForURL(foo_url
));
138 TEST_F(PrerenderDispatcherTest
, PrerenderDispatcherMultipleRemoves
) {
139 GURL foo_url
= GURL("http://foo.com");
140 EXPECT_FALSE(IsPrerenderURL(foo_url
));
141 int foo_id
= StartPrerender(foo_url
);
142 EXPECT_TRUE(IsPrerenderURL(foo_url
));
143 AddAliasToPrerender(foo_url
);
144 StopPrerender(foo_id
);
145 EXPECT_TRUE(IsPrerenderURL(foo_url
));
146 EXPECT_EQ(2, GetCountForURL(foo_url
));
147 RemoveAliasFromPrerender(foo_url
);
148 EXPECT_TRUE(IsPrerenderURL(foo_url
));
149 EXPECT_EQ(1, GetCountForURL(foo_url
));
150 RemoveAliasFromPrerender(foo_url
);
151 EXPECT_FALSE(IsPrerenderURL(foo_url
));
152 EXPECT_EQ(0, GetCountForURL(foo_url
));
155 } // end namespace prerender