Home | History | Annotate | Download | only in search
      1 // Copyright 2013 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/memory/scoped_ptr.h"
      6 #include "base/prefs/pref_service.h"
      7 #include "chrome/browser/content_settings/host_content_settings_map.h"
      8 #include "chrome/browser/search/instant_service_factory.h"
      9 #include "chrome/browser/search/search.h"
     10 #include "chrome/browser/ui/search/instant_ntp.h"
     11 #include "chrome/browser/ui/search/instant_ntp_prerenderer.h"
     12 #include "chrome/common/content_settings.h"
     13 #include "chrome/common/pref_names.h"
     14 #include "chrome/test/base/testing_profile.h"
     15 #include "content/public/test/test_browser_thread_bundle.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 class TestableInstantNTP : public InstantNTP {
     19  public:
     20   TestableInstantNTP(InstantNTPPrerenderer* ntp_prerenderer,
     21                      const std::string& instant_url,
     22                      Profile* profile)
     23       : InstantNTP(ntp_prerenderer, "UNUSED", profile),
     24         test_instant_url_(instant_url),
     25         test_supports_instant_(true),
     26         test_is_local_(false) {
     27   }
     28 
     29   // Overrides from InstantPage
     30   virtual bool supports_instant() const OVERRIDE {
     31     return test_supports_instant_;
     32   }
     33 
     34   virtual bool IsLocal() const OVERRIDE {
     35     return test_is_local_;
     36   };
     37 
     38   virtual const std::string& instant_url() const OVERRIDE {
     39     return test_instant_url_;
     40   }
     41 
     42   void set_instant_url(const std::string& instant_url) {
     43     test_instant_url_ = instant_url;
     44   }
     45 
     46   void set_supports_instant(bool supports_instant) {
     47     test_supports_instant_ = supports_instant;
     48   }
     49 
     50   void set_is_local(bool is_local) {
     51     test_is_local_ = is_local;
     52   }
     53 
     54  private:
     55   std::string test_instant_url_;
     56   bool test_supports_instant_;
     57   bool test_is_local_;
     58 };
     59 
     60 class TestableInstantNTPPrerenderer : public InstantNTPPrerenderer {
     61  public:
     62   explicit TestableInstantNTPPrerenderer(TestingProfile* profile,
     63       InstantService* instant_service)
     64       : InstantNTPPrerenderer(profile, instant_service, NULL),
     65         test_instant_url_("http://test_url"),
     66         override_javascript_enabled_(true),
     67         test_javascript_enabled_(true),
     68         test_in_startup_(false),
     69         test_ntp_(NULL) {
     70   }
     71 
     72   // Overrides from InstantNTPPrerenderer
     73   virtual std::string GetInstantURL() const OVERRIDE {
     74     return test_instant_url_;
     75   }
     76 
     77   virtual std::string GetLocalInstantURL() const OVERRIDE {
     78     return "http://local_instant_url";
     79   }
     80 
     81   virtual InstantNTP* ntp() const OVERRIDE {
     82     return test_ntp_;
     83   }
     84 
     85   virtual bool IsJavascriptEnabled() const OVERRIDE {
     86     if (override_javascript_enabled_)
     87       return test_javascript_enabled_;
     88     else
     89       return InstantNTPPrerenderer::IsJavascriptEnabled();
     90   }
     91 
     92   virtual bool InStartup() const OVERRIDE {
     93     return test_in_startup_;
     94   }
     95 
     96   void set_instant_url(const std::string& instant_url) {
     97     test_instant_url_ = instant_url;
     98   }
     99 
    100   void set_ntp(InstantNTP* ntp) {
    101     test_ntp_ = ntp;
    102   }
    103 
    104   void set_javascript_enabled(bool javascript_enabled) {
    105     override_javascript_enabled_ = true;
    106     test_javascript_enabled_ = javascript_enabled;
    107   }
    108 
    109   void set_override_javascript_enabled(bool override_javascript_enabled) {
    110     override_javascript_enabled_ = override_javascript_enabled;
    111   }
    112 
    113   void set_in_startup(bool in_startup) {
    114     test_in_startup_ = in_startup;
    115   }
    116 
    117 private:
    118   std::string test_instant_url_;
    119   bool override_javascript_enabled_;
    120   bool test_javascript_enabled_;
    121   bool test_in_startup_;
    122   InstantNTP* test_ntp_;
    123 };
    124 
    125 class InstantNTPPrerendererTest : public testing::Test {
    126  public:
    127   virtual void SetUp() OVERRIDE {
    128     instant_service_ = InstantServiceFactory::GetForProfile(&profile_);
    129     instant_ntp_prerenderer_.reset(
    130         new TestableInstantNTPPrerenderer(&profile_, instant_service_));
    131   }
    132 
    133   virtual void TearDown() OVERRIDE {
    134     instant_ntp_prerenderer_.reset();
    135   }
    136 
    137   TestableInstantNTPPrerenderer* instant_ntp_prerenderer() {
    138     return instant_ntp_prerenderer_.get();
    139   }
    140 
    141   Profile* profile() {
    142     return instant_ntp_prerenderer()->profile();
    143   }
    144 
    145  private:
    146   content::TestBrowserThreadBundle thread_bundle_;
    147   scoped_ptr<TestableInstantNTPPrerenderer> instant_ntp_prerenderer_;
    148   InstantService* instant_service_;
    149   mutable TestingProfile profile_;
    150 };
    151 
    152 TEST_F(InstantNTPPrerendererTest, PrefersRemoteNTPOnStartup) {
    153   std::string instant_url("http://instant_url");
    154   scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP(
    155       instant_ntp_prerenderer(), instant_url, profile()));
    156   instant_ntp_prerenderer()->set_ntp(ntp.get());
    157   instant_ntp_prerenderer()->set_instant_url(instant_url);
    158   instant_ntp_prerenderer()->set_in_startup(true);
    159   EXPECT_FALSE(instant_ntp_prerenderer()->ShouldSwitchToLocalNTP());
    160 }
    161 
    162 TEST_F(InstantNTPPrerendererTest, SwitchesToLocalNTPIfNoInstantSupport) {
    163   std::string instant_url("http://instant_url");
    164   scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP(
    165       instant_ntp_prerenderer(), instant_url, profile()));
    166   instant_ntp_prerenderer()->set_ntp(ntp.get());
    167   instant_ntp_prerenderer()->set_instant_url(instant_url);
    168   ntp->set_supports_instant(false);
    169   EXPECT_TRUE(instant_ntp_prerenderer()->ShouldSwitchToLocalNTP());
    170 }
    171 
    172 TEST_F(InstantNTPPrerendererTest, SwitchesToLocalNTPIfPathBad) {
    173   std::string instant_url("http://instant_url");
    174   scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP(
    175       instant_ntp_prerenderer(), instant_url, profile()));
    176   instant_ntp_prerenderer()->set_ntp(ntp.get());
    177   instant_ntp_prerenderer()->set_instant_url("http://bogus_url");
    178   EXPECT_TRUE(instant_ntp_prerenderer()->ShouldSwitchToLocalNTP());
    179 }
    180 
    181 TEST_F(InstantNTPPrerendererTest, DoesNotSwitchToLocalNTPIfOnCurrentNTP) {
    182   std::string instant_url("http://instant_url");
    183   scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP(
    184       instant_ntp_prerenderer(), instant_url, profile()));
    185   instant_ntp_prerenderer()->set_ntp(ntp.get());
    186   instant_ntp_prerenderer()->set_instant_url(instant_url);
    187   EXPECT_FALSE(instant_ntp_prerenderer()->ShouldSwitchToLocalNTP());
    188 }
    189 
    190 TEST_F(InstantNTPPrerendererTest, DoesNotSwitchToLocalNTPIfOnLocalNTP) {
    191   std::string instant_url("http://instant_url");
    192   scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP(
    193       instant_ntp_prerenderer(), instant_url, profile()));
    194   instant_ntp_prerenderer()->set_ntp(ntp.get());
    195   instant_ntp_prerenderer()->set_instant_url(instant_url);
    196   ntp->set_instant_url("http://local_instant_url");
    197   EXPECT_FALSE(instant_ntp_prerenderer()->ShouldSwitchToLocalNTP());
    198 }
    199 
    200 TEST_F(InstantNTPPrerendererTest, SwitchesToLocalNTPIfJSDisabled) {
    201   std::string instant_url("http://instant_url");
    202   scoped_ptr<TestableInstantNTP> ntp(new TestableInstantNTP(
    203       instant_ntp_prerenderer(), instant_url, profile()));
    204   instant_ntp_prerenderer()->set_ntp(ntp.get());
    205   instant_ntp_prerenderer()->set_javascript_enabled(false);
    206   instant_ntp_prerenderer()->set_instant_url(instant_url);
    207   ntp->set_instant_url("http://local_instant_url");
    208   EXPECT_TRUE(instant_ntp_prerenderer()->ShouldSwitchToLocalNTP());
    209 }
    210 
    211 TEST_F(InstantNTPPrerendererTest, SwitchesToLocalNTPIfNoNTPReady) {
    212   EXPECT_TRUE(instant_ntp_prerenderer()->ShouldSwitchToLocalNTP());
    213 }
    214 
    215 TEST_F(InstantNTPPrerendererTest, IsJavascriptEnabled) {
    216   instant_ntp_prerenderer()->set_override_javascript_enabled(false);
    217   EXPECT_TRUE(instant_ntp_prerenderer()->IsJavascriptEnabled());
    218 }
    219 
    220 TEST_F(InstantNTPPrerendererTest, IsJavascriptEnabledChecksContentSettings) {
    221   instant_ntp_prerenderer()->set_override_javascript_enabled(false);
    222   instant_ntp_prerenderer()->profile()->GetHostContentSettingsMap()
    223       ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT,
    224                                  CONTENT_SETTING_DEFAULT);
    225   EXPECT_TRUE(instant_ntp_prerenderer()->IsJavascriptEnabled());
    226   instant_ntp_prerenderer()->profile()->GetHostContentSettingsMap()
    227       ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT,
    228                                  CONTENT_SETTING_ALLOW);
    229   EXPECT_TRUE(instant_ntp_prerenderer()->IsJavascriptEnabled());
    230   instant_ntp_prerenderer()->profile()->GetHostContentSettingsMap()
    231       ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT,
    232                                  CONTENT_SETTING_BLOCK);
    233   EXPECT_FALSE(instant_ntp_prerenderer()->IsJavascriptEnabled());
    234 }
    235 
    236 TEST_F(InstantNTPPrerendererTest, IsJavascriptEnabledChecksPrefs) {
    237   instant_ntp_prerenderer()->set_override_javascript_enabled(false);
    238   instant_ntp_prerenderer()->profile()->GetPrefs()->SetBoolean(
    239       prefs::kWebKitJavascriptEnabled, true);
    240   EXPECT_TRUE(instant_ntp_prerenderer()->IsJavascriptEnabled());
    241   instant_ntp_prerenderer()->profile()->GetPrefs()->SetBoolean(
    242       prefs::kWebKitJavascriptEnabled, false);
    243   EXPECT_FALSE(instant_ntp_prerenderer()->IsJavascriptEnabled());
    244 }
    245