| 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" |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/extensions/extension_service_test_base.h" | 18 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 19 #include "chrome/browser/extensions/permissions_updater.h" | 19 #include "chrome/browser/extensions/permissions_updater.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
| 22 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
| 23 #include "extensions/browser/extension_prefs.h" |
| 23 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
| 24 #include "extensions/browser/uninstall_reason.h" | 25 #include "extensions/browser/uninstall_reason.h" |
| 25 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/manifest_constants.h" | 27 #include "extensions/common/manifest_constants.h" |
| 27 #include "extensions/common/permissions/api_permission.h" | 28 #include "extensions/common/permissions/api_permission.h" |
| 28 #include "extensions/common/permissions/permission_set.h" | 29 #include "extensions/common/permissions/permission_set.h" |
| 29 #include "extensions/common/permissions/permissions_data.h" | 30 #include "extensions/common/permissions/permissions_data.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 32 |
| 32 // This value is used to seed the PRNG at the beginning of a sequence of | 33 // This value is used to seed the PRNG at the beginning of a sequence of |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 150 |
| 150 void RemoveBackgroundPermission(ExtensionService* service, | 151 void RemoveBackgroundPermission(ExtensionService* service, |
| 151 Extension* extension) { | 152 Extension* extension) { |
| 152 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension, | 153 if (!BackgroundApplicationListModel::IsBackgroundApp(*extension, |
| 153 service->profile())) { | 154 service->profile())) { |
| 154 return; | 155 return; |
| 155 } | 156 } |
| 156 extensions::PermissionsUpdater(service->profile()).RemovePermissions( | 157 extensions::PermissionsUpdater(service->profile()).RemovePermissions( |
| 157 extension, extension->permissions_data()->active_permissions().get()); | 158 extension, extension->permissions_data()->active_permissions().get()); |
| 158 } | 159 } |
| 160 |
| 161 void AddEphemeralApp(const Extension* extension, ExtensionService* service) { |
| 162 extensions::ExtensionPrefs* prefs = |
| 163 extensions::ExtensionPrefs::Get(service->profile()); |
| 164 ASSERT_TRUE(prefs); |
| 165 prefs->OnExtensionInstalled(extension, |
| 166 extensions::Extension::ENABLED, |
| 167 syncer::StringOrdinal(), |
| 168 extensions::kInstallFlagIsEphemeral, |
| 169 std::string()); |
| 170 |
| 171 service->AddExtension(extension); |
| 172 } |
| 173 |
| 159 } // namespace | 174 } // namespace |
| 160 | 175 |
| 161 // Crashes on Mac tryslaves. | 176 // Crashes on Mac tryslaves. |
| 162 // http://crbug.com/165458 | 177 // http://crbug.com/165458 |
| 163 #if defined(OS_MACOSX) || defined(OS_LINUX) | 178 #if defined(OS_MACOSX) || defined(OS_LINUX) |
| 164 #define MAYBE_ExplicitTest DISABLED_ExplicitTest | 179 #define MAYBE_ExplicitTest DISABLED_ExplicitTest |
| 165 #else | 180 #else |
| 166 #define MAYBE_ExplicitTest ExplicitTest | 181 #define MAYBE_ExplicitTest ExplicitTest |
| 167 #endif | 182 #endif |
| 168 // With minimal test logic, verifies behavior over an explicit set of | 183 // With minimal test logic, verifies behavior over an explicit set of |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 bgapp2->id(), extensions::UNINSTALL_REASON_FOR_TESTING, NULL); | 324 bgapp2->id(), extensions::UNINSTALL_REASON_FOR_TESTING, NULL); |
| 310 ASSERT_EQ(1U, service->extensions()->size()); | 325 ASSERT_EQ(1U, service->extensions()->size()); |
| 311 ASSERT_EQ(1U, model->size()); | 326 ASSERT_EQ(1U, model->size()); |
| 312 ASSERT_TRUE(IsBackgroundApp(*bgapp3.get())); | 327 ASSERT_TRUE(IsBackgroundApp(*bgapp3.get())); |
| 313 service->UninstallExtension( | 328 service->UninstallExtension( |
| 314 bgapp3->id(), extensions::UNINSTALL_REASON_FOR_TESTING, NULL); | 329 bgapp3->id(), extensions::UNINSTALL_REASON_FOR_TESTING, NULL); |
| 315 ASSERT_EQ(0U, service->extensions()->size()); | 330 ASSERT_EQ(0U, service->extensions()->size()); |
| 316 ASSERT_EQ(0U, model->size()); | 331 ASSERT_EQ(0U, model->size()); |
| 317 } | 332 } |
| 318 | 333 |
| 334 // Verifies that an ephemeral app cannot trigger background mode. |
| 335 TEST_F(BackgroundApplicationListModelTest, EphemeralAppTest) { |
| 336 InitializeAndLoadEmptyExtensionService(); |
| 337 ExtensionService* service = extensions::ExtensionSystem::Get(profile_.get())-> |
| 338 extension_service(); |
| 339 ASSERT_TRUE(service); |
| 340 ASSERT_TRUE(service->is_ready()); |
| 341 ASSERT_TRUE(service->extensions()); |
| 342 ASSERT_TRUE(service->extensions()->is_empty()); |
| 343 scoped_ptr<BackgroundApplicationListModel> model( |
| 344 new BackgroundApplicationListModel(profile_.get())); |
| 345 ASSERT_EQ(0U, model->size()); |
| 319 | 346 |
| 347 scoped_refptr<Extension> installed = |
| 348 CreateExtensionBase("installed", false, PUSH_MESSAGING_PERMISSION); |
| 349 scoped_refptr<Extension> ephemeral = |
| 350 CreateExtensionBase("ephemeral", false, PUSH_MESSAGING_PERMISSION); |
| 351 scoped_refptr<Extension> background = CreateExtension("background", true); |
| 352 |
| 353 // Installed app with push messaging permissions can trigger background mode. |
| 354 ASSERT_TRUE(IsBackgroundApp(*installed.get())); |
| 355 service->AddExtension(installed.get()); |
| 356 ASSERT_EQ(1U, service->extensions()->size()); |
| 357 ASSERT_EQ(1U, model->size()); |
| 358 // An ephemeral app with push messaging permissions should not trigger |
| 359 // background mode. |
| 360 AddEphemeralApp(ephemeral.get(), service); |
| 361 ASSERT_FALSE(IsBackgroundApp(*ephemeral.get())); |
| 362 ASSERT_EQ(2U, service->extensions()->size()); |
| 363 ASSERT_EQ(1U, model->size()); |
| 364 // An ephemeral app with the background permission should not trigger |
| 365 // background mode. |
| 366 AddEphemeralApp(background.get(), service); |
| 367 ASSERT_FALSE(IsBackgroundApp(*background.get())); |
| 368 ASSERT_EQ(3U, service->extensions()->size()); |
| 369 ASSERT_EQ(1U, model->size()); |
| 370 |
| 371 // If the ephemeral app becomes promoted to an installed app, it can now |
| 372 // trigger background mode. |
| 373 service->PromoteEphemeralApp(ephemeral.get(), false /*from sync*/); |
| 374 ASSERT_TRUE(IsBackgroundApp(*ephemeral.get())); |
| 375 ASSERT_EQ(3U, service->extensions()->size()); |
| 376 ASSERT_EQ(2U, model->size()); |
| 377 } |
| 320 | 378 |
| 321 // With minimal test logic, verifies behavior with dynamic permissions. | 379 // With minimal test logic, verifies behavior with dynamic permissions. |
| 322 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { | 380 TEST_F(BackgroundApplicationListModelTest, AddRemovePermissionsTest) { |
| 323 InitializeAndLoadEmptyExtensionService(); | 381 InitializeAndLoadEmptyExtensionService(); |
| 324 ExtensionService* service = extensions::ExtensionSystem::Get(profile_.get())-> | 382 ExtensionService* service = extensions::ExtensionSystem::Get(profile_.get())-> |
| 325 extension_service(); | 383 extension_service(); |
| 326 ASSERT_TRUE(service); | 384 ASSERT_TRUE(service); |
| 327 ASSERT_TRUE(service->is_ready()); | 385 ASSERT_TRUE(service->is_ready()); |
| 328 ASSERT_TRUE(service->extensions()); | 386 ASSERT_TRUE(service->extensions()); |
| 329 ASSERT_TRUE(service->extensions()->is_empty()); | 387 ASSERT_TRUE(service->extensions()->is_empty()); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 break; | 561 break; |
| 504 case 2: | 562 case 2: |
| 505 TogglePermission(service, &extensions, model.get(), &expected, &count); | 563 TogglePermission(service, &extensions, model.get(), &expected, &count); |
| 506 break; | 564 break; |
| 507 default: | 565 default: |
| 508 NOTREACHED(); | 566 NOTREACHED(); |
| 509 break; | 567 break; |
| 510 } | 568 } |
| 511 } | 569 } |
| 512 } | 570 } |
| OLD | NEW |