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 "chrome/renderer/prerender/prerender_dispatcher.h" 6 7 #include <map> 8 #include <utility> 9 10 #include "base/compiler_specific.h" 11 #include "base/logging.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "url/gurl.h" 14 15 namespace prerender { 16 17 namespace { 18 19 int g_next_prerender_id = 0; 20 21 } // namespace 22 23 using blink::WebPrerender; 24 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 30 // coverage of this. 31 class PrerenderDispatcherTest : public testing::Test { 32 public: 33 PrerenderDispatcherTest() {} 34 35 bool IsPrerenderURL(const GURL& url) const { 36 return prerender_dispatcher_.IsPrerenderURL(url); 37 } 38 39 const std::map<int, WebPrerender>& prerenders() const { 40 return prerender_dispatcher_.prerenders_; 41 } 42 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(); 46 47 prerender_dispatcher_.OnPrerenderStart(g_next_prerender_id); 48 prerender_dispatcher_.OnPrerenderAddAlias(url); 49 return g_next_prerender_id++; 50 } 51 52 void AddAliasToPrerender(const GURL& url) { 53 prerender_dispatcher_.OnPrerenderAddAlias(url); 54 } 55 56 void RemoveAliasFromPrerender(const GURL& url) { 57 std::vector<GURL> urls; 58 urls.push_back(url); 59 prerender_dispatcher_.OnPrerenderRemoveAliases(urls); 60 } 61 62 void StopPrerender(int prerender_id) { 63 prerender_dispatcher_.OnPrerenderStop(prerender_id); 64 } 65 66 int GetCountForURL(const GURL& url) const { 67 return prerender_dispatcher_.running_prerender_urls_.count(url); 68 } 69 70 private: 71 PrerenderDispatcher prerender_dispatcher_; 72 DISALLOW_COPY_AND_ASSIGN(PrerenderDispatcherTest); 73 }; 74 75 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherEmpty) { 76 EXPECT_TRUE(prerenders().empty()); 77 } 78 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)); 85 } 86 87 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleAdd) { 88 GURL foo_url = GURL("http://foo.com"); 89 GURL bar_url = GURL("http://bar.com"); 90 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)); 96 97 AddAliasToPrerender(foo_url); 98 EXPECT_TRUE(IsPrerenderURL(foo_url)); 99 EXPECT_FALSE(IsPrerenderURL(bar_url)); 100 EXPECT_EQ(2, GetCountForURL(foo_url)); 101 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)); 107 } 108 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)); 120 } 121 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)); 136 } 137 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)); 153 } 154 155 } // end namespace prerender 156