Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/prerender/prerender_dispatcher.h" | 5 #include "chrome/renderer/prerender/prerender_dispatcher.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 #include <utility> | |
| 9 | |
| 7 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 9 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 14 |
| 12 namespace prerender { | 15 namespace prerender { |
| 13 | 16 |
| 17 namespace { | |
| 18 | |
| 19 int g_next_prerender_id = 0; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 using WebKit::WebPrerender; | |
| 24 | |
| 25 // Since we can't mock out WebKit::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. | |
| 14 class PrerenderDispatcherTest : public testing::Test { | 31 class PrerenderDispatcherTest : public testing::Test { |
| 15 public: | 32 public: |
| 16 PrerenderDispatcherTest() {} | 33 PrerenderDispatcherTest() {} |
| 17 | 34 |
| 18 bool is_prerender_url(const GURL& url) const { | 35 bool IsPrerenderURL(const GURL& url) const { |
| 19 return prerender_dispatcher_.IsPrerenderURL(url); | 36 return prerender_dispatcher_.IsPrerenderURL(url); |
| 20 } | 37 } |
| 21 | 38 |
| 22 const PrerenderDispatcher::PrerenderMap& urls() const { | 39 const std::map<int, GURL>& prerenders() const { |
| 23 return prerender_dispatcher_.prerender_urls_; | 40 return prerender_dispatcher_.prerenders_; |
| 24 } | 41 } |
| 25 | 42 |
| 26 void AddURL(const GURL& url) { prerender_dispatcher_.OnAddPrerenderURL(url); } | 43 int StartPrerender(const GURL& url) { |
| 27 void RemoveURL(const GURL& url) { | 44 std::pair<std::map<int, GURL>::iterator, bool> insert_result = |
| 28 prerender_dispatcher_.OnRemovePrerenderURL(url); | 45 prerender_dispatcher_.prerenders_.insert( |
| 46 std::make_pair(g_next_prerender_id, url)); | |
| 47 CHECK(insert_result.second); | |
|
mmenke
2012/12/12 16:54:12
I think that the following is much clearer:
DCHEC
gavinp
2012/12/13 13:38:03
Done. You are slowly winning me over to your crazy
| |
| 48 | |
| 49 prerender_dispatcher_.OnPrerenderStart(g_next_prerender_id); | |
| 50 return g_next_prerender_id++; | |
| 51 } | |
| 52 | |
| 53 void AddAliasToPrerender(int prerender_id, const GURL& url) { | |
| 54 prerender_dispatcher_.OnPrerenderAddAlias(prerender_id, url); | |
| 55 } | |
| 56 | |
| 57 void StopPrerender(int prerender_id) { | |
| 58 prerender_dispatcher_.OnPrerenderStop(prerender_id); | |
| 29 } | 59 } |
| 30 | 60 |
| 31 int GetCountForURL(const GURL& url) const { | 61 int GetCountForURL(const GURL& url) const { |
| 32 PrerenderDispatcher::PrerenderMap::const_iterator entry = urls().find(url); | 62 return prerender_dispatcher_.running_prerender_urls_.count(url); |
| 33 if (entry == urls().end()) | |
| 34 return 0; | |
| 35 EXPECT_GT(entry->second, 0); | |
| 36 return entry->second; | |
| 37 } | 63 } |
| 38 | 64 |
| 39 private: | 65 private: |
| 40 PrerenderDispatcher prerender_dispatcher_; | 66 PrerenderDispatcher prerender_dispatcher_; |
| 41 DISALLOW_COPY_AND_ASSIGN(PrerenderDispatcherTest); | 67 DISALLOW_COPY_AND_ASSIGN(PrerenderDispatcherTest); |
| 42 }; | 68 }; |
| 43 | 69 |
| 44 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherEmpty) { | 70 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherEmpty) { |
| 45 EXPECT_EQ(0U, urls().size()); | 71 EXPECT_TRUE(prerenders().empty()); |
| 46 } | 72 } |
| 47 | 73 |
| 48 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherSingleAdd) { | 74 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherSingleAdd) { |
| 49 GURL foo_url = GURL("http://foo.com"); | 75 GURL foo_url = GURL("http://foo.com"); |
| 50 EXPECT_FALSE(is_prerender_url(foo_url)); | 76 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 51 AddURL(foo_url); | 77 StartPrerender(foo_url); |
| 52 EXPECT_TRUE(is_prerender_url(foo_url)); | 78 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 53 EXPECT_EQ(1, GetCountForURL(foo_url)); | 79 EXPECT_EQ(1, GetCountForURL(foo_url)); |
| 54 } | 80 } |
| 55 | 81 |
| 56 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleAdd) { | 82 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleAdd) { |
| 57 GURL foo_url = GURL("http://foo.com"); | 83 GURL foo_url = GURL("http://foo.com"); |
| 58 GURL bar_url = GURL("http://bar.com"); | 84 GURL bar_url = GURL("http://bar.com"); |
| 59 | 85 |
| 60 EXPECT_FALSE(is_prerender_url(foo_url)); | 86 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 61 EXPECT_FALSE(is_prerender_url(bar_url)); | 87 EXPECT_FALSE(IsPrerenderURL(bar_url)); |
| 62 AddURL(foo_url); | 88 int foo_id = StartPrerender(foo_url); |
| 63 EXPECT_TRUE(is_prerender_url(foo_url)); | 89 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 64 EXPECT_FALSE(is_prerender_url(bar_url)); | 90 EXPECT_FALSE(IsPrerenderURL(bar_url)); |
| 65 | 91 |
| 66 AddURL(foo_url); | 92 AddAliasToPrerender(foo_id, foo_url); |
| 67 EXPECT_TRUE(is_prerender_url(foo_url)); | 93 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 68 EXPECT_FALSE(is_prerender_url(bar_url)); | 94 EXPECT_FALSE(IsPrerenderURL(bar_url)); |
| 69 EXPECT_EQ(2, GetCountForURL(foo_url)); | 95 EXPECT_EQ(2, GetCountForURL(foo_url)); |
| 70 | 96 |
| 71 AddURL(bar_url); | 97 StartPrerender(bar_url); |
| 72 EXPECT_TRUE(is_prerender_url(foo_url)); | 98 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 73 EXPECT_TRUE(is_prerender_url(bar_url)); | 99 EXPECT_TRUE(IsPrerenderURL(bar_url)); |
| 74 EXPECT_EQ(2, GetCountForURL(foo_url)); | 100 EXPECT_EQ(2, GetCountForURL(foo_url)); |
| 75 EXPECT_EQ(1, GetCountForURL(bar_url)); | 101 EXPECT_EQ(1, GetCountForURL(bar_url)); |
| 76 } | 102 } |
| 77 | 103 |
| 78 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherSingleRemove) { | 104 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherSingleRemove) { |
| 79 GURL foo_url = GURL("http://foo.com"); | 105 GURL foo_url = GURL("http://foo.com"); |
| 80 EXPECT_FALSE(is_prerender_url(foo_url)); | 106 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 81 AddURL(foo_url); | 107 int foo_id = StartPrerender(foo_url); |
| 82 EXPECT_TRUE(is_prerender_url(foo_url)); | 108 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 83 RemoveURL(foo_url); | 109 StopPrerender(foo_id); |
| 84 EXPECT_FALSE(is_prerender_url(foo_url)); | 110 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 85 EXPECT_EQ(0, GetCountForURL(foo_url)); | 111 EXPECT_EQ(0, GetCountForURL(foo_url)); |
| 86 } | 112 } |
| 87 | 113 |
| 88 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleRemove) { | 114 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleRemove) { |
| 89 GURL foo_url = GURL("http://foo.com"); | 115 GURL foo_url = GURL("http://foo.com"); |
| 90 EXPECT_FALSE(is_prerender_url(foo_url)); | 116 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 91 AddURL(foo_url); | 117 int foo_id = StartPrerender(foo_url); |
| 92 EXPECT_TRUE(is_prerender_url(foo_url)); | 118 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 93 AddURL(foo_url); | 119 AddAliasToPrerender(foo_id, foo_url); |
| 94 EXPECT_TRUE(is_prerender_url(foo_url)); | 120 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 95 EXPECT_EQ(2, GetCountForURL(foo_url)); | 121 EXPECT_EQ(2, GetCountForURL(foo_url)); |
| 96 | 122 |
|
mmenke
2012/12/12 16:54:12
May want to add a second url as well.
gavinp
2012/12/13 13:38:03
Throwing out the test, as we seem to be able to DC
| |
| 97 RemoveURL(foo_url); | 123 StopPrerender(foo_id); |
| 98 EXPECT_TRUE(is_prerender_url(foo_url)); | 124 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 99 EXPECT_EQ(1, GetCountForURL(foo_url)); | |
| 100 | |
| 101 RemoveURL(foo_url); | |
| 102 EXPECT_FALSE(is_prerender_url(foo_url)); | |
| 103 EXPECT_EQ(0, GetCountForURL(foo_url)); | 125 EXPECT_EQ(0, GetCountForURL(foo_url)); |
| 104 } | 126 } |
| 105 | 127 |
| 106 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherRemoveWithoutAdd) { | 128 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherRemoveWithoutAdd) { |
|
mmenke
2012/12/12 16:54:12
Does this ever happen?
gavinp
2012/12/13 13:38:03
I don't think so.
| |
| 107 GURL foo_url = GURL("http://foo.com"); | 129 GURL foo_url = GURL("http://foo.com"); |
| 108 EXPECT_FALSE(is_prerender_url(foo_url)); | 130 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 109 RemoveURL(foo_url); | 131 int crazy_prerender_id = 13; |
| 110 EXPECT_FALSE(is_prerender_url(foo_url)); | 132 StopPrerender(crazy_prerender_id); |
| 133 EXPECT_FALSE(IsPrerenderURL(foo_url)); | |
| 111 EXPECT_EQ(0, GetCountForURL(foo_url)); | 134 EXPECT_EQ(0, GetCountForURL(foo_url)); |
| 112 } | 135 } |
| 113 | 136 |
| 114 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherRemoveTooMany) { | 137 TEST_F(PrerenderDispatcherTest, PrerenderDispatcherRemoveTooMany) { |
|
mmenke
2012/12/12 16:54:12
Does this ever happen?
gavinp
2012/12/13 13:38:03
I don't think so.
| |
| 115 GURL foo_url = GURL("http://foo.com"); | 138 GURL foo_url = GURL("http://foo.com"); |
| 116 EXPECT_FALSE(is_prerender_url(foo_url)); | 139 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 117 AddURL(foo_url); | 140 int foo_id = StartPrerender(foo_url); |
| 118 EXPECT_TRUE(is_prerender_url(foo_url)); | 141 EXPECT_TRUE(IsPrerenderURL(foo_url)); |
| 119 RemoveURL(foo_url); | 142 StopPrerender(foo_id); |
| 120 EXPECT_FALSE(is_prerender_url(foo_url)); | 143 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 121 RemoveURL(foo_url); | 144 StopPrerender(foo_id); |
| 122 EXPECT_FALSE(is_prerender_url(foo_url)); | 145 EXPECT_FALSE(IsPrerenderURL(foo_url)); |
| 123 EXPECT_EQ(0, GetCountForURL(foo_url)); | 146 EXPECT_EQ(0, GetCountForURL(foo_url)); |
| 124 } | 147 } |
| 125 | 148 |
| 126 } // end namespace prerender | 149 } // end namespace prerender |
| OLD | NEW |