Home | History | Annotate | Download | only in preference
      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/prefs/pref_service.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #include "chrome/browser/browser_process.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/extensions/extension_apitest.h"
     10 #include "chrome/browser/extensions/extension_test_message_listener.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/ui/browser.h"
     13 #include "chrome/common/chrome_switches.h"
     14 #include "chrome/common/pref_names.h"
     15 #include "chrome/test/base/ui_test_utils.h"
     16 #include "content/public/browser/notification_service.h"
     17 
     18 namespace {
     19 
     20 void ReleaseBrowserProcessModule() {
     21   g_browser_process->ReleaseModule();
     22 }
     23 
     24 }  // namespace
     25 
     26 class ExtensionPreferenceApiTest : public ExtensionApiTest {
     27  protected:
     28   ExtensionPreferenceApiTest() : profile_(NULL) {}
     29 
     30   void CheckPreferencesSet() {
     31     PrefService* prefs = profile_->GetPrefs();
     32     const PrefService::Preference* pref = prefs->FindPreference(
     33         prefs::kBlockThirdPartyCookies);
     34     ASSERT_TRUE(pref);
     35     EXPECT_TRUE(pref->IsExtensionControlled());
     36     EXPECT_TRUE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
     37     EXPECT_TRUE(prefs->GetBoolean(autofill::prefs::kAutofillEnabled));
     38     EXPECT_FALSE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
     39     EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableHyperlinkAuditing));
     40     EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableReferrers));
     41     EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableTranslate));
     42     EXPECT_TRUE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
     43     EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
     44     EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
     45   }
     46 
     47   void CheckPreferencesCleared() {
     48     PrefService* prefs = profile_->GetPrefs();
     49     const PrefService::Preference* pref = prefs->FindPreference(
     50         prefs::kBlockThirdPartyCookies);
     51     ASSERT_TRUE(pref);
     52     EXPECT_FALSE(pref->IsExtensionControlled());
     53     EXPECT_FALSE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
     54     EXPECT_FALSE(prefs->GetBoolean(autofill::prefs::kAutofillEnabled));
     55     EXPECT_TRUE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
     56     EXPECT_FALSE(prefs->GetBoolean(prefs::kEnableHyperlinkAuditing));
     57     EXPECT_FALSE(prefs->GetBoolean(prefs::kEnableReferrers));
     58     EXPECT_FALSE(prefs->GetBoolean(prefs::kEnableTranslate));
     59     EXPECT_FALSE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
     60     EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
     61     EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
     62   }
     63 
     64   virtual void SetUpOnMainThread() OVERRIDE {
     65     ExtensionApiTest::SetUpOnMainThread();
     66 
     67     // The browser might get closed later (and therefore be destroyed), so we
     68     // save the profile.
     69     profile_ = browser()->profile();
     70 
     71     // Closing the last browser window also releases a module reference. Make
     72     // sure it's not the last one, so the message loop doesn't quit
     73     // unexpectedly.
     74     g_browser_process->AddRefModule();
     75   }
     76 
     77   virtual void CleanUpOnMainThread() OVERRIDE {
     78     // ReleaseBrowserProcessModule() needs to be called in a message loop, so we
     79     // post a task to do it, then run the message loop.
     80     base::MessageLoop::current()->PostTask(
     81         FROM_HERE, base::Bind(&ReleaseBrowserProcessModule));
     82     content::RunAllPendingInMessageLoop();
     83 
     84     ExtensionApiTest::CleanUpOnMainThread();
     85   }
     86 
     87   Profile* profile_;
     88 };
     89 
     90 // http://crbug.com/177163
     91 #if defined(OS_WIN) && !defined(NDEBUG)
     92 #define MAYBE_Standard DISABLED_Standard
     93 #else
     94 #define MAYBE_Standard Standard
     95 #endif
     96 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, MAYBE_Standard) {
     97   PrefService* prefs = profile_->GetPrefs();
     98   prefs->SetBoolean(prefs::kAlternateErrorPagesEnabled, false);
     99   prefs->SetBoolean(autofill::prefs::kAutofillEnabled, false);
    100   prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true);
    101   prefs->SetBoolean(prefs::kEnableHyperlinkAuditing, false);
    102   prefs->SetBoolean(prefs::kEnableReferrers, false);
    103   prefs->SetBoolean(prefs::kEnableTranslate, false);
    104   prefs->SetBoolean(prefs::kNetworkPredictionEnabled, false);
    105   prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false);
    106   prefs->SetBoolean(prefs::kSearchSuggestEnabled, false);
    107 
    108   const char kExtensionPath[] = "preference/standard";
    109 
    110   EXPECT_TRUE(RunExtensionSubtest(kExtensionPath, "test.html")) << message_;
    111   CheckPreferencesSet();
    112 
    113   // The settings should not be reset when the extension is reloaded.
    114   ReloadExtension(last_loaded_extension_id());
    115   CheckPreferencesSet();
    116 
    117   // Uninstalling and installing the extension (without running the test that
    118   // calls the extension API) should clear the settings.
    119   content::WindowedNotificationObserver observer(
    120       chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
    121       content::NotificationService::AllSources());
    122   UninstallExtension(last_loaded_extension_id());
    123   observer.Wait();
    124   CheckPreferencesCleared();
    125 
    126   LoadExtension(test_data_dir_.AppendASCII(kExtensionPath));
    127   CheckPreferencesCleared();
    128 }
    129 
    130 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, PersistentIncognito) {
    131   PrefService* prefs = profile_->GetPrefs();
    132   prefs->SetBoolean(prefs::kBlockThirdPartyCookies, false);
    133 
    134   EXPECT_TRUE(
    135       RunExtensionTestIncognito("preference/persistent_incognito")) <<
    136       message_;
    137 
    138   // Setting an incognito preference should not create an incognito profile.
    139   EXPECT_FALSE(profile_->HasOffTheRecordProfile());
    140 
    141   PrefService* otr_prefs = profile_->GetOffTheRecordProfile()->GetPrefs();
    142   const PrefService::Preference* pref =
    143       otr_prefs->FindPreference(prefs::kBlockThirdPartyCookies);
    144   ASSERT_TRUE(pref);
    145   EXPECT_TRUE(pref->IsExtensionControlled());
    146   EXPECT_TRUE(otr_prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
    147 
    148   pref = prefs->FindPreference(prefs::kBlockThirdPartyCookies);
    149   ASSERT_TRUE(pref);
    150   EXPECT_FALSE(pref->IsExtensionControlled());
    151   EXPECT_FALSE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
    152 }
    153 
    154 // Flakily times out: http://crbug.com/106144
    155 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, DISABLED_IncognitoDisabled) {
    156   EXPECT_FALSE(RunExtensionTest("preference/persistent_incognito"));
    157 }
    158 
    159 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, SessionOnlyIncognito) {
    160   PrefService* prefs = profile_->GetPrefs();
    161   prefs->SetBoolean(prefs::kBlockThirdPartyCookies, false);
    162 
    163   EXPECT_TRUE(
    164       RunExtensionTestIncognito("preference/session_only_incognito")) <<
    165       message_;
    166 
    167   EXPECT_TRUE(profile_->HasOffTheRecordProfile());
    168 
    169   PrefService* otr_prefs = profile_->GetOffTheRecordProfile()->GetPrefs();
    170   const PrefService::Preference* pref =
    171       otr_prefs->FindPreference(prefs::kBlockThirdPartyCookies);
    172   ASSERT_TRUE(pref);
    173   EXPECT_TRUE(pref->IsExtensionControlled());
    174   EXPECT_FALSE(otr_prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
    175 
    176   pref = prefs->FindPreference(prefs::kBlockThirdPartyCookies);
    177   ASSERT_TRUE(pref);
    178   EXPECT_FALSE(pref->IsExtensionControlled());
    179   EXPECT_FALSE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
    180 }
    181 
    182 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, Clear) {
    183   PrefService* prefs = profile_->GetPrefs();
    184   prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true);
    185 
    186   EXPECT_TRUE(RunExtensionTest("preference/clear")) << message_;
    187 
    188   const PrefService::Preference* pref = prefs->FindPreference(
    189       prefs::kBlockThirdPartyCookies);
    190   ASSERT_TRUE(pref);
    191   EXPECT_FALSE(pref->IsExtensionControlled());
    192   EXPECT_EQ(true, prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
    193 }
    194 
    195 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, OnChange) {
    196   EXPECT_TRUE(RunExtensionTestIncognito("preference/onchange")) <<
    197       message_;
    198 }
    199 
    200 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, OnChangeSplit) {
    201   ResultCatcher catcher;
    202   catcher.RestrictToProfile(profile_);
    203   ResultCatcher catcher_incognito;
    204   catcher_incognito.RestrictToProfile(profile_->GetOffTheRecordProfile());
    205 
    206   // Open an incognito window.
    207   ui_test_utils::OpenURLOffTheRecord(profile_, GURL("chrome://newtab/"));
    208 
    209   // changeDefault listeners.
    210   ExtensionTestMessageListener listener1("changeDefault regular ready", true);
    211   ExtensionTestMessageListener listener_incognito1(
    212       "changeDefault incognito ready", true);
    213 
    214   // changeIncognitoOnly listeners.
    215   ExtensionTestMessageListener listener2(
    216       "changeIncognitoOnly regular ready", true);
    217   ExtensionTestMessageListener listener_incognito2(
    218       "changeIncognitoOnly incognito ready", true);
    219   ExtensionTestMessageListener listener3(
    220       "changeIncognitoOnly regular listening", true);
    221   ExtensionTestMessageListener listener_incognito3(
    222       "changeIncognitoOnly incognito pref set", false);
    223 
    224   // changeDefaultOnly listeners.
    225   ExtensionTestMessageListener listener4(
    226       "changeDefaultOnly regular ready", true);
    227   ExtensionTestMessageListener listener_incognito4(
    228       "changeDefaultOnly incognito ready", true);
    229   ExtensionTestMessageListener listener5(
    230       "changeDefaultOnly regular pref set", false);
    231   ExtensionTestMessageListener listener_incognito5(
    232       "changeDefaultOnly incognito listening", true);
    233 
    234   // changeIncognitoOnlyBack listeners.
    235   ExtensionTestMessageListener listener6(
    236       "changeIncognitoOnlyBack regular ready", true);
    237   ExtensionTestMessageListener listener_incognito6(
    238       "changeIncognitoOnlyBack incognito ready", true);
    239   ExtensionTestMessageListener listener7(
    240       "changeIncognitoOnlyBack regular listening", true);
    241   ExtensionTestMessageListener listener_incognito7(
    242       "changeIncognitoOnlyBack incognito pref set", false);
    243 
    244   // clearIncognito listeners.
    245   ExtensionTestMessageListener listener8(
    246       "clearIncognito regular ready", true);
    247   ExtensionTestMessageListener listener_incognito8(
    248       "clearIncognito incognito ready", true);
    249   ExtensionTestMessageListener listener9(
    250       "clearIncognito regular listening", true);
    251   ExtensionTestMessageListener listener_incognito9(
    252       "clearIncognito incognito pref cleared", false);
    253 
    254   // clearDefault listeners.
    255   ExtensionTestMessageListener listener10(
    256       "clearDefault regular ready", true);
    257   ExtensionTestMessageListener listener_incognito10(
    258       "clearDefault incognito ready", true);
    259 
    260   base::FilePath extension_data_dir =
    261       test_data_dir_.AppendASCII("preference").AppendASCII("onchange_split");
    262   ASSERT_TRUE(LoadExtensionIncognito(extension_data_dir));
    263 
    264   // Test 1 - changeDefault
    265   EXPECT_TRUE(listener1.WaitUntilSatisfied()); // Regular ready
    266   EXPECT_TRUE(listener_incognito1.WaitUntilSatisfied()); // Incognito ready
    267   listener1.Reply("ok");
    268   listener_incognito1.Reply("ok");
    269 
    270   // Test 2 - changeIncognitoOnly
    271   EXPECT_TRUE(listener2.WaitUntilSatisfied()); // Regular ready
    272   EXPECT_TRUE(listener_incognito2.WaitUntilSatisfied()); // Incognito ready
    273   EXPECT_TRUE(listener3.WaitUntilSatisfied()); // Regular listening
    274   listener2.Reply("ok");
    275   listener_incognito2.Reply("ok");
    276   // Incognito preference set -- notify the regular listener
    277   EXPECT_TRUE(listener_incognito3.WaitUntilSatisfied());
    278   listener3.Reply("ok");
    279 
    280   // Test 3 - changeDefaultOnly
    281   EXPECT_TRUE(listener4.WaitUntilSatisfied()); // Regular ready
    282   EXPECT_TRUE(listener_incognito4.WaitUntilSatisfied()); // Incognito ready
    283   EXPECT_TRUE(listener_incognito5.WaitUntilSatisfied()); // Incognito listening
    284   listener4.Reply("ok");
    285   listener_incognito4.Reply("ok");
    286   // Regular preference set - notify the incognito listener
    287   EXPECT_TRUE(listener5.WaitUntilSatisfied());
    288   listener_incognito5.Reply("ok");
    289 
    290   // Test 4 - changeIncognitoOnlyBack
    291   EXPECT_TRUE(listener6.WaitUntilSatisfied()); // Regular ready
    292   EXPECT_TRUE(listener_incognito6.WaitUntilSatisfied()); // Incognito ready
    293   EXPECT_TRUE(listener7.WaitUntilSatisfied()); // Regular listening
    294   listener6.Reply("ok");
    295   listener_incognito6.Reply("ok");
    296   // Incognito preference set -- notify the regular listener
    297   EXPECT_TRUE(listener_incognito7.WaitUntilSatisfied());
    298   listener7.Reply("ok");
    299 
    300   // Test 5 - clearIncognito
    301   EXPECT_TRUE(listener8.WaitUntilSatisfied()); // Regular ready
    302   EXPECT_TRUE(listener_incognito8.WaitUntilSatisfied()); // Incognito ready
    303   EXPECT_TRUE(listener9.WaitUntilSatisfied()); // Regular listening
    304   listener8.Reply("ok");
    305   listener_incognito8.Reply("ok");
    306   // Incognito preference cleared -- notify the regular listener
    307   EXPECT_TRUE(listener_incognito9.WaitUntilSatisfied());
    308   listener9.Reply("ok");
    309 
    310   // Test 6 - clearDefault
    311   EXPECT_TRUE(listener10.WaitUntilSatisfied()); // Regular ready
    312   EXPECT_TRUE(listener_incognito10.WaitUntilSatisfied()); // Incognito ready
    313   listener10.Reply("ok");
    314   listener_incognito10.Reply("ok");
    315 
    316   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
    317   EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
    318 }
    319