1 // Copyright 2015 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.
5 #include "chrome/browser/extensions/extension_migrator.h"
7 #include "base/files/file_util.h"
8 #include "base/macros.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_service_test_base.h"
12 #include "chrome/browser/extensions/external_provider_impl.h"
13 #include "chrome/browser/extensions/pending_extension_manager.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/common/extension_builder.h"
18 namespace extensions
{
22 const char kOldId
[] = "oooooooooooooooooooooooooooooooo";
23 const char kNewId
[] = "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn";
25 scoped_refptr
<Extension
> CreateExtension(const std::string
& id
) {
26 return ExtensionBuilder()
27 .SetManifest(DictionaryBuilder().Set("name", "test").Set(
35 class ExtensionMigratorTest
: public ExtensionServiceTestBase
{
37 ExtensionMigratorTest() {}
38 ~ExtensionMigratorTest() override
{}
41 void InitWithExistingProfile() {
42 ExtensionServiceInitParams params
= CreateDefaultInitParams();
43 params
.is_first_run
= false;
44 // Create prefs file to make the profile not new.
45 const char prefs
[] = "{}";
46 EXPECT_EQ(int(sizeof(prefs
)),
47 base::WriteFile(params
.pref_file
, prefs
, sizeof(prefs
)));
48 InitializeExtensionService(params
);
50 AddMigratorProvider();
53 void AddMigratorProvider() {
54 service()->AddProviderForTesting(new ExternalProviderImpl(
56 new ExtensionMigrator(profile(), kOldId
, kNewId
),
58 Manifest::EXTERNAL_PREF
,
59 Manifest::EXTERNAL_PREF_DOWNLOAD
,
60 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
));
63 void AddExtension(const std::string
& id
) {
64 scoped_refptr
<Extension
> fake_app
= CreateExtension(id
);
65 service()->AddExtension(fake_app
.get());
68 bool HasNewExtension() {
69 return service()->pending_extension_manager()->IsIdPending(kNewId
) ||
70 !!registry()->GetInstalledExtension(kNewId
);
74 DISALLOW_COPY_AND_ASSIGN(ExtensionMigratorTest
);
77 TEST_F(ExtensionMigratorTest
, NoExistingOld
) {
78 InitWithExistingProfile();
79 service()->CheckForExternalUpdates();
80 base::RunLoop().RunUntilIdle();
81 EXPECT_FALSE(HasNewExtension());
84 TEST_F(ExtensionMigratorTest
, HasExistingOld
) {
85 InitWithExistingProfile();
87 service()->CheckForExternalUpdates();
88 base::RunLoop().RunUntilIdle();
89 EXPECT_TRUE(HasNewExtension());
90 EXPECT_TRUE(!!registry()->GetInstalledExtension(kOldId
));
93 TEST_F(ExtensionMigratorTest
, KeepExistingNew
) {
94 InitWithExistingProfile();
96 service()->CheckForExternalUpdates();
97 base::RunLoop().RunUntilIdle();
98 EXPECT_TRUE(!!registry()->GetInstalledExtension(kNewId
));
101 TEST_F(ExtensionMigratorTest
, HasBothOldAndNew
) {
102 InitWithExistingProfile();
103 AddExtension(kOldId
);
104 AddExtension(kNewId
);
105 service()->CheckForExternalUpdates();
106 base::RunLoop().RunUntilIdle();
107 EXPECT_TRUE(!!registry()->GetInstalledExtension(kOldId
));
108 EXPECT_TRUE(!!registry()->GetInstalledExtension(kNewId
));
111 } // namespace extensions