Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / install_tracker_unittest.cc
blob179f6276f5e79c6d0edb24a0ba4bfe06312cf918
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 "extensions/browser/extension_registry.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using extensions::ActiveInstallData;
15 using extensions::Extension;
16 using extensions::ExtensionRegistry;
17 using extensions::InstallTracker;
18 using extensions::InstallObserver;
19 using extensions::ScopedActiveInstall;
21 namespace {
23 // Random extension ids for testing.
24 const char kExtensionId1[] = "oochhailbdickimldhhodijaldpllppf";
25 const char kExtensionId2[] = "ahionppacfhbbmpmlcbkdgcpokfpflji";
26 const char kExtensionId3[] = "ladmcjmmmmgonboiadnaindoekpbljde";
28 scoped_refptr<Extension> CreateDummyExtension(const std::string& id) {
29 base::DictionaryValue manifest;
30 manifest.SetString(extensions::manifest_keys::kVersion, "1.0");
31 manifest.SetString(extensions::manifest_keys::kName, "Dummy name");
33 std::string error;
34 scoped_refptr<Extension> extension;
35 extension = Extension::Create(base::FilePath(),
36 extensions::Manifest::INTERNAL,
37 manifest,
38 Extension::NO_FLAGS,
39 id,
40 &error);
41 EXPECT_TRUE(extension.get()) << "Error creating extension: " << error;
42 return extension;
45 } // namespace
47 class InstallTrackerTest : public testing::Test {
48 public:
49 InstallTrackerTest() {
50 profile_.reset(new TestingProfile());
51 tracker_.reset(new InstallTracker(profile_.get(), NULL));
54 virtual ~InstallTrackerTest() {}
56 protected:
57 Profile* profile() { return profile_.get(); }
58 InstallTracker* tracker() { return tracker_.get(); }
60 void VerifyInstallData(const ActiveInstallData& original,
61 const ActiveInstallData& retrieved) {
62 EXPECT_EQ(original.extension_id, retrieved.extension_id);
63 EXPECT_EQ(original.is_ephemeral, retrieved.is_ephemeral);
64 EXPECT_EQ(original.percent_downloaded, retrieved.percent_downloaded);
67 scoped_ptr<TestingProfile> profile_;
68 scoped_ptr<InstallTracker> tracker_;
71 // Verifies that active installs are registered and deregistered correctly.
72 TEST_F(InstallTrackerTest, AddAndRemoveActiveInstalls) {
73 ActiveInstallData install_data1(kExtensionId1);
74 install_data1.percent_downloaded = 76;
75 ActiveInstallData install_data2(kExtensionId2);
76 install_data2.is_ephemeral = true;
78 tracker_->AddActiveInstall(install_data1);
79 tracker_->AddActiveInstall(install_data2);
81 const ActiveInstallData* retrieved_data1 =
82 tracker_->GetActiveInstall(kExtensionId1);
83 const ActiveInstallData* retrieved_data2 =
84 tracker_->GetActiveInstall(kExtensionId2);
85 const ActiveInstallData* retrieved_data3 =
86 tracker_->GetActiveInstall(kExtensionId3);
87 ASSERT_TRUE(retrieved_data1);
88 ASSERT_TRUE(retrieved_data2);
89 ASSERT_FALSE(retrieved_data3);
90 VerifyInstallData(install_data1, *retrieved_data1);
91 VerifyInstallData(install_data2, *retrieved_data2);
92 retrieved_data1 = NULL;
93 retrieved_data2 = NULL;
95 tracker_->RemoveActiveInstall(kExtensionId1);
96 EXPECT_FALSE(tracker_->GetActiveInstall(kExtensionId1));
97 EXPECT_TRUE(tracker_->GetActiveInstall(kExtensionId2));
98 EXPECT_FALSE(tracker_->GetActiveInstall(kExtensionId3));
101 // Verifies that active installs are registered and deregistered correctly
102 // using ScopedActiveInstall.
103 TEST_F(InstallTrackerTest, ScopedActiveInstallDeregister) {
104 // Verify the constructor that registers the install.
105 ActiveInstallData install_data(kExtensionId1);
106 install_data.percent_downloaded = 6;
107 scoped_ptr<ScopedActiveInstall> scoped_active_install(
108 new ScopedActiveInstall(tracker(), install_data));
110 const ActiveInstallData* retrieved_data =
111 tracker_->GetActiveInstall(kExtensionId1);
112 ASSERT_TRUE(retrieved_data);
113 VerifyInstallData(install_data, *retrieved_data);
114 retrieved_data = NULL;
116 scoped_active_install.reset();
117 EXPECT_FALSE(tracker_->GetActiveInstall(kExtensionId1));
119 // Verify the constructor that doesn't register the install.
120 scoped_active_install.reset(
121 new ScopedActiveInstall(tracker(), kExtensionId1));
122 EXPECT_FALSE(tracker_->GetActiveInstall(kExtensionId1));
124 tracker_->AddActiveInstall(install_data);
125 EXPECT_TRUE(tracker_->GetActiveInstall(kExtensionId1));
127 scoped_active_install.reset();
128 EXPECT_FALSE(tracker_->GetActiveInstall(kExtensionId1));
131 // Verifies that ScopedActiveInstall can be cancelled.
132 TEST_F(InstallTrackerTest, ScopedActiveInstallCancelled) {
133 ActiveInstallData install_data(kExtensionId1);
134 install_data.percent_downloaded = 87;
135 scoped_ptr<ScopedActiveInstall> scoped_active_install(
136 new ScopedActiveInstall(tracker(), install_data));
138 const ActiveInstallData* retrieved_data =
139 tracker_->GetActiveInstall(kExtensionId1);
140 ASSERT_TRUE(retrieved_data);
141 VerifyInstallData(install_data, *retrieved_data);
142 retrieved_data = NULL;
144 scoped_active_install->CancelDeregister();
145 scoped_active_install.reset();
147 retrieved_data = tracker_->GetActiveInstall(kExtensionId1);
148 ASSERT_TRUE(retrieved_data);
149 VerifyInstallData(install_data, *retrieved_data);
152 // Verifies that the download progress is updated correctly.
153 TEST_F(InstallTrackerTest, DownloadProgressUpdated) {
154 ActiveInstallData install_data(kExtensionId1);
155 tracker_->AddActiveInstall(install_data);
157 const ActiveInstallData* retrieved_data =
158 tracker_->GetActiveInstall(kExtensionId1);
159 ASSERT_TRUE(retrieved_data);
160 EXPECT_EQ(0, retrieved_data->percent_downloaded);
162 const int kUpdatedDownloadProgress = 23;
163 tracker_->OnDownloadProgress(kExtensionId1, kUpdatedDownloadProgress);
165 retrieved_data = tracker_->GetActiveInstall(kExtensionId1);
166 ASSERT_TRUE(retrieved_data);
167 EXPECT_EQ(kUpdatedDownloadProgress, retrieved_data->percent_downloaded);
170 // Verifies that OnBeginExtensionInstall() registers an active install and
171 // OnInstallFailure() removes an active install.
172 TEST_F(InstallTrackerTest, ExtensionInstallFailure) {
173 InstallObserver::ExtensionInstallParams install_params(
174 kExtensionId1, std::string(), gfx::ImageSkia(), false, false);
175 install_params.is_ephemeral = true;
176 tracker_->OnBeginExtensionInstall(install_params);
178 const ActiveInstallData* retrieved_data =
179 tracker_->GetActiveInstall(kExtensionId1);
180 ASSERT_TRUE(retrieved_data);
181 EXPECT_EQ(0, retrieved_data->percent_downloaded);
182 EXPECT_EQ(install_params.extension_id, retrieved_data->extension_id);
183 EXPECT_EQ(install_params.is_ephemeral, retrieved_data->is_ephemeral);
184 retrieved_data = NULL;
186 tracker_->OnInstallFailure(kExtensionId1);
187 EXPECT_FALSE(tracker_->GetActiveInstall(kExtensionId1));
190 // Verifies that OnExtensionInstalled() notification removes an active install.
191 TEST_F(InstallTrackerTest, ExtensionInstalledEvent) {
192 InstallObserver::ExtensionInstallParams install_params(
193 kExtensionId1, std::string(), gfx::ImageSkia(), false, false);
194 tracker_->OnBeginExtensionInstall(install_params);
196 const ActiveInstallData* retrieved_data =
197 tracker_->GetActiveInstall(kExtensionId1);
198 ASSERT_TRUE(retrieved_data);
199 EXPECT_EQ(0, retrieved_data->percent_downloaded);
200 EXPECT_EQ(install_params.extension_id, retrieved_data->extension_id);
201 EXPECT_EQ(install_params.is_ephemeral, retrieved_data->is_ephemeral);
202 retrieved_data = NULL;
204 // Simulate an extension install.
205 scoped_refptr<Extension> extension = CreateDummyExtension(kExtensionId1);
206 ASSERT_TRUE(extension.get());
207 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
208 ASSERT_TRUE(registry);
209 registry->AddEnabled(extension);
210 registry->TriggerOnInstalled(extension.get(), false);
212 EXPECT_FALSE(tracker_->GetActiveInstall(kExtensionId1));