Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/extensions/app_data_migrator_unittest.cc

Issue 671873004: Migrates legacy packaged app data when it's upgraded to a platform app (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses cmumford comments Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
benwells 2015/01/14 03:34:28 Ditto with the year
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/callback_forward.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "chrome/browser/extensions/app_data_migrator.h"
11 #include "chrome/browser/extensions/extension_special_storage_policy.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/indexed_db_context.h"
15 #include "content/public/browser/storage_partition.h"
16 #include "content/public/test/mock_blob_url_request_context.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/common/manifest.h"
22 #include "storage/browser/fileapi/file_system_context.h"
23 #include "storage/browser/fileapi/file_system_operation_runner.h"
24 #include "storage/browser/fileapi/file_system_url.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace {
28 scoped_ptr<TestingProfile> GetTestingProfile() {
29 TestingProfile::Builder profile_builder;
30 return profile_builder.Build();
31 }
32 }
33 namespace extensions {
benwells 2015/01/14 03:34:28 Nit: blank line before namespace
34
35 class AppDataMigratorTest : public testing::Test {
36 public:
37 AppDataMigratorTest()
38 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
39
40 void SetUp() override {
41 profile_ = GetTestingProfile();
42 registry_ = ExtensionRegistry::Get(profile_.get());
43 migrator_ = scoped_ptr<AppDataMigrator>(
44 new AppDataMigrator(profile_.get(), registry_));
45
46 default_partition_ =
47 content::BrowserContext::GetDefaultStoragePartition(profile_.get());
48
49 idb_context_ = default_partition_->GetIndexedDBContext();
50 idb_context_->SetTaskRunnerForTesting(
51 base::MessageLoop::current()->message_loop_proxy().get());
52
53 default_fs_context_ = default_partition_->GetFileSystemContext();
54
55 url_request_context_ = scoped_ptr<content::MockBlobURLRequestContext>(
56 new content::MockBlobURLRequestContext(default_fs_context_));
57 }
58
59 void TearDown() override {}
60
61 protected:
62 content::TestBrowserThreadBundle thread_bundle_;
63 scoped_ptr<TestingProfile> profile_;
64 scoped_ptr<AppDataMigrator> migrator_;
65 content::StoragePartition* default_partition_;
66 ExtensionRegistry* registry_;
67 storage::FileSystemContext* default_fs_context_;
68 content::IndexedDBContext* idb_context_;
69 scoped_ptr<content::MockBlobURLRequestContext> url_request_context_;
70 };
71
72 scoped_refptr<const Extension> GetTestExtension(bool platform_app) {
73 scoped_refptr<const Extension> app;
74 if (platform_app) {
75 app = ExtensionBuilder()
76 .SetManifest(
77 DictionaryBuilder()
78 .Set("name", "test app")
79 .Set("version", "1")
80 .Set("app", DictionaryBuilder().Set(
81 "background",
82 DictionaryBuilder().Set(
83 "scripts", ListBuilder().Append(
84 "background.js"))))
85 .Set("permissions",
86 ListBuilder().Append("unlimitedStorage")))
87 .Build();
88 } else {
89 app = ExtensionBuilder()
90 .SetManifest(DictionaryBuilder()
91 .Set("name", "test app")
92 .Set("version", "1")
93 .Set("app", DictionaryBuilder().Set(
94 "launch",
95 DictionaryBuilder().Set(
96 "local_path", "index.html")))
97 .Set("permissions",
98 ListBuilder().Append("unlimitedStorage")))
99 .Build();
100 }
101 return app;
102 }
103
104 void MigrationCallback() {
105 }
106
107 void DidWrite(base::File::Error status, int64 bytes, bool complete) {
108 base::MessageLoop::current()->Quit();
109 }
110
111 void DidCreate(base::File::Error status) {
112 }
113
114 void DidOpenFileSystem(const GURL& root,
115 const std::string& name,
116 base::File::Error result) {
117 }
118
119 void OpenFileSystems(storage::FileSystemContext* fs_context,
120 GURL extension_url) {
121 fs_context->OpenFileSystem(extension_url, storage::kFileSystemTypeTemporary,
122 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
123 base::Bind(&DidOpenFileSystem));
124
125 fs_context->OpenFileSystem(extension_url, storage::kFileSystemTypePersistent,
126 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
127 base::Bind(&DidOpenFileSystem));
128 base::MessageLoop::current()->RunUntilIdle();
129 }
130
131 void GenerateTestFiles(content::MockBlobURLRequestContext* url_request_context,
132 const Extension* ext,
133 storage::FileSystemContext* fs_context,
134 Profile* profile) {
135 profile->GetExtensionSpecialStoragePolicy()->GrantRightsForExtension(ext,
136 profile);
137
138 base::FilePath path(FILE_PATH_LITERAL("test.txt"));
139 GURL extension_url =
140 extensions::Extension::GetBaseURLFromExtensionId(ext->id());
141
142 OpenFileSystems(fs_context, extension_url);
143
144 storage::FileSystemURL fs_temp_url = fs_context->CreateCrackedFileSystemURL(
145 extension_url, storage::kFileSystemTypeTemporary, path);
146
147 storage::FileSystemURL fs_persistent_url =
148 fs_context->CreateCrackedFileSystemURL(
149 extension_url, storage::kFileSystemTypePersistent, path);
150
151 content::ScopedTextBlob blob1(*url_request_context, "blob-id:success1",
152 "Hello, world!\n");
153
154 fs_context->operation_runner()->CreateFile(fs_temp_url, false,
155 base::Bind(&DidCreate));
156
157 fs_context->operation_runner()->CreateFile(fs_persistent_url, false,
158 base::Bind(&DidCreate));
159 base::MessageLoop::current()->RunUntilIdle();
160
161 fs_context->operation_runner()->Write(url_request_context, fs_temp_url,
162 blob1.GetBlobDataHandle(), 0,
163 base::Bind(&DidWrite));
164 base::MessageLoop::current()->Run();
165 fs_context->operation_runner()->Write(url_request_context, fs_persistent_url,
166 blob1.GetBlobDataHandle(), 0,
167 base::Bind(&DidWrite));
168 base::MessageLoop::current()->Run();
169 }
170
171 void VerifyFileContents(base::File file,
172 const base::Closure& on_close_callback) {
173 ASSERT_EQ(14, file.GetLength());
174 scoped_ptr<char[]> buffer(new char[15]);
175
176 file.Read(0, buffer.get(), 14);
177 buffer.get()[14] = 0;
178
179 std::string expected = "Hello, world!\n";
180 std::string actual = buffer.get();
181 EXPECT_EQ(expected, actual);
182
183 file.Close();
184 if (!on_close_callback.is_null())
185 on_close_callback.Run();
186 base::MessageLoop::current()->Quit();
187 }
188
189 void VerifyTestFilesMigrated(content::StoragePartition* new_partition,
190 const Extension* new_ext) {
191 GURL extension_url =
192 extensions::Extension::GetBaseURLFromExtensionId(new_ext->id());
193 storage::FileSystemContext* new_fs_context =
194 new_partition->GetFileSystemContext();
195
196 OpenFileSystems(new_fs_context, extension_url);
197
198 base::FilePath path(FILE_PATH_LITERAL("test.txt"));
199
200 storage::FileSystemURL fs_temp_url =
201 new_fs_context->CreateCrackedFileSystemURL(
202 extension_url, storage::kFileSystemTypeTemporary, path);
203 storage::FileSystemURL fs_persistent_url =
204 new_fs_context->CreateCrackedFileSystemURL(
205 extension_url, storage::kFileSystemTypePersistent, path);
206
207 new_fs_context->operation_runner()->OpenFile(
208 fs_temp_url, base::File::FLAG_READ | base::File::FLAG_OPEN,
209 base::Bind(&VerifyFileContents));
210 base::MessageLoop::current()->Run();
211 new_fs_context->operation_runner()->OpenFile(
212 fs_persistent_url, base::File::FLAG_READ | base::File::FLAG_OPEN,
213 base::Bind(&VerifyFileContents));
214 base::MessageLoop::current()->Run();
215 }
216
217 TEST_F(AppDataMigratorTest, ShouldMigrate) {
218 scoped_refptr<const Extension> old_ext = GetTestExtension(false);
219 scoped_refptr<const Extension> new_ext = GetTestExtension(true);
220
221 EXPECT_TRUE(AppDataMigrator::NeedsMigration(old_ext.get(), new_ext.get()));
222 }
223
224 TEST_F(AppDataMigratorTest, ShouldNotMigratePlatformApp) {
225 scoped_refptr<const Extension> old_ext = GetTestExtension(true);
226 scoped_refptr<const Extension> new_ext = GetTestExtension(true);
227
228 EXPECT_FALSE(AppDataMigrator::NeedsMigration(old_ext.get(), new_ext.get()));
229 }
230
231 TEST_F(AppDataMigratorTest, ShouldNotMigrateLegacyApp) {
232 scoped_refptr<const Extension> old_ext = GetTestExtension(false);
233 scoped_refptr<const Extension> new_ext = GetTestExtension(false);
234
235 EXPECT_FALSE(AppDataMigrator::NeedsMigration(old_ext.get(), new_ext.get()));
236 }
237
238 TEST_F(AppDataMigratorTest, NoOpMigration) {
239 scoped_refptr<const Extension> old_ext = GetTestExtension(false);
240 scoped_refptr<const Extension> new_ext = GetTestExtension(true);
241
242 // Nothing to migrate. Basically this should just not cause an error
243 migrator_->DoMigrationAndReply(old_ext.get(), new_ext.get(),
244 base::Bind(&MigrationCallback));
245 }
246
247 TEST_F(AppDataMigratorTest, FileSystemMigration) {
248 scoped_refptr<const Extension> old_ext = GetTestExtension(false);
249 scoped_refptr<const Extension> new_ext = GetTestExtension(true);
250
251 GenerateTestFiles(url_request_context_.get(), old_ext.get(),
252 default_fs_context_, profile_.get());
253
254 migrator_->DoMigrationAndReply(old_ext.get(), new_ext.get(),
255 base::Bind(&MigrationCallback));
256
257 base::MessageLoop::current()->RunUntilIdle();
258
259 registry_->AddEnabled(new_ext);
260 GURL extension_url =
261 extensions::Extension::GetBaseURLFromExtensionId(new_ext->id());
262
263 content::StoragePartition* new_partition =
264 content::BrowserContext::GetStoragePartitionForSite(profile_.get(),
265 extension_url);
266
267 ASSERT_NE(new_partition->GetPath(), default_partition_->GetPath());
268
269 VerifyTestFilesMigrated(new_partition, new_ext.get());
270 }
271 } // namespace extensions
benwells 2015/01/14 03:34:28 Nit: blank line before namespace end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698