summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prerender/prerender_manager_unittest.cc
blob: 86d6f33f444435e9bbe3de5d2259796c088f20dd (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
// 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 "base/message_loop.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "content/public/test/test_browser_thread.h"


namespace prerender {

namespace {

// These tests don't require the dummy PrerenderContents & PrerenderManager that
// the heavier PrerenderTest.* tests do.
class PrerenderManagerTest : public testing::Test {
 public:
  PrerenderManagerTest()
      : ui_thread_(content::BrowserThread::UI, &message_loop_),
        prerender_manager_(NULL, NULL) {
  }

  PrerenderManager* prerender_manager() { return &prerender_manager_; }

  MessageLoop message_loop_;
  content::TestBrowserThread ui_thread_;
  PrerenderManager prerender_manager_;
};

TEST_F(PrerenderManagerTest, IsWebContentsPrerenderedTest) {
  // The methods being tested should not dereference their WebContents, instead
  // they use it as a unique identifier.
  int not_a_webcontents = 0;
  content::WebContents* web_contents =
      reinterpret_cast<content::WebContents*>(&not_a_webcontents);

  EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
      web_contents, NULL));

  const Origin origin = ORIGIN_OMNIBOX;
  prerender_manager()->MarkWebContentsAsPrerendered(web_contents, origin);

  Origin test_origin = ORIGIN_NONE;
  EXPECT_TRUE(prerender_manager()->IsWebContentsPrerendered(
      web_contents, &test_origin));

  int also_not_a_webcontents = 1;
  content::WebContents* another_web_contents =
      reinterpret_cast<content::WebContents*>(&also_not_a_webcontents);
  EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
      another_web_contents, NULL));

  EXPECT_EQ(origin, test_origin);

  prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents);
  EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
      web_contents, NULL));
}

TEST_F(PrerenderManagerTest, WouldWebContentsBePrerenderedTest) {
  // The methods being tested should not dereference their WebContents, instead
  // they use it as a unique identifier.
  int not_a_webcontents = 0;
  content::WebContents* web_contents =
      reinterpret_cast<content::WebContents*>(&not_a_webcontents);

  EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
      web_contents, NULL));

  const Origin origin = ORIGIN_OMNIBOX;
  prerender_manager()->MarkWebContentsAsWouldBePrerendered(web_contents,
                                                           origin);

  Origin test_origin = ORIGIN_NONE;
  EXPECT_TRUE(prerender_manager()->WouldWebContentsBePrerendered(
      web_contents, &test_origin));
  EXPECT_EQ(origin, test_origin);

  int also_not_a_webcontents = 1;
  content::WebContents* another_web_contents =
      reinterpret_cast<content::WebContents*>(&also_not_a_webcontents);
  EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
      another_web_contents, NULL));

  // Control group (aka WouldBe...) web_contents need to be removed twice. See
  // the comment in prerender_manager.cc at the definition of
  // WouldBePrerenderedWebContentsData and its inner enum State.
  prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents);
  EXPECT_TRUE(prerender_manager()->WouldWebContentsBePrerendered(
      web_contents, NULL));

  prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents);
  EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
      web_contents, NULL));
}

}  // namespace

}  // namespace prerender