summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/prerender/prerender_dispatcher_unittest.cc
blob: f7170ba8b4c6a49c1bb7dc4fed4f8283f2ea5807 (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
// 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 "base/compiler_specific.h"
#include "base/logging.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace prerender {

class PrerenderDispatcherTest : public testing::Test {
 public:
  PrerenderDispatcherTest() {}

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

  const PrerenderDispatcher::PrerenderMap& urls() const {
    return prerender_dispatcher_.prerender_urls_;
  }

  void AddURL(const GURL& url) { prerender_dispatcher_.OnAddPrerenderURL(url); }
  void RemoveURL(const GURL& url) {
    prerender_dispatcher_.OnRemovePrerenderURL(url);
  }

  int GetCountForURL(const GURL& url) const {
    PrerenderDispatcher::PrerenderMap::const_iterator entry = urls().find(url);
    if (entry == urls().end())
      return 0;
    EXPECT_GT(entry->second, 0);
    return entry->second;
  }

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

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherEmpty) {
  EXPECT_EQ(0U, urls().size());
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherSingleAdd) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(is_prerender_url(foo_url));
  AddURL(foo_url);
  EXPECT_TRUE(is_prerender_url(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(is_prerender_url(foo_url));
  EXPECT_FALSE(is_prerender_url(bar_url));
  AddURL(foo_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  EXPECT_FALSE(is_prerender_url(bar_url));

  AddURL(foo_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  EXPECT_FALSE(is_prerender_url(bar_url));
  EXPECT_EQ(2, GetCountForURL(foo_url));

  AddURL(bar_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  EXPECT_TRUE(is_prerender_url(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(is_prerender_url(foo_url));
  AddURL(foo_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  RemoveURL(foo_url);
  EXPECT_FALSE(is_prerender_url(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherMultipleRemove) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(is_prerender_url(foo_url));
  AddURL(foo_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  AddURL(foo_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  EXPECT_EQ(2, GetCountForURL(foo_url));

  RemoveURL(foo_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  EXPECT_EQ(1, GetCountForURL(foo_url));

  RemoveURL(foo_url);
  EXPECT_FALSE(is_prerender_url(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherRemoveWithoutAdd) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(is_prerender_url(foo_url));
  RemoveURL(foo_url);
  EXPECT_FALSE(is_prerender_url(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
}

TEST_F(PrerenderDispatcherTest, PrerenderDispatcherRemoveTooMany) {
  GURL foo_url = GURL("http://foo.com");
  EXPECT_FALSE(is_prerender_url(foo_url));
  AddURL(foo_url);
  EXPECT_TRUE(is_prerender_url(foo_url));
  RemoveURL(foo_url);
  EXPECT_FALSE(is_prerender_url(foo_url));
  RemoveURL(foo_url);
  EXPECT_FALSE(is_prerender_url(foo_url));
  EXPECT_EQ(0, GetCountForURL(foo_url));
}

}  // end namespace prerender