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 "base/message_loop/message_loop.h"
6 #include "chrome/browser/prerender/prerender_manager.h"
7 #include "content/public/test/test_browser_thread.h"
8 #include "testing/gtest/include/gtest/gtest.h"
15 // These tests don't require the dummy PrerenderContents & PrerenderManager that
16 // the heavier PrerenderTest.* tests do.
17 class PrerenderManagerTest
: public testing::Test
{
19 PrerenderManagerTest()
20 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
21 prerender_manager_(NULL
, NULL
) {
24 PrerenderManager
* prerender_manager() { return &prerender_manager_
; }
26 base::MessageLoop message_loop_
;
27 content::TestBrowserThread ui_thread_
;
28 PrerenderManager prerender_manager_
;
31 TEST_F(PrerenderManagerTest
, IsWebContentsPrerenderedTest
) {
32 // The methods being tested should not dereference their WebContents, instead
33 // they use it as a unique identifier.
34 int not_a_webcontents
= 0;
35 content::WebContents
* web_contents
=
36 reinterpret_cast<content::WebContents
*>(¬_a_webcontents
);
38 EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
41 const Origin origin
= ORIGIN_OMNIBOX
;
42 prerender_manager()->MarkWebContentsAsPrerendered(web_contents
, origin
);
44 Origin test_origin
= ORIGIN_NONE
;
45 EXPECT_TRUE(prerender_manager()->IsWebContentsPrerendered(
46 web_contents
, &test_origin
));
48 int also_not_a_webcontents
= 1;
49 content::WebContents
* another_web_contents
=
50 reinterpret_cast<content::WebContents
*>(&also_not_a_webcontents
);
51 EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
52 another_web_contents
, NULL
));
54 EXPECT_EQ(origin
, test_origin
);
56 prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents
);
57 EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
61 TEST_F(PrerenderManagerTest
, WouldWebContentsBePrerenderedTest
) {
62 // The methods being tested should not dereference their WebContents, instead
63 // they use it as a unique identifier.
64 int not_a_webcontents
= 0;
65 content::WebContents
* web_contents
=
66 reinterpret_cast<content::WebContents
*>(¬_a_webcontents
);
68 EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
71 const Origin origin
= ORIGIN_OMNIBOX
;
72 prerender_manager()->MarkWebContentsAsWouldBePrerendered(web_contents
,
75 Origin test_origin
= ORIGIN_NONE
;
76 EXPECT_TRUE(prerender_manager()->WouldWebContentsBePrerendered(
77 web_contents
, &test_origin
));
78 EXPECT_EQ(origin
, test_origin
);
80 int also_not_a_webcontents
= 1;
81 content::WebContents
* another_web_contents
=
82 reinterpret_cast<content::WebContents
*>(&also_not_a_webcontents
);
83 EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
84 another_web_contents
, NULL
));
86 // Control group (aka WouldBe...) web_contents need to be removed twice. See
87 // the comment in prerender_manager.cc at the definition of
88 // WouldBePrerenderedWebContentsData and its inner enum State.
89 prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents
);
90 EXPECT_TRUE(prerender_manager()->WouldWebContentsBePrerendered(
93 prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents
);
94 EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
100 } // namespace prerender