17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include <string>
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/ref_counted.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/prefs/mock_pref_change_callback.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/values.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/./extension_prefs_unittest.h"
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/extensions/api/preference/preference_api.h"
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_prefs.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)using base::Value;
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace extensions {
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace {
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kPref1[] = "path1.subpath";
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kPref2[] = "path2";
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kPref3[] = "path3";
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kPref4[] = "path4";
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Default values in case an extension pref value is not overridden.
327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kDefaultPref1[] = "default pref 1";
337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kDefaultPref2[] = "default pref 2";
347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kDefaultPref3[] = "default pref 3";
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kDefaultPref4[] = "default pref 4";
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// An implementation of the PreferenceAPI which returns the ExtensionPrefs and
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// ExtensionPrefValueMap from the TestExtensionPrefs, rather than from a
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// profile (which we don't create in unittests).
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class TestPreferenceAPI : public PreferenceAPIBase {
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
44e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  explicit TestPreferenceAPI(TestExtensionPrefs* test_extension_prefs,
45e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                             ContentSettingsService* content_settings)
46e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      : test_extension_prefs_(test_extension_prefs),
47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch        content_settings_(content_settings) {}
48e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  ~TestPreferenceAPI() {}
49e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) private:
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // PreferenceAPIBase implementation.
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ExtensionPrefs* extension_prefs() OVERRIDE {
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return test_extension_prefs_->prefs();
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ExtensionPrefValueMap* extension_pref_value_map() OVERRIDE {
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return test_extension_prefs_->extension_pref_value_map();
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual scoped_refptr<ContentSettingsStore> content_settings_store()
59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      OVERRIDE {
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    return content_settings_->content_settings_store();
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TestExtensionPrefs* test_extension_prefs_;
64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  ContentSettingsService* content_settings_;
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestPreferenceAPI);
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ExtensionControlledPrefsTest : public PrefsPrepopulatedTestBase {
707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ExtensionControlledPrefsTest();
727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ~ExtensionControlledPrefsTest();
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void RegisterPreferences(user_prefs::PrefRegistrySyncable* registry)
757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      OVERRIDE;
767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void InstallExtensionControlledPref(Extension* extension,
777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      const std::string& key,
787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      base::Value* value);
797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void InstallExtensionControlledPrefIncognito(Extension* extension,
807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                               const std::string& key,
817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                               base::Value* value);
827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void InstallExtensionControlledPrefIncognitoSessionOnly(
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      Extension* extension,
847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const std::string& key,
857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      base::Value* value);
867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void InstallExtension(Extension* extension);
877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void UninstallExtension(const std::string& extension_id);
887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
89e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  scoped_refptr<ContentSettingsStore> content_settings_store() {
90e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    return content_settings_->content_settings_store();
91e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) protected:
947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void EnsureExtensionInstalled(Extension* extension);
957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void EnsureExtensionUninstalled(const std::string& extension_id);
967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
97e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  TestingProfile profile_;
98e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  ContentSettingsService* content_settings_;
997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TestPreferenceAPI test_preference_api_;
1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)ExtensionControlledPrefsTest::ExtensionControlledPrefsTest()
1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : PrefsPrepopulatedTestBase(),
104e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      content_settings_(ContentSettingsService::Get(&profile_)),
105e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      test_preference_api_(&prefs_, content_settings_) {
106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  prefs_.prefs()->AddObserver(content_settings_);
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)ExtensionControlledPrefsTest::~ExtensionControlledPrefsTest() {
1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::RegisterPreferences(
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registry->RegisterStringPref(
1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      kPref1, kDefaultPref1, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registry->RegisterStringPref(
1177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      kPref2, kDefaultPref2, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registry->RegisterStringPref(
1197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      kPref3, kDefaultPref3, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registry->RegisterStringPref(
1217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      kPref4, kDefaultPref4, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
1227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::InstallExtensionControlledPref(
1257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Extension* extension,
1267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::string& key,
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::Value* value) {
1287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EnsureExtensionInstalled(extension);
1297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  test_preference_api_.SetExtensionControlledPref(
1307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      extension->id(), key, kExtensionPrefsScopeRegular, value);
1317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::InstallExtensionControlledPrefIncognito(
1347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Extension* extension,
1357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::string& key,
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::Value* value) {
1377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EnsureExtensionInstalled(extension);
1387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  test_preference_api_.SetExtensionControlledPref(
1397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      extension->id(), key, kExtensionPrefsScopeIncognitoPersistent, value);
1407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::
1437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)InstallExtensionControlledPrefIncognitoSessionOnly(Extension* extension,
1447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                                   const std::string& key,
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                   base::Value* value) {
1467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EnsureExtensionInstalled(extension);
1477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  test_preference_api_.SetExtensionControlledPref(
1487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      extension->id(), key, kExtensionPrefsScopeIncognitoSessionOnly, value);
1497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::InstallExtension(Extension* extension) {
1527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EnsureExtensionInstalled(extension);
1537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::UninstallExtension(
1567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::string& extension_id) {
1577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EnsureExtensionUninstalled(extension_id);
1587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::EnsureExtensionInstalled(
1617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Extension* extension) {
1627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Install extension the first time a preference is set for it.
1637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Extension* extensions[] = { extension1(),
1647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              extension2(),
1657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              extension3(),
1667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              extension4() };
1677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (size_t i = 0; i < kNumInstalledExtensions; ++i) {
1687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (extension == extensions[i] && !installed_[i]) {
169a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      prefs()->OnExtensionInstalled(extension,
170a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                    Extension::ENABLED,
171effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                    syncer::StringOrdinal(),
172effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                    std::string());
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prefs()->SetIsIncognitoEnabled(extension->id(), true);
1747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      installed_[i] = true;
1757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
1767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
1777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void ExtensionControlledPrefsTest::EnsureExtensionUninstalled(
1817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const std::string& extension_id) {
1827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  Extension* extensions[] = { extension1(),
1837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              extension2(),
1847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              extension3(),
1857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              extension4() };
1867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  for (size_t i = 0; i < kNumInstalledExtensions; ++i) {
1877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (extensions[i]->id() == extension_id) {
1887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      installed_[i] = false;
1897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
1907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
1917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  prefs()->OnExtensionUninstalled(extension_id, Manifest::INTERNAL, false);
1937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsInstallOneExtension
1967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : public ExtensionControlledPrefsTest {
1977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
1987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(extension1(),
1997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                   kPref1,
2003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                   new base::StringValue("val1"));
2017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
2037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
2047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val1", actual);
2057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
2077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsInstallOneExtension,
2087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       ControlledPrefsInstallOneExtension) { }
2097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Check that we do not forget persistent incognito values after a reload.
2117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsInstallIncognitoPersistent
2127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : public ExtensionControlledPrefsTest {
2137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
2147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
2157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(
2163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val1"));
2177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPrefIncognito(
2183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val2"));
2197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    scoped_ptr<PrefService> incog_prefs(prefs_.CreateIncognitoPrefService());
2207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = incog_prefs->GetString(kPref1);
2217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val2", actual);
2227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Main pref service shall see only non-incognito settings.
2267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
2277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val1", actual);
2287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Incognito pref service shall see incognito values.
2297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    scoped_ptr<PrefService> incog_prefs(prefs_.CreateIncognitoPrefService());
2307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    actual = incog_prefs->GetString(kPref1);
2317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val2", actual);
2327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
2347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsInstallIncognitoPersistent,
2357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       ControlledPrefsInstallIncognitoPersistent) { }
2367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Check that we forget 'session only' incognito values after a reload.
2387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsInstallIncognitoSessionOnly
2397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : public ExtensionControlledPrefsTest {
2407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
2417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ControlledPrefsInstallIncognitoSessionOnly() : iteration_(0) {}
2427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
2447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(
2453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val1"));
2467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPrefIncognitoSessionOnly(
2473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val2"));
2487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    scoped_ptr<PrefService> incog_prefs(prefs_.CreateIncognitoPrefService());
2497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = incog_prefs->GetString(kPref1);
2507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val2", actual);
2517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
2537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Main pref service shall see only non-incognito settings.
2547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
2557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val1", actual);
2567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Incognito pref service shall see session-only incognito values only
2577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // during first run. Once the pref service was reloaded, all values shall be
2587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // discarded.
2597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    scoped_ptr<PrefService> incog_prefs(prefs_.CreateIncognitoPrefService());
2607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    actual = incog_prefs->GetString(kPref1);
2617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (iteration_ == 0) {
2627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      EXPECT_EQ("val2", actual);
2637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    } else {
2647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      EXPECT_EQ("val1", actual);
2657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
2667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    ++iteration_;
2677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int iteration_;
2697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
2707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsInstallIncognitoSessionOnly,
2717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       ControlledPrefsInstallIncognitoSessionOnly) { }
2727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsUninstallExtension : public ExtensionControlledPrefsTest {
2747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
2757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(
2763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val1"));
2777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(
2783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref2, new base::StringValue("val2"));
279e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    scoped_refptr<ContentSettingsStore> store = content_settings_store();
2807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    ContentSettingsPattern pattern =
2817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        ContentSettingsPattern::FromString("http://[*.]example.com");
2827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    store->SetExtensionContentSetting(extension1()->id(),
2837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      pattern, pattern,
2847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      CONTENT_SETTINGS_TYPE_IMAGES,
2857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      std::string(),
2867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      CONTENT_SETTING_BLOCK,
2877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                      kExtensionPrefsScopeRegular);
2887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    UninstallExtension(extension1()->id());
2907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
2927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_FALSE(prefs()->HasPrefForExtension(extension1()->id()));
2937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual;
2957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    actual = prefs()->pref_service()->GetString(kPref1);
2967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ(kDefaultPref1, actual);
2977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    actual = prefs()->pref_service()->GetString(kPref2);
2987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ(kDefaultPref2, actual);
2997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
3017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsUninstallExtension,
3027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       ControlledPrefsUninstallExtension) { }
3037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Tests triggering of notifications to registered observers.
3057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsNotifyWhenNeeded : public ExtensionControlledPrefsTest {
3067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
3077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    using testing::_;
3087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    using testing::Mock;
3097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    using testing::StrEq;
3107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    MockPrefChangeCallback observer(prefs()->pref_service());
3127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    PrefChangeRegistrar registrar;
3137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    registrar.Init(prefs()->pref_service());
3147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    registrar.Add(kPref1, observer.GetCallback());
3157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    MockPrefChangeCallback incognito_observer(prefs()->pref_service());
3177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    scoped_ptr<PrefService> incog_prefs(prefs_.CreateIncognitoPrefService());
3187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    PrefChangeRegistrar incognito_registrar;
3197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    incognito_registrar.Init(incog_prefs.get());
3207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    incognito_registrar.Add(kPref1, incognito_observer.GetCallback());
3217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Write value and check notification.
3237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(observer, OnPreferenceChanged(_));
3247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(incognito_observer, OnPreferenceChanged(_));
3257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(extension1(), kPref1,
3263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        new base::StringValue("https://www.chromium.org"));
3277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&observer);
3287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&incognito_observer);
3297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Write same value.
3317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
3327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(incognito_observer, OnPreferenceChanged(_)).Times(0);
3337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(extension1(), kPref1,
3343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        new base::StringValue("https://www.chromium.org"));
3357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&observer);
3367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&incognito_observer);
3377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Change value.
3397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(observer, OnPreferenceChanged(_));
3407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(incognito_observer, OnPreferenceChanged(_));
3417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(extension1(), kPref1,
3423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        new base::StringValue("chrome://newtab"));
3437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&observer);
3447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&incognito_observer);
3457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Change only incognito persistent value.
3467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
3477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(incognito_observer, OnPreferenceChanged(_));
3487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPrefIncognito(extension1(), kPref1,
3493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        new base::StringValue("chrome://newtab2"));
3507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&observer);
3517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&incognito_observer);
3527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Change only incognito session-only value.
3547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
3557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(incognito_observer, OnPreferenceChanged(_));
3567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPrefIncognito(extension1(), kPref1,
3573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        new base::StringValue("chrome://newtab3"));
3587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&observer);
3597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&incognito_observer);
3607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Uninstall.
3627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(observer, OnPreferenceChanged(_));
3637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(incognito_observer, OnPreferenceChanged(_));
3647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    UninstallExtension(extension1()->id());
3657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&observer);
3667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Mock::VerifyAndClearExpectations(&incognito_observer);
3677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    registrar.Remove(kPref1);
3697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    incognito_registrar.Remove(kPref1);
3707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
3727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
3737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ(kDefaultPref1, actual);
3747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
3767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsNotifyWhenNeeded,
3777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       ControlledPrefsNotifyWhenNeeded) { }
3787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Tests disabling an extension.
3807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsDisableExtension : public ExtensionControlledPrefsTest {
3817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
3827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(
3833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val1"));
3847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
3857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val1", actual);
3867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    prefs()->SetExtensionState(extension1()->id(), Extension::DISABLED);
3877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
3897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
3907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ(kDefaultPref1, actual);
3917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
3927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
3937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsDisableExtension, ControlledPrefsDisableExtension) { }
3947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Tests disabling and reenabling an extension.
3967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsReenableExtension : public ExtensionControlledPrefsTest {
3977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
3987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(
3993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val1"));
4007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    prefs()->SetExtensionState(extension1()->id(), Extension::DISABLED);
4017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    prefs()->SetExtensionState(extension1()->id(), Extension::ENABLED);
4027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
4047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
4057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_EQ("val1", actual);
4067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
4087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsDisableExtension, ControlledPrefsReenableExtension) { }
4097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Mock class to test whether objects are deleted correctly.
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockStringValue : public base::StringValue {
4127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
4137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  explicit MockStringValue(const std::string& in_value)
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : base::StringValue(in_value) {
4157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ~MockStringValue() {
4177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    Die();
4187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MOCK_METHOD0(Die, void());
4207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
4217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsSetExtensionControlledPref
4237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : public ExtensionControlledPrefsTest {
4247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
4257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
4267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    MockStringValue* v1 = new MockStringValue("https://www.chromium.org");
4277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    MockStringValue* v2 = new MockStringValue("https://www.chromium.org");
4287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    MockStringValue* v1i = new MockStringValue("https://www.chromium.org");
4297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    MockStringValue* v2i = new MockStringValue("https://www.chromium.org");
4307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Ownership is taken, value shall not be deleted.
4317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(*v1, Die()).Times(0);
4327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(*v1i, Die()).Times(0);
4337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(extension1(), kPref1, v1);
4347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPrefIncognito(extension1(), kPref1, v1i);
4357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    testing::Mock::VerifyAndClearExpectations(v1);
4367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    testing::Mock::VerifyAndClearExpectations(v1i);
4377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Make sure there is no memory leak and both values are deleted.
4387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(*v1, Die()).Times(1);
4397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(*v1i, Die()).Times(1);
4407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(*v2, Die()).Times(1);
4417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EXPECT_CALL(*v2i, Die()).Times(1);
4427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(extension1(), kPref1, v2);
4437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPrefIncognito(extension1(), kPref1, v2i);
4447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    prefs_.RecreateExtensionPrefs();
4457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    testing::Mock::VerifyAndClearExpectations(v1);
4467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    testing::Mock::VerifyAndClearExpectations(v1i);
4477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    testing::Mock::VerifyAndClearExpectations(v2);
4487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    testing::Mock::VerifyAndClearExpectations(v2i);
4497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
4527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
4547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsSetExtensionControlledPref,
4557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)       ControlledPrefsSetExtensionControlledPref) { }
4567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Tests that the switches::kDisableExtensions command-line flag prevents
4587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// extension controlled preferences from being enacted.
4597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ControlledPrefsDisableExtensions : public ExtensionControlledPrefsTest {
4607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
4617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ControlledPrefsDisableExtensions()
4627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      : iteration_(0) {}
4637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ~ControlledPrefsDisableExtensions() {}
4647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Initialize() OVERRIDE {
4657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    InstallExtensionControlledPref(
4663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        extension1(), kPref1, new base::StringValue("val1"));
4677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // This becomes only active in the second verification phase.
4687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    prefs_.set_extensions_disabled(true);
4697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void Verify() OVERRIDE {
4717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    std::string actual = prefs()->pref_service()->GetString(kPref1);
4727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (iteration_ == 0) {
4737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      EXPECT_EQ("val1", actual);
4747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ++iteration_;
4757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    } else {
4767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      EXPECT_EQ(kDefaultPref1, actual);
4777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
4787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
4797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) private:
4817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  int iteration_;
4827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
4837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(ControlledPrefsDisableExtensions, ControlledPrefsDisableExtensions) { }
4847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace extensions
486