OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/path_service.h" |
| 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 8 #include "chrome/browser/extensions/unpacked_installer.h" |
| 9 #include "chrome/browser/managed_mode/managed_user_service.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "content/public/test/test_utils.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using content::MessageLoopRunner; |
| 20 |
| 21 namespace { |
| 22 |
| 23 class ManagedModeURLFilterObserver : public ManagedModeURLFilter::Observer { |
| 24 public: |
| 25 explicit ManagedModeURLFilterObserver(ManagedModeURLFilter* url_filter) |
| 26 : url_filter_(url_filter) { |
| 27 Reset(); |
| 28 url_filter_->AddObserver(this); |
| 29 } |
| 30 |
| 31 ~ManagedModeURLFilterObserver() { |
| 32 url_filter_->RemoveObserver(this); |
| 33 } |
| 34 |
| 35 void Wait() { |
| 36 message_loop_runner_->Run(); |
| 37 Reset(); |
| 38 } |
| 39 |
| 40 // ManagedModeURLFilter::Observer |
| 41 virtual void OnSiteListUpdated() OVERRIDE { |
| 42 message_loop_runner_->Quit(); |
| 43 } |
| 44 |
| 45 private: |
| 46 void Reset() { |
| 47 message_loop_runner_ = new MessageLoopRunner; |
| 48 } |
| 49 |
| 50 ManagedModeURLFilter* url_filter_; |
| 51 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 52 }; |
| 53 |
| 54 } // namespace |
| 55 |
| 56 TEST(ManagedUserServiceTest, ExtensionManagementPolicyProvider) { |
| 57 MessageLoop message_loop; |
| 58 TestingProfile profile; |
| 59 { |
| 60 ManagedUserService managed_user_service(&profile); |
| 61 EXPECT_FALSE(managed_user_service.ProfileIsManaged()); |
| 62 |
| 63 string16 error_1; |
| 64 EXPECT_TRUE(managed_user_service.UserMayLoad(NULL, &error_1)); |
| 65 EXPECT_EQ(string16(), error_1); |
| 66 |
| 67 string16 error_2; |
| 68 EXPECT_TRUE(managed_user_service.UserMayModifySettings(NULL, &error_2)); |
| 69 EXPECT_EQ(string16(), error_2); |
| 70 } |
| 71 |
| 72 profile.GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true); |
| 73 { |
| 74 ManagedUserService managed_user_service(&profile); |
| 75 EXPECT_TRUE(managed_user_service.ProfileIsManaged()); |
| 76 |
| 77 string16 error_1; |
| 78 EXPECT_FALSE(managed_user_service.UserMayLoad(NULL, &error_1)); |
| 79 EXPECT_FALSE(error_1.empty()); |
| 80 |
| 81 string16 error_2; |
| 82 EXPECT_FALSE(managed_user_service.UserMayModifySettings(NULL, &error_2)); |
| 83 EXPECT_FALSE(error_2.empty()); |
| 84 |
| 85 #ifndef NDEBUG |
| 86 EXPECT_FALSE(managed_user_service.GetDebugPolicyProviderName().empty()); |
| 87 #endif |
| 88 } |
| 89 } |
| 90 |
| 91 class ManagedUserServiceExtensionTest : public ExtensionServiceTestBase { |
| 92 public: |
| 93 ManagedUserServiceExtensionTest() {} |
| 94 virtual ~ManagedUserServiceExtensionTest() {} |
| 95 |
| 96 virtual void SetUp() OVERRIDE { |
| 97 ExtensionServiceTestBase::SetUp(); |
| 98 InitializeEmptyExtensionService(); |
| 99 } |
| 100 |
| 101 protected: |
| 102 ScopedVector<ManagedModeSiteList> GetActiveSiteLists( |
| 103 ManagedUserService* managed_user_service) { |
| 104 return managed_user_service->GetActiveSiteLists(); |
| 105 } |
| 106 }; |
| 107 |
| 108 TEST_F(ManagedUserServiceExtensionTest, NoContentPacks) { |
| 109 ManagedUserService managed_user_service(profile_.get()); |
| 110 ManagedModeURLFilter* url_filter = |
| 111 managed_user_service.GetURLFilterForUIThread(); |
| 112 |
| 113 GURL url("http://youtube.com"); |
| 114 ScopedVector<ManagedModeSiteList> site_lists = |
| 115 GetActiveSiteLists(&managed_user_service); |
| 116 ASSERT_EQ(0u, site_lists.size()); |
| 117 EXPECT_EQ(ManagedModeURLFilter::ALLOW, |
| 118 url_filter->GetFilteringBehaviorForURL(url)); |
| 119 } |
| 120 |
| 121 TEST_F(ManagedUserServiceExtensionTest, InstallContentPacks) { |
| 122 profile_->GetPrefs()->SetBoolean(prefs::kProfileIsManaged, true); |
| 123 ManagedUserService managed_user_service(profile_.get()); |
| 124 managed_user_service.SetElevatedForTesting(true); |
| 125 ManagedModeURLFilter* url_filter = |
| 126 managed_user_service.GetURLFilterForUIThread(); |
| 127 ManagedModeURLFilterObserver observer(url_filter); |
| 128 observer.Wait(); |
| 129 |
| 130 GURL example_url("http://example.com"); |
| 131 GURL moose_url("http://moose.org"); |
| 132 EXPECT_EQ(ManagedModeURLFilter::BLOCK, |
| 133 url_filter->GetFilteringBehaviorForURL(example_url)); |
| 134 |
| 135 profile_->GetPrefs()->SetInteger(prefs::kDefaultManagedModeFilteringBehavior, |
| 136 ManagedModeURLFilter::WARN); |
| 137 EXPECT_EQ(ManagedModeURLFilter::WARN, |
| 138 url_filter->GetFilteringBehaviorForURL(example_url)); |
| 139 |
| 140 // Load a content pack. |
| 141 scoped_refptr<extensions::UnpackedInstaller> installer( |
| 142 extensions::UnpackedInstaller::Create(service_)); |
| 143 installer->set_prompt_for_plugins(false); |
| 144 FilePath test_data_dir; |
| 145 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); |
| 146 FilePath extension_path = |
| 147 test_data_dir.AppendASCII("extensions/managed_mode/content_pack"); |
| 148 content::WindowedNotificationObserver extension_load_observer( |
| 149 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 150 content::Source<Profile>(profile_.get())); |
| 151 installer->Load(extension_path); |
| 152 extension_load_observer.Wait(); |
| 153 observer.Wait(); |
| 154 content::Details<extensions::Extension> details = |
| 155 extension_load_observer.details(); |
| 156 scoped_refptr<extensions::Extension> extension = |
| 157 make_scoped_refptr(details.ptr()); |
| 158 ASSERT_TRUE(extension); |
| 159 |
| 160 ScopedVector<ManagedModeSiteList> site_lists = |
| 161 GetActiveSiteLists(&managed_user_service); |
| 162 ASSERT_EQ(1u, site_lists.size()); |
| 163 std::vector<ManagedModeSiteList::Site> sites; |
| 164 site_lists[0]->GetSites(&sites); |
| 165 ASSERT_EQ(3u, sites.size()); |
| 166 EXPECT_EQ(ASCIIToUTF16("YouTube"), sites[0].name); |
| 167 EXPECT_EQ(ASCIIToUTF16("Homestar Runner"), sites[1].name); |
| 168 EXPECT_EQ(string16(), sites[2].name); |
| 169 |
| 170 EXPECT_EQ(ManagedModeURLFilter::ALLOW, |
| 171 url_filter->GetFilteringBehaviorForURL(example_url)); |
| 172 EXPECT_EQ(ManagedModeURLFilter::WARN, |
| 173 url_filter->GetFilteringBehaviorForURL(moose_url)); |
| 174 |
| 175 // Load a second content pack. |
| 176 installer = extensions::UnpackedInstaller::Create(service_); |
| 177 extension_path = |
| 178 test_data_dir.AppendASCII("extensions/managed_mode/content_pack_2"); |
| 179 installer->Load(extension_path); |
| 180 observer.Wait(); |
| 181 |
| 182 site_lists = GetActiveSiteLists(&managed_user_service); |
| 183 ASSERT_EQ(2u, site_lists.size()); |
| 184 sites.clear(); |
| 185 site_lists[0]->GetSites(&sites); |
| 186 site_lists[1]->GetSites(&sites); |
| 187 ASSERT_EQ(4u, sites.size()); |
| 188 // The site lists might be returned in any order, so we put them into a set. |
| 189 std::set<std::string> site_names; |
| 190 for (std::vector<ManagedModeSiteList::Site>::const_iterator it = |
| 191 sites.begin(); it != sites.end(); ++it) { |
| 192 site_names.insert(UTF16ToUTF8(it->name)); |
| 193 } |
| 194 EXPECT_TRUE(site_names.count("YouTube") == 1u); |
| 195 EXPECT_TRUE(site_names.count("Homestar Runner") == 1u); |
| 196 EXPECT_TRUE(site_names.count(std::string()) == 1u); |
| 197 EXPECT_TRUE(site_names.count("Moose") == 1u); |
| 198 |
| 199 EXPECT_EQ(ManagedModeURLFilter::ALLOW, |
| 200 url_filter->GetFilteringBehaviorForURL(example_url)); |
| 201 EXPECT_EQ(ManagedModeURLFilter::ALLOW, |
| 202 url_filter->GetFilteringBehaviorForURL(moose_url)); |
| 203 |
| 204 // Disable the first content pack. |
| 205 service_->DisableExtension(extension->id(), |
| 206 extensions::Extension::DISABLE_USER_ACTION); |
| 207 observer.Wait(); |
| 208 |
| 209 site_lists = GetActiveSiteLists(&managed_user_service); |
| 210 ASSERT_EQ(1u, site_lists.size()); |
| 211 sites.clear(); |
| 212 site_lists[0]->GetSites(&sites); |
| 213 ASSERT_EQ(1u, sites.size()); |
| 214 EXPECT_EQ(ASCIIToUTF16("Moose"), sites[0].name); |
| 215 |
| 216 EXPECT_EQ(ManagedModeURLFilter::WARN, |
| 217 url_filter->GetFilteringBehaviorForURL(example_url)); |
| 218 EXPECT_EQ(ManagedModeURLFilter::ALLOW, |
| 219 url_filter->GetFilteringBehaviorForURL(moose_url)); |
| 220 } |
OLD | NEW |