1 // Copyright 2014 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 "base/files/file_path.h"
6 #include "chrome/browser/extensions/active_install_data.h"
7 #include "chrome/browser/extensions/install_tracker.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "extensions/browser/extension_registry.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using extensions::ActiveInstallData
;
16 using extensions::Extension
;
17 using extensions::ExtensionRegistry
;
18 using extensions::InstallTracker
;
19 using extensions::InstallObserver
;
20 using extensions::ScopedActiveInstall
;
24 // Random extension ids for testing.
25 const char kExtensionId1
[] = "oochhailbdickimldhhodijaldpllppf";
26 const char kExtensionId2
[] = "ahionppacfhbbmpmlcbkdgcpokfpflji";
27 const char kExtensionId3
[] = "ladmcjmmmmgonboiadnaindoekpbljde";
29 scoped_refptr
<Extension
> CreateDummyExtension(const std::string
& id
) {
30 base::DictionaryValue manifest
;
31 manifest
.SetString(extensions::manifest_keys::kVersion
, "1.0");
32 manifest
.SetString(extensions::manifest_keys::kName
, "Dummy name");
35 scoped_refptr
<Extension
> extension
;
36 extension
= Extension::Create(base::FilePath(),
37 extensions::Manifest::INTERNAL
,
42 EXPECT_TRUE(extension
.get()) << "Error creating extension: " << error
;
48 class InstallTrackerTest
: public testing::Test
{
50 InstallTrackerTest() {
51 profile_
.reset(new TestingProfile());
52 tracker_
.reset(new InstallTracker(profile_
.get(), NULL
));
55 ~InstallTrackerTest() override
{}
58 Profile
* profile() { return profile_
.get(); }
59 InstallTracker
* tracker() { return tracker_
.get(); }
61 void VerifyInstallData(const ActiveInstallData
& original
,
62 const ActiveInstallData
& retrieved
) {
63 EXPECT_EQ(original
.extension_id
, retrieved
.extension_id
);
64 EXPECT_EQ(original
.is_ephemeral
, retrieved
.is_ephemeral
);
65 EXPECT_EQ(original
.percent_downloaded
, retrieved
.percent_downloaded
);
68 content::TestBrowserThreadBundle thread_bundle_
;
69 scoped_ptr
<TestingProfile
> profile_
;
70 scoped_ptr
<InstallTracker
> tracker_
;
73 // Verifies that active installs are registered and deregistered correctly.
74 TEST_F(InstallTrackerTest
, AddAndRemoveActiveInstalls
) {
75 ActiveInstallData
install_data1(kExtensionId1
);
76 install_data1
.percent_downloaded
= 76;
77 ActiveInstallData
install_data2(kExtensionId2
);
78 install_data2
.is_ephemeral
= true;
80 tracker_
->AddActiveInstall(install_data1
);
81 tracker_
->AddActiveInstall(install_data2
);
83 const ActiveInstallData
* retrieved_data1
=
84 tracker_
->GetActiveInstall(kExtensionId1
);
85 const ActiveInstallData
* retrieved_data2
=
86 tracker_
->GetActiveInstall(kExtensionId2
);
87 const ActiveInstallData
* retrieved_data3
=
88 tracker_
->GetActiveInstall(kExtensionId3
);
89 ASSERT_TRUE(retrieved_data1
);
90 ASSERT_TRUE(retrieved_data2
);
91 ASSERT_FALSE(retrieved_data3
);
92 VerifyInstallData(install_data1
, *retrieved_data1
);
93 VerifyInstallData(install_data2
, *retrieved_data2
);
94 retrieved_data1
= NULL
;
95 retrieved_data2
= NULL
;
97 tracker_
->RemoveActiveInstall(kExtensionId1
);
98 EXPECT_FALSE(tracker_
->GetActiveInstall(kExtensionId1
));
99 EXPECT_TRUE(tracker_
->GetActiveInstall(kExtensionId2
));
100 EXPECT_FALSE(tracker_
->GetActiveInstall(kExtensionId3
));
103 // Verifies that active installs are registered and deregistered correctly
104 // using ScopedActiveInstall.
105 TEST_F(InstallTrackerTest
, ScopedActiveInstallDeregister
) {
106 // Verify the constructor that registers the install.
107 ActiveInstallData
install_data(kExtensionId1
);
108 install_data
.percent_downloaded
= 6;
109 scoped_ptr
<ScopedActiveInstall
> scoped_active_install(
110 new ScopedActiveInstall(tracker(), install_data
));
112 const ActiveInstallData
* retrieved_data
=
113 tracker_
->GetActiveInstall(kExtensionId1
);
114 ASSERT_TRUE(retrieved_data
);
115 VerifyInstallData(install_data
, *retrieved_data
);
116 retrieved_data
= NULL
;
118 scoped_active_install
.reset();
119 EXPECT_FALSE(tracker_
->GetActiveInstall(kExtensionId1
));
121 // Verify the constructor that doesn't register the install.
122 scoped_active_install
.reset(
123 new ScopedActiveInstall(tracker(), kExtensionId1
));
124 EXPECT_FALSE(tracker_
->GetActiveInstall(kExtensionId1
));
126 tracker_
->AddActiveInstall(install_data
);
127 EXPECT_TRUE(tracker_
->GetActiveInstall(kExtensionId1
));
129 scoped_active_install
.reset();
130 EXPECT_FALSE(tracker_
->GetActiveInstall(kExtensionId1
));
133 // Verifies that ScopedActiveInstall can be cancelled.
134 TEST_F(InstallTrackerTest
, ScopedActiveInstallCancelled
) {
135 ActiveInstallData
install_data(kExtensionId1
);
136 install_data
.percent_downloaded
= 87;
137 scoped_ptr
<ScopedActiveInstall
> scoped_active_install(
138 new ScopedActiveInstall(tracker(), install_data
));
140 const ActiveInstallData
* retrieved_data
=
141 tracker_
->GetActiveInstall(kExtensionId1
);
142 ASSERT_TRUE(retrieved_data
);
143 VerifyInstallData(install_data
, *retrieved_data
);
144 retrieved_data
= NULL
;
146 scoped_active_install
->CancelDeregister();
147 scoped_active_install
.reset();
149 retrieved_data
= tracker_
->GetActiveInstall(kExtensionId1
);
150 ASSERT_TRUE(retrieved_data
);
151 VerifyInstallData(install_data
, *retrieved_data
);
154 // Verifies that the download progress is updated correctly.
155 TEST_F(InstallTrackerTest
, DownloadProgressUpdated
) {
156 ActiveInstallData
install_data(kExtensionId1
);
157 tracker_
->AddActiveInstall(install_data
);
159 const ActiveInstallData
* retrieved_data
=
160 tracker_
->GetActiveInstall(kExtensionId1
);
161 ASSERT_TRUE(retrieved_data
);
162 EXPECT_EQ(0, retrieved_data
->percent_downloaded
);
164 const int kUpdatedDownloadProgress
= 23;
165 tracker_
->OnDownloadProgress(kExtensionId1
, kUpdatedDownloadProgress
);
167 retrieved_data
= tracker_
->GetActiveInstall(kExtensionId1
);
168 ASSERT_TRUE(retrieved_data
);
169 EXPECT_EQ(kUpdatedDownloadProgress
, retrieved_data
->percent_downloaded
);
172 // Verifies that OnBeginExtensionInstall() registers an active install and
173 // OnInstallFailure() removes an active install.
174 TEST_F(InstallTrackerTest
, ExtensionInstallFailure
) {
175 InstallObserver::ExtensionInstallParams
install_params(
176 kExtensionId1
, std::string(), gfx::ImageSkia(), false, false);
177 install_params
.is_ephemeral
= true;
178 tracker_
->OnBeginExtensionInstall(install_params
);
180 const ActiveInstallData
* retrieved_data
=
181 tracker_
->GetActiveInstall(kExtensionId1
);
182 ASSERT_TRUE(retrieved_data
);
183 EXPECT_EQ(0, retrieved_data
->percent_downloaded
);
184 EXPECT_EQ(install_params
.extension_id
, retrieved_data
->extension_id
);
185 EXPECT_EQ(install_params
.is_ephemeral
, retrieved_data
->is_ephemeral
);
186 retrieved_data
= NULL
;
188 tracker_
->OnInstallFailure(kExtensionId1
);
189 EXPECT_FALSE(tracker_
->GetActiveInstall(kExtensionId1
));
192 // Verifies that OnExtensionInstalled() notification removes an active install.
193 TEST_F(InstallTrackerTest
, ExtensionInstalledEvent
) {
194 InstallObserver::ExtensionInstallParams
install_params(
195 kExtensionId1
, std::string(), gfx::ImageSkia(), false, false);
196 tracker_
->OnBeginExtensionInstall(install_params
);
198 const ActiveInstallData
* retrieved_data
=
199 tracker_
->GetActiveInstall(kExtensionId1
);
200 ASSERT_TRUE(retrieved_data
);
201 EXPECT_EQ(0, retrieved_data
->percent_downloaded
);
202 EXPECT_EQ(install_params
.extension_id
, retrieved_data
->extension_id
);
203 EXPECT_EQ(install_params
.is_ephemeral
, retrieved_data
->is_ephemeral
);
204 retrieved_data
= NULL
;
206 // Simulate an extension install.
207 scoped_refptr
<Extension
> extension
= CreateDummyExtension(kExtensionId1
);
208 ASSERT_TRUE(extension
.get());
209 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
210 ASSERT_TRUE(registry
);
211 registry
->AddEnabled(extension
);
212 registry
->TriggerOnInstalled(extension
.get(), false);
214 EXPECT_FALSE(tracker_
->GetActiveInstall(kExtensionId1
));