Home | History | Annotate | Download | only in prerender
      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 "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"
      9 
     10 
     11 namespace prerender {
     12 
     13 namespace {
     14 
     15 // These tests don't require the dummy PrerenderContents & PrerenderManager that
     16 // the heavier PrerenderTest.* tests do.
     17 class PrerenderManagerTest : public testing::Test {
     18  public:
     19   PrerenderManagerTest()
     20       : ui_thread_(content::BrowserThread::UI, &message_loop_),
     21         prerender_manager_(NULL, NULL) {
     22   }
     23 
     24   PrerenderManager* prerender_manager() { return &prerender_manager_; }
     25 
     26   base::MessageLoop message_loop_;
     27   content::TestBrowserThread ui_thread_;
     28   PrerenderManager prerender_manager_;
     29 };
     30 
     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*>(&not_a_webcontents);
     37 
     38   EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
     39       web_contents, NULL));
     40 
     41   const Origin origin = ORIGIN_OMNIBOX;
     42   prerender_manager()->MarkWebContentsAsPrerendered(web_contents, origin);
     43 
     44   Origin test_origin = ORIGIN_NONE;
     45   EXPECT_TRUE(prerender_manager()->IsWebContentsPrerendered(
     46       web_contents, &test_origin));
     47 
     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));
     53 
     54   EXPECT_EQ(origin, test_origin);
     55 
     56   prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents);
     57   EXPECT_FALSE(prerender_manager()->IsWebContentsPrerendered(
     58       web_contents, NULL));
     59 }
     60 
     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*>(&not_a_webcontents);
     67 
     68   EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
     69       web_contents, NULL));
     70 
     71   const Origin origin = ORIGIN_OMNIBOX;
     72   prerender_manager()->MarkWebContentsAsWouldBePrerendered(web_contents,
     73                                                            origin);
     74 
     75   Origin test_origin = ORIGIN_NONE;
     76   EXPECT_TRUE(prerender_manager()->WouldWebContentsBePrerendered(
     77       web_contents, &test_origin));
     78   EXPECT_EQ(origin, test_origin);
     79 
     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));
     85 
     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(
     91       web_contents, NULL));
     92 
     93   prerender_manager()->MarkWebContentsAsNotPrerendered(web_contents);
     94   EXPECT_FALSE(prerender_manager()->WouldWebContentsBePrerendered(
     95       web_contents, NULL));
     96 }
     97 
     98 }  // namespace
     99 
    100 }  // namespace prerender
    101