| OLD | NEW |
| (Empty) | |
| 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/utf_string_conversions.h" |
| 6 #include "chrome/browser/extensions/extension_management_policy.h" |
| 7 #include "chrome/browser/extensions/test_extension_management_policy.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 typedef TestExtensionManagementPolicyDelegate TestDelegate; |
| 11 |
| 12 class ExtensionManagementPolicyTest : public testing::Test { |
| 13 public: |
| 14 void SetUp() { |
| 15 allow_all_.SetAllowedActions(TestDelegate::ALLOW_ALL); |
| 16 no_modify_status_.SetAllowedActions(TestDelegate::PROHIBIT_MODIFY_STATUS); |
| 17 no_modify_usage_.SetAllowedActions(TestDelegate::PROHIBIT_MODIFY_USAGE); |
| 18 no_install_.SetAllowedActions(TestDelegate::PROHIBIT_INSTALL); |
| 19 must_remain_enabled_.SetAllowedActions(TestDelegate::MUST_REMAIN_ENABLED); |
| 20 } |
| 21 |
| 22 protected: |
| 23 ExtensionManagementPolicy policy_; |
| 24 |
| 25 TestDelegate allow_all_; |
| 26 TestDelegate no_modify_status_; |
| 27 TestDelegate no_modify_usage_; |
| 28 TestDelegate no_install_; |
| 29 TestDelegate must_remain_enabled_; |
| 30 }; |
| 31 |
| 32 TEST_F(ExtensionManagementPolicyTest, RegisterAndUnregister) { |
| 33 EXPECT_EQ(0u, policy_.CountDelegates()); |
| 34 policy_.RegisterDelegate(&allow_all_); |
| 35 EXPECT_EQ(1u, policy_.CountDelegates()); |
| 36 policy_.RegisterDelegate(&allow_all_); |
| 37 EXPECT_EQ(1u, policy_.CountDelegates()); |
| 38 |
| 39 policy_.RegisterDelegate(&no_modify_status_); |
| 40 EXPECT_EQ(2u, policy_.CountDelegates()); |
| 41 policy_.UnregisterDelegate(&allow_all_); |
| 42 EXPECT_EQ(1u, policy_.CountDelegates()); |
| 43 policy_.UnregisterDelegate(&allow_all_); |
| 44 EXPECT_EQ(1u, policy_.CountDelegates()); |
| 45 policy_.UnregisterDelegate(&no_modify_status_); |
| 46 EXPECT_EQ(0u, policy_.CountDelegates()); |
| 47 |
| 48 policy_.RegisterDelegate(&allow_all_); |
| 49 policy_.RegisterDelegate(&no_modify_status_); |
| 50 EXPECT_EQ(2u, policy_.CountDelegates()); |
| 51 policy_.UnregisterAllDelegates(); |
| 52 EXPECT_EQ(0u, policy_.CountDelegates()); |
| 53 } |
| 54 |
| 55 TEST_F(ExtensionManagementPolicyTest, UserMayInstall) { |
| 56 // No delegates registered. |
| 57 string16 error; |
| 58 EXPECT_TRUE(policy_.UserMayInstall("", Extension::INVALID, "", &error)); |
| 59 EXPECT_TRUE(error.empty()); |
| 60 |
| 61 // One delegate, no relevant restriction. |
| 62 policy_.RegisterDelegate(&no_modify_status_); |
| 63 EXPECT_TRUE(policy_.UserMayInstall("", Extension::INVALID, "", &error)); |
| 64 EXPECT_TRUE(error.empty()); |
| 65 |
| 66 // Two delegates, no relevant restrictions. |
| 67 policy_.RegisterDelegate(&no_modify_usage_); |
| 68 EXPECT_TRUE(policy_.UserMayInstall("", Extension::INVALID, "", &error)); |
| 69 EXPECT_TRUE(error.empty()); |
| 70 |
| 71 // Three delegates, one with a relevant restriction. |
| 72 policy_.RegisterDelegate(&no_install_); |
| 73 EXPECT_FALSE(policy_.UserMayInstall("", Extension::INVALID, "", &error)); |
| 74 EXPECT_FALSE(error.empty()); |
| 75 |
| 76 // Remove the restriction. |
| 77 policy_.UnregisterDelegate(&no_install_); |
| 78 error.clear(); |
| 79 EXPECT_TRUE(policy_.UserMayInstall("", Extension::INVALID, "", &error)); |
| 80 EXPECT_TRUE(error.empty()); |
| 81 } |
| 82 TEST_F(ExtensionManagementPolicyTest, UserMayModifyStatus) { |
| 83 // No delegates registered. |
| 84 string16 error; |
| 85 EXPECT_TRUE(policy_.UserMayModifyStatus(NULL, &error)); |
| 86 EXPECT_TRUE(error.empty()); |
| 87 |
| 88 // One delegate, no relevant restriction. |
| 89 policy_.RegisterDelegate(&allow_all_); |
| 90 EXPECT_TRUE(policy_.UserMayModifyStatus(NULL, &error)); |
| 91 EXPECT_TRUE(error.empty()); |
| 92 |
| 93 // Two delegates, no relevant restrictions. |
| 94 policy_.RegisterDelegate(&no_modify_usage_); |
| 95 EXPECT_TRUE(policy_.UserMayModifyStatus(NULL, &error)); |
| 96 EXPECT_TRUE(error.empty()); |
| 97 |
| 98 // Three delegates, one with a relevant restriction. |
| 99 policy_.RegisterDelegate(&no_modify_status_); |
| 100 EXPECT_FALSE(policy_.UserMayModifyStatus(NULL, &error)); |
| 101 EXPECT_FALSE(error.empty()); |
| 102 |
| 103 // Remove the restriction. |
| 104 policy_.UnregisterDelegate(&no_modify_status_); |
| 105 error.clear(); |
| 106 EXPECT_TRUE(policy_.UserMayModifyStatus(NULL, &error)); |
| 107 EXPECT_TRUE(error.empty()); |
| 108 } |
| 109 |
| 110 TEST_F(ExtensionManagementPolicyTest, UserMayModifyUsage) { |
| 111 // No delegates registered. |
| 112 string16 error; |
| 113 EXPECT_TRUE(policy_.UserMayModifyUsage(NULL, &error)); |
| 114 EXPECT_TRUE(error.empty()); |
| 115 |
| 116 // One delegate, no relevant restriction. |
| 117 policy_.RegisterDelegate(&allow_all_); |
| 118 EXPECT_TRUE(policy_.UserMayModifyUsage(NULL, &error)); |
| 119 EXPECT_TRUE(error.empty()); |
| 120 |
| 121 // Two delegates, no relevant restrictions. |
| 122 policy_.RegisterDelegate(&no_modify_status_); |
| 123 EXPECT_TRUE(policy_.UserMayModifyUsage(NULL, &error)); |
| 124 EXPECT_TRUE(error.empty()); |
| 125 |
| 126 // Three delegates, one with a relevant restriction. |
| 127 policy_.RegisterDelegate(&no_modify_usage_); |
| 128 EXPECT_FALSE(policy_.UserMayModifyUsage(NULL, &error)); |
| 129 EXPECT_FALSE(error.empty()); |
| 130 |
| 131 // Remove the restriction. |
| 132 policy_.UnregisterDelegate(&no_modify_usage_); |
| 133 error.clear(); |
| 134 EXPECT_TRUE(policy_.UserMayModifyUsage(NULL, &error)); |
| 135 EXPECT_TRUE(error.empty()); |
| 136 } |
| 137 |
| 138 TEST_F(ExtensionManagementPolicyTest, MustRemainEnabled) { |
| 139 // No delegates registered. |
| 140 string16 error; |
| 141 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error)); |
| 142 EXPECT_TRUE(error.empty()); |
| 143 |
| 144 // One delegate, no relevant restriction. |
| 145 policy_.RegisterDelegate(&allow_all_); |
| 146 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error)); |
| 147 EXPECT_TRUE(error.empty()); |
| 148 |
| 149 // Two delegates, no relevant restrictions. |
| 150 policy_.RegisterDelegate(&no_modify_status_); |
| 151 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error)); |
| 152 EXPECT_TRUE(error.empty()); |
| 153 |
| 154 // Three delegates, one with a relevant restriction. |
| 155 policy_.RegisterDelegate(&must_remain_enabled_); |
| 156 EXPECT_TRUE(policy_.MustRemainEnabled(NULL, &error)); |
| 157 EXPECT_FALSE(error.empty()); |
| 158 |
| 159 // Remove the restriction. |
| 160 policy_.UnregisterDelegate(&must_remain_enabled_); |
| 161 error.clear(); |
| 162 EXPECT_FALSE(policy_.MustRemainEnabled(NULL, &error)); |
| 163 EXPECT_TRUE(error.empty()); |
| 164 } |
| 165 |
| 166 TEST_F(ExtensionManagementPolicyTest, ErrorHandling) { |
| 167 // The error parameter should be unchanged if no restriction was found. |
| 168 std::string original_error = "Ceci est en effet une erreur."; |
| 169 string16 error = UTF8ToUTF16(original_error); |
| 170 EXPECT_TRUE(policy_.UserMayModifyStatus(NULL, &error)); |
| 171 EXPECT_EQ(original_error, UTF16ToUTF8(error)); |
| 172 |
| 173 // Ensure no crashes if no error message was requested. |
| 174 EXPECT_TRUE(policy_.UserMayModifyStatus(NULL, NULL)); |
| 175 policy_.RegisterDelegate(&no_modify_status_); |
| 176 EXPECT_FALSE(policy_.UserMayModifyStatus(NULL, NULL)); |
| 177 |
| 178 // Make sure returned error is correct. |
| 179 EXPECT_FALSE(policy_.UserMayModifyStatus(NULL, &error)); |
| 180 EXPECT_EQ(UTF8ToUTF16(TestDelegate::expected_error()), error); |
| 181 } |
| OLD | NEW |