| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // TODO(rickcam): Bug 73183: Add unit tests for image loading | 5 // TODO(rickcam): Bug 73183: Add unit tests for image loading |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "chrome/browser/background/background_application_list_model.h" | 10 #include "chrome/browser/background/background_application_list_model.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 class BackgroundApplicationListModelTest : public ExtensionServiceTestBase { | 38 class BackgroundApplicationListModelTest : public ExtensionServiceTestBase { |
| 39 public: | 39 public: |
| 40 BackgroundApplicationListModelTest() {} | 40 BackgroundApplicationListModelTest() {} |
| 41 virtual ~BackgroundApplicationListModelTest() {} | 41 virtual ~BackgroundApplicationListModelTest() {} |
| 42 | 42 |
| 43 protected: | 43 protected: |
| 44 void InitializeAndLoadEmptyExtensionService() { | 44 void InitializeAndLoadEmptyExtensionService() { |
| 45 InitializeEmptyExtensionService(); | 45 InitializeEmptyExtensionService(); |
| 46 service_->OnLoadedInstalledExtensions(); /* Sends EXTENSIONS_READY */ | 46 service_->OnLoadedInstalledExtensions(); /* Sends EXTENSIONS_READY */ |
| 47 } | 47 } |
| 48 |
| 49 bool IsBackgroundApp(const Extension& app) { |
| 50 return BackgroundApplicationListModel::IsBackgroundApp(app, |
| 51 profile_.get()); |
| 52 } |
| 48 }; | 53 }; |
| 49 | 54 |
| 50 // Returns a barebones test Extension object with the specified |name|. The | 55 // Returns a barebones test Extension object with the specified |name|. The |
| 51 // returned extension will include background permission iff | 56 // returned extension will include background permission iff |
| 52 // |background_permission| is true. | 57 // |background_permission| is true. |
| 53 static scoped_refptr<Extension> CreateExtension(const std::string& name, | 58 static scoped_refptr<Extension> CreateExtension(const std::string& name, |
| 54 bool background_permission) { | 59 bool background_permission) { |
| 55 DictionaryValue manifest; | 60 DictionaryValue manifest; |
| 56 manifest.SetString(extension_manifest_keys::kVersion, "1.0.0.0"); | 61 manifest.SetString(extension_manifest_keys::kVersion, "1.0.0.0"); |
| 57 manifest.SetString(extension_manifest_keys::kName, name); | 62 manifest.SetString(extension_manifest_keys::kName, name); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 77 std::string GenerateUniqueExtensionName() { | 82 std::string GenerateUniqueExtensionName() { |
| 78 static int uniqueness = 0; | 83 static int uniqueness = 0; |
| 79 std::ostringstream output; | 84 std::ostringstream output; |
| 80 output << "Unique Named Extension " << uniqueness; | 85 output << "Unique Named Extension " << uniqueness; |
| 81 ++uniqueness; | 86 ++uniqueness; |
| 82 return output.str(); | 87 return output.str(); |
| 83 } | 88 } |
| 84 | 89 |
| 85 void AddBackgroundPermission(ExtensionService* service, | 90 void AddBackgroundPermission(ExtensionService* service, |
| 86 Extension* extension) { | 91 Extension* extension) { |
| 87 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) return; | 92 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 93 service->profile())) { |
| 94 return; |
| 95 } |
| 88 | 96 |
| 89 static scoped_refptr<Extension> temporary = | 97 static scoped_refptr<Extension> temporary = |
| 90 CreateExtension(GenerateUniqueExtensionName(), true); | 98 CreateExtension(GenerateUniqueExtensionName(), true); |
| 91 scoped_refptr<const ExtensionPermissionSet> permissions = | 99 scoped_refptr<const ExtensionPermissionSet> permissions = |
| 92 temporary->GetActivePermissions(); | 100 temporary->GetActivePermissions(); |
| 93 extensions::PermissionsUpdater(service->profile()).AddPermissions( | 101 extensions::PermissionsUpdater(service->profile()).AddPermissions( |
| 94 extension, permissions.get()); | 102 extension, permissions.get()); |
| 95 } | 103 } |
| 96 | 104 |
| 97 void RemoveBackgroundPermission(ExtensionService* service, | 105 void RemoveBackgroundPermission(ExtensionService* service, |
| 98 Extension* extension) { | 106 Extension* extension) { |
| 99 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension)) return; | 107 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 108 service->profile())) { |
| 109 return; |
| 110 } |
| 100 extensions::PermissionsUpdater(service->profile()).RemovePermissions( | 111 extensions::PermissionsUpdater(service->profile()).RemovePermissions( |
| 101 extension, extension->GetActivePermissions()); | 112 extension, extension->GetActivePermissions()); |
| 102 } | 113 } |
| 103 } // namespace | 114 } // namespace |
| 104 | 115 |
| 105 // With minimal test logic, verifies behavior over an explicit set of | 116 // With minimal test logic, verifies behavior over an explicit set of |
| 106 // extensions, of which some are Background Apps and others are not. | 117 // extensions, of which some are Background Apps and others are not. |
| 107 TEST_F(BackgroundApplicationListModelTest, ExplicitTest) { | 118 TEST_F(BackgroundApplicationListModelTest, ExplicitTest) { |
| 108 InitializeAndLoadEmptyExtensionService(); | 119 InitializeAndLoadEmptyExtensionService(); |
| 109 ExtensionService* service = profile_->GetExtensionService(); | 120 ExtensionService* service = profile_->GetExtensionService(); |
| 110 ASSERT_TRUE(service); | 121 ASSERT_TRUE(service); |
| 111 ASSERT_TRUE(service->is_ready()); | 122 ASSERT_TRUE(service->is_ready()); |
| 112 ASSERT_TRUE(service->extensions()); | 123 ASSERT_TRUE(service->extensions()); |
| 113 ASSERT_TRUE(service->extensions()->is_empty()); | 124 ASSERT_TRUE(service->extensions()->is_empty()); |
| 114 scoped_ptr<BackgroundApplicationListModel> model( | 125 scoped_ptr<BackgroundApplicationListModel> model( |
| 115 new BackgroundApplicationListModel(profile_.get())); | 126 new BackgroundApplicationListModel(profile_.get())); |
| 116 ASSERT_EQ(0U, model->size()); | 127 ASSERT_EQ(0U, model->size()); |
| 117 | 128 |
| 118 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); | 129 scoped_refptr<Extension> ext1 = CreateExtension("alpha", false); |
| 119 scoped_refptr<Extension> ext2 = CreateExtension("bravo", false); | 130 scoped_refptr<Extension> ext2 = CreateExtension("bravo", false); |
| 120 scoped_refptr<Extension> ext3 = CreateExtension("charlie", false); | 131 scoped_refptr<Extension> ext3 = CreateExtension("charlie", false); |
| 121 scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true); | 132 scoped_refptr<Extension> bgapp1 = CreateExtension("delta", true); |
| 122 scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true); | 133 scoped_refptr<Extension> bgapp2 = CreateExtension("echo", true); |
| 123 ASSERT_TRUE(service->extensions() != NULL); | 134 ASSERT_TRUE(service->extensions() != NULL); |
| 124 ASSERT_EQ(0U, service->extensions()->size()); | 135 ASSERT_EQ(0U, service->extensions()->size()); |
| 125 ASSERT_EQ(0U, model->size()); | 136 ASSERT_EQ(0U, model->size()); |
| 126 | 137 |
| 127 // Add alternating Extensions and Background Apps | 138 // Add alternating Extensions and Background Apps |
| 128 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1)); | 139 ASSERT_FALSE(IsBackgroundApp(*ext1)); |
| 129 service->AddExtension(ext1); | 140 service->AddExtension(ext1); |
| 130 ASSERT_EQ(1U, service->extensions()->size()); | 141 ASSERT_EQ(1U, service->extensions()->size()); |
| 131 ASSERT_EQ(0U, model->size()); | 142 ASSERT_EQ(0U, model->size()); |
| 132 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1)); | 143 ASSERT_TRUE(IsBackgroundApp(*bgapp1)); |
| 133 service->AddExtension(bgapp1); | 144 service->AddExtension(bgapp1); |
| 134 ASSERT_EQ(2U, service->extensions()->size()); | 145 ASSERT_EQ(2U, service->extensions()->size()); |
| 135 ASSERT_EQ(1U, model->size()); | 146 ASSERT_EQ(1U, model->size()); |
| 136 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2)); | 147 ASSERT_FALSE(IsBackgroundApp(*ext2)); |
| 137 service->AddExtension(ext2); | 148 service->AddExtension(ext2); |
| 138 ASSERT_EQ(3U, service->extensions()->size()); | 149 ASSERT_EQ(3U, service->extensions()->size()); |
| 139 ASSERT_EQ(1U, model->size()); | 150 ASSERT_EQ(1U, model->size()); |
| 140 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2)); | 151 ASSERT_TRUE(IsBackgroundApp(*bgapp2)); |
| 141 service->AddExtension(bgapp2); | 152 service->AddExtension(bgapp2); |
| 142 ASSERT_EQ(4U, service->extensions()->size()); | 153 ASSERT_EQ(4U, service->extensions()->size()); |
| 143 ASSERT_EQ(2U, model->size()); | 154 ASSERT_EQ(2U, model->size()); |
| 144 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3)); | 155 ASSERT_FALSE(IsBackgroundApp(*ext3)); |
| 145 service->AddExtension(ext3); | 156 service->AddExtension(ext3); |
| 146 ASSERT_EQ(5U, service->extensions()->size()); | 157 ASSERT_EQ(5U, service->extensions()->size()); |
| 147 ASSERT_EQ(2U, model->size()); | 158 ASSERT_EQ(2U, model->size()); |
| 148 | 159 |
| 149 // Remove in FIFO order. | 160 // Remove in FIFO order. |
| 150 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext1)); | 161 ASSERT_FALSE(IsBackgroundApp(*ext1)); |
| 151 service->UninstallExtension(ext1->id(), false, NULL); | 162 service->UninstallExtension(ext1->id(), false, NULL); |
| 152 ASSERT_EQ(4U, service->extensions()->size()); | 163 ASSERT_EQ(4U, service->extensions()->size()); |
| 153 ASSERT_EQ(2U, model->size()); | 164 ASSERT_EQ(2U, model->size()); |
| 154 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp1)); | 165 ASSERT_TRUE(IsBackgroundApp(*bgapp1)); |
| 155 service->UninstallExtension(bgapp1->id(), false, NULL); | 166 service->UninstallExtension(bgapp1->id(), false, NULL); |
| 156 ASSERT_EQ(3U, service->extensions()->size()); | 167 ASSERT_EQ(3U, service->extensions()->size()); |
| 157 ASSERT_EQ(1U, model->size()); | 168 ASSERT_EQ(1U, model->size()); |
| 158 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext2)); | 169 ASSERT_FALSE(IsBackgroundApp(*ext2)); |
| 159 service->UninstallExtension(ext2->id(), false, NULL); | 170 service->UninstallExtension(ext2->id(), false, NULL); |
| 160 ASSERT_EQ(2U, service->extensions()->size()); | 171 ASSERT_EQ(2U, service->extensions()->size()); |
| 161 ASSERT_EQ(1U, model->size()); | 172 ASSERT_EQ(1U, model->size()); |
| 162 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp2)); | 173 ASSERT_TRUE(IsBackgroundApp(*bgapp2)); |
| 163 service->UninstallExtension(bgapp2->id(), false, NULL); | 174 service->UninstallExtension(bgapp2->id(), false, NULL); |
| 164 ASSERT_EQ(1U, service->extensions()->size()); | 175 ASSERT_EQ(1U, service->extensions()->size()); |
| 165 ASSERT_EQ(0U, model->size()); | 176 ASSERT_EQ(0U, model->size()); |
| 166 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext3)); | 177 ASSERT_FALSE(IsBackgroundApp(*ext3)); |
| 167 service->UninstallExtension(ext3->id(), false, NULL); | 178 service->UninstallExtension(ext3->id(), false, NULL); |
| 168 ASSERT_EQ(0U, service->extensions()->size()); | 179 ASSERT_EQ(0U, service->extensions()->size()); |
| 169 ASSERT_EQ(0U, model->size()); | 180 ASSERT_EQ(0U, model->size()); |
| 170 } | 181 } |
| 171 | 182 |
| 172 // With minimal test logic, verifies behavior with dynamic permissions. | 183 // With minimal test logic, verifies behavior with dynamic permissions. |
| 173 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { | 184 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { |
| 174 InitializeAndLoadEmptyExtensionService(); | 185 InitializeAndLoadEmptyExtensionService(); |
| 175 ExtensionService* service = profile_->GetExtensionService(); | 186 ExtensionService* service = profile_->GetExtensionService(); |
| 176 ASSERT_TRUE(service); | 187 ASSERT_TRUE(service); |
| 177 ASSERT_TRUE(service->is_ready()); | 188 ASSERT_TRUE(service->is_ready()); |
| 178 ASSERT_TRUE(service->extensions()); | 189 ASSERT_TRUE(service->extensions()); |
| 179 ASSERT_TRUE(service->extensions()->is_empty()); | 190 ASSERT_TRUE(service->extensions()->is_empty()); |
| 180 scoped_ptr<BackgroundApplicationListModel> model( | 191 scoped_ptr<BackgroundApplicationListModel> model( |
| 181 new BackgroundApplicationListModel(profile_.get())); | 192 new BackgroundApplicationListModel(profile_.get())); |
| 182 ASSERT_EQ(0U, model->size()); | 193 ASSERT_EQ(0U, model->size()); |
| 183 | 194 |
| 184 scoped_refptr<Extension> ext = CreateExtension("extension", false); | 195 scoped_refptr<Extension> ext = CreateExtension("extension", false); |
| 185 scoped_refptr<Extension> bgapp = CreateExtension("application", true); | 196 scoped_refptr<Extension> bgapp = CreateExtension("application", true); |
| 186 ASSERT_TRUE(service->extensions() != NULL); | 197 ASSERT_TRUE(service->extensions() != NULL); |
| 187 ASSERT_EQ(0U, service->extensions()->size()); | 198 ASSERT_EQ(0U, service->extensions()->size()); |
| 188 ASSERT_EQ(0U, model->size()); | 199 ASSERT_EQ(0U, model->size()); |
| 189 | 200 |
| 190 // Add one (non-background) extension and one background application | 201 // Add one (non-background) extension and one background application |
| 191 ASSERT_FALSE(BackgroundApplicationListModel::IsBackgroundApp(*ext)); | 202 ASSERT_FALSE(IsBackgroundApp(*ext)); |
| 192 service->AddExtension(ext); | 203 service->AddExtension(ext); |
| 193 ASSERT_EQ(1U, service->extensions()->size()); | 204 ASSERT_EQ(1U, service->extensions()->size()); |
| 194 ASSERT_EQ(0U, model->size()); | 205 ASSERT_EQ(0U, model->size()); |
| 195 ASSERT_TRUE(BackgroundApplicationListModel::IsBackgroundApp(*bgapp)); | 206 ASSERT_TRUE(IsBackgroundApp(*bgapp)); |
| 196 service->AddExtension(bgapp); | 207 service->AddExtension(bgapp); |
| 197 ASSERT_EQ(2U, service->extensions()->size()); | 208 ASSERT_EQ(2U, service->extensions()->size()); |
| 198 ASSERT_EQ(1U, model->size()); | 209 ASSERT_EQ(1U, model->size()); |
| 199 | 210 |
| 200 // Change permissions back and forth | 211 // Change permissions back and forth |
| 201 AddBackgroundPermission(service, ext.get()); | 212 AddBackgroundPermission(service, ext.get()); |
| 202 ASSERT_EQ(2U, service->extensions()->size()); | 213 ASSERT_EQ(2U, service->extensions()->size()); |
| 203 ASSERT_EQ(2U, model->size()); | 214 ASSERT_EQ(2U, model->size()); |
| 204 RemoveBackgroundPermission(service, bgapp.get()); | 215 RemoveBackgroundPermission(service, bgapp.get()); |
| 205 ASSERT_EQ(2U, service->extensions()->size()); | 216 ASSERT_EQ(2U, service->extensions()->size()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 220 BackgroundApplicationListModel* model, | 231 BackgroundApplicationListModel* model, |
| 221 size_t* expected, | 232 size_t* expected, |
| 222 size_t* count) { | 233 size_t* count) { |
| 223 bool create_background = false; | 234 bool create_background = false; |
| 224 if (rand() % 2) { | 235 if (rand() % 2) { |
| 225 create_background = true; | 236 create_background = true; |
| 226 ++*expected; | 237 ++*expected; |
| 227 } | 238 } |
| 228 scoped_refptr<Extension> extension = | 239 scoped_refptr<Extension> extension = |
| 229 CreateExtension(GenerateUniqueExtensionName(), create_background); | 240 CreateExtension(GenerateUniqueExtensionName(), create_background); |
| 230 ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension), | 241 ASSERT_EQ(BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 242 service->profile()), |
| 231 create_background); | 243 create_background); |
| 232 extensions->insert(extension); | 244 extensions->insert(extension); |
| 233 ++*count; | 245 ++*count; |
| 234 ASSERT_EQ(*count, extensions->size()); | 246 ASSERT_EQ(*count, extensions->size()); |
| 235 service->AddExtension(extension); | 247 service->AddExtension(extension); |
| 236 ASSERT_EQ(*count, service->extensions()->size()); | 248 ASSERT_EQ(*count, service->extensions()->size()); |
| 237 ASSERT_EQ(*expected, model->size()); | 249 ASSERT_EQ(*expected, model->size()); |
| 238 } | 250 } |
| 239 | 251 |
| 240 void RemoveExtension(ExtensionService* service, | 252 void RemoveExtension(ExtensionService* service, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 251 ASSERT_EQ(0U, model->size()); | 263 ASSERT_EQ(0U, model->size()); |
| 252 } else { | 264 } else { |
| 253 // Randomly select which extension to remove | 265 // Randomly select which extension to remove |
| 254 if (extensions->size() > 1) { | 266 if (extensions->size() > 1) { |
| 255 int offset = rand() % (extensions->size() - 1); | 267 int offset = rand() % (extensions->size() - 1); |
| 256 for (int index = 0; index < offset; ++index) | 268 for (int index = 0; index < offset; ++index) |
| 257 ++cursor; | 269 ++cursor; |
| 258 } | 270 } |
| 259 scoped_refptr<Extension> extension = cursor->get(); | 271 scoped_refptr<Extension> extension = cursor->get(); |
| 260 std::string id = extension->id(); | 272 std::string id = extension->id(); |
| 261 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) | 273 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 274 service->profile())) { |
| 262 --*expected; | 275 --*expected; |
| 276 } |
| 263 extensions->erase(cursor); | 277 extensions->erase(cursor); |
| 264 --*count; | 278 --*count; |
| 265 ASSERT_EQ(*count, extensions->size()); | 279 ASSERT_EQ(*count, extensions->size()); |
| 266 service->UninstallExtension(extension->id(), false, NULL); | 280 service->UninstallExtension(extension->id(), false, NULL); |
| 267 ASSERT_EQ(*count, service->extensions()->size()); | 281 ASSERT_EQ(*count, service->extensions()->size()); |
| 268 ASSERT_EQ(*expected, model->size()); | 282 ASSERT_EQ(*expected, model->size()); |
| 269 } | 283 } |
| 270 } | 284 } |
| 271 | 285 |
| 272 void TogglePermission(ExtensionService* service, | 286 void TogglePermission(ExtensionService* service, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 283 ASSERT_EQ(0U, model->size()); | 297 ASSERT_EQ(0U, model->size()); |
| 284 } else { | 298 } else { |
| 285 // Randomly select which extension to toggle. | 299 // Randomly select which extension to toggle. |
| 286 if (extensions->size() > 1) { | 300 if (extensions->size() > 1) { |
| 287 int offset = rand() % (extensions->size() - 1); | 301 int offset = rand() % (extensions->size() - 1); |
| 288 for (int index = 0; index < offset; ++index) | 302 for (int index = 0; index < offset; ++index) |
| 289 ++cursor; | 303 ++cursor; |
| 290 } | 304 } |
| 291 scoped_refptr<Extension> extension = cursor->get(); | 305 scoped_refptr<Extension> extension = cursor->get(); |
| 292 std::string id = extension->id(); | 306 std::string id = extension->id(); |
| 293 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) { | 307 if (BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 308 service->profile())) { |
| 294 --*expected; | 309 --*expected; |
| 295 ASSERT_EQ(*count, extensions->size()); | 310 ASSERT_EQ(*count, extensions->size()); |
| 296 RemoveBackgroundPermission(service, extension); | 311 RemoveBackgroundPermission(service, extension); |
| 297 ASSERT_EQ(*count, service->extensions()->size()); | 312 ASSERT_EQ(*count, service->extensions()->size()); |
| 298 ASSERT_EQ(*expected, model->size()); | 313 ASSERT_EQ(*expected, model->size()); |
| 299 } else { | 314 } else { |
| 300 ++*expected; | 315 ++*expected; |
| 301 ASSERT_EQ(*count, extensions->size()); | 316 ASSERT_EQ(*count, extensions->size()); |
| 302 AddBackgroundPermission(service, extension); | 317 AddBackgroundPermission(service, extension); |
| 303 ASSERT_EQ(*count, service->extensions()->size()); | 318 ASSERT_EQ(*count, service->extensions()->size()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 break; | 350 break; |
| 336 case 2: | 351 case 2: |
| 337 TogglePermission(service, &extensions, model.get(), &expected, &count); | 352 TogglePermission(service, &extensions, model.get(), &expected, &count); |
| 338 break; | 353 break; |
| 339 default: | 354 default: |
| 340 NOTREACHED(); | 355 NOTREACHED(); |
| 341 break; | 356 break; |
| 342 } | 357 } |
| 343 } | 358 } |
| 344 } | 359 } |
| OLD | NEW |