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

Side by Side Diff: webkit/blob/blob_storage_context_unittest.cc

Issue 14139026: New blobstoragecontext for use in the main browser process, not plugged in yet. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h"
8 #include "base/time.h" 9 #include "base/time.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/blob/blob_data.h" 11 #include "webkit/blob/blob_data.h"
11 #include "webkit/blob/blob_storage_controller.h" 12 #include "webkit/blob/blob_storage_context.h"
12 13
13 namespace webkit_blob { 14 namespace webkit_blob {
14 15
15 TEST(BlobStorageControllerTest, RegisterBlobUrl) { 16 namespace {
17 void SetupBasicBlob(BlobStorageHost* host, const std::string& id) {
18 host->StartBuildingBlob(id);
19 BlobData::Item item;
20 item.SetToBytes("1", 1);
21 host->AppendBlobDataItem(id, item);
22 host->FinishBuildingBlob(id, "text/plain");
23 }
24 } // anon namespace
25
26 TEST(BlobStorageContextTest, IncrementDecrementRef) {
27 BlobStorageContext context;
28 BlobStorageHost host(&context);
29 MessageLoop fake_io_message_loop;
30
31 // Build up a basic blob.
32 const std::string kId("id");
33 SetupBasicBlob(&host, kId);
34
35 // Make sure its there, finish building implies a ref of one.
ericu 2013/04/18 02:01:58 s/its/it's
michaeln 2013/04/20 00:31:02 Done.
36 scoped_ptr<BlobDataHandle> blob_data_handle;
37 blob_data_handle = context.GetBlobDataFromUUID(kId);
38 EXPECT_TRUE(blob_data_handle);
39 blob_data_handle.reset();
40
41 // Make sure its still there after inc/dec.
42 host.IncrementBlobRefCount(kId);
43 host.DecrementBlobRefCount(kId);
44 blob_data_handle = context.GetBlobDataFromUUID(kId);
45 EXPECT_TRUE(blob_data_handle);
46 blob_data_handle.reset();
47
48 // Make sure it goes away in the end.
49 host.DecrementBlobRefCount(kId);
50 blob_data_handle = context.GetBlobDataFromUUID(kId);
51 EXPECT_FALSE(blob_data_handle);
52 }
53
54 TEST(BlobStorageContextTest, BlobDataHandle) {
55 BlobStorageContext context;
56 BlobStorageHost host(&context);
57 MessageLoop fake_io_message_loop;
58
59 // Build up a basic blob.
60 const std::string kId("id");
61 SetupBasicBlob(&host, kId);
62
63 // Get a handle to it.
64 scoped_ptr<BlobDataHandle> blob_data_handle =
65 context.GetBlobDataFromUUID(kId);
66 EXPECT_TRUE(blob_data_handle);
67
68 // Drop the hosts ref to it.
ericu 2013/04/18 02:01:58 s/hosts/host's/
michaeln 2013/04/20 00:31:02 Done.
69 host.DecrementBlobRefCount(kId);
70
71 // Should still be there due to the handle.
72 scoped_ptr<BlobDataHandle> another_handle =
73 context.GetBlobDataFromUUID(kId);
74 EXPECT_TRUE(another_handle);
75
76 // Should disappear after dropping both handles.
77 blob_data_handle.reset();
78 another_handle.reset();
79 blob_data_handle = context.GetBlobDataFromUUID(kId);
80 EXPECT_FALSE(blob_data_handle);
81 }
82
83
84 TEST(BlobStorageContextTest, CompoundBlobs) {
85 const std::string kId1("id1");
86 const std::string kId2("id2");
87 const std::string kId2Prime("id2.prime");
88
89 MessageLoop fake_io_message_loop;
90
16 // Setup a set of blob data for testing. 91 // Setup a set of blob data for testing.
17 base::Time time1, time2; 92 base::Time time1, time2;
18 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); 93 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1);
19 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2); 94 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2);
20 95
21 scoped_refptr<BlobData> blob_data1(new BlobData()); 96 scoped_refptr<BlobData> blob_data1(new BlobData(kId1));
22 blob_data1->AppendData("Data1"); 97 blob_data1->AppendData("Data1");
23 blob_data1->AppendData("Data2"); 98 blob_data1->AppendData("Data2");
24 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), 99 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")),
25 10, 1024, time1); 100 10, 1024, time1);
26 101
27 scoped_refptr<BlobData> blob_data2(new BlobData()); 102 scoped_refptr<BlobData> blob_data2(new BlobData(kId2));
28 blob_data2->AppendData("Data3"); 103 blob_data2->AppendData("Data3");
29 blob_data2->AppendBlob(GURL("blob://url_1"), 8, 100); 104 blob_data2->AppendBlob(kId1, 8, 100);
30 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), 105 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")),
31 0, 20, time2); 106 0, 20, time2);
32 107
33 scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData()); 108 scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData(kId2Prime));
34 canonicalized_blob_data2->AppendData("Data3"); 109 canonicalized_blob_data2->AppendData("Data3");
35 canonicalized_blob_data2->AppendData("a2___", 2); 110 canonicalized_blob_data2->AppendData("a2___", 2);
36 canonicalized_blob_data2->AppendFile( 111 canonicalized_blob_data2->AppendFile(
37 base::FilePath(FILE_PATH_LITERAL("File1.txt")), 112 base::FilePath(FILE_PATH_LITERAL("File1.txt")),
38 10, 98, time1); 113 10, 98, time1);
39 canonicalized_blob_data2->AppendFile( 114 canonicalized_blob_data2->AppendFile(
40 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); 115 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2);
41 116
42 BlobStorageController blob_storage_controller; 117 BlobStorageContext context;
118 BlobStorageHost host(&context);
119 scoped_ptr<BlobDataHandle> blob_data_handle;
43 120
44 // Test registering a blob URL referring to the blob data containing only 121 // Test a blob referring to only data and a file.
45 // data and file. 122 host.AddFinishedBlob(blob_data1);
46 GURL blob_url1("blob://url_1"); 123 blob_data_handle = context.GetBlobDataFromUUID(kId1);
47 blob_storage_controller.AddFinishedBlob(blob_url1, blob_data1); 124 ASSERT_TRUE(blob_data_handle.get());
125 EXPECT_TRUE(*(blob_data_handle->data()) == *blob_data1);
48 126
49 BlobData* blob_data_found = 127 // Test a blob composed in part with another blob.
50 blob_storage_controller.GetBlobDataFromUrl(blob_url1); 128 host.AddFinishedBlob(blob_data2);
51 ASSERT_TRUE(blob_data_found != NULL); 129 blob_data_handle = context.GetBlobDataFromUUID(kId2);
52 EXPECT_TRUE(*blob_data_found == *blob_data1); 130 ASSERT_TRUE(blob_data_handle.get());
131 EXPECT_TRUE(*(blob_data_handle->data()) ==*canonicalized_blob_data2);
132 }
53 133
54 // Test registering a blob URL referring to the blob data containing data, 134 TEST(BlobStorageContextTest, PublicBlobUrls) {
55 // file and blob. 135 BlobStorageContext context;
56 GURL blob_url2("blob://url_2"); 136 BlobStorageHost host(&context);
57 blob_storage_controller.AddFinishedBlob(blob_url2, blob_data2); 137 MessageLoop fake_io_message_loop;
58 138
59 blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url2); 139 // Build up a basic blob.
60 ASSERT_TRUE(blob_data_found != NULL); 140 const std::string kId("id");
61 EXPECT_TRUE(*blob_data_found == *canonicalized_blob_data2); 141 SetupBasicBlob(&host, kId);
62 142
63 // Test registering a blob URL referring to existent blob URL. 143 // Now register a url for that blob.
64 GURL blob_url3("blob://url_3"); 144 GURL kUrl("blob:id");
65 blob_storage_controller.CloneBlob(blob_url3, blob_url1); 145 host.RegisterPublicBlobURL(kUrl, kId);
146 scoped_ptr<BlobDataHandle> blob_data_handle =
147 context.GetBlobDataFromPublicURL(kUrl);
148 ASSERT_TRUE(blob_data_handle.get());
149 EXPECT_EQ(kId, blob_data_handle->data()->uuid());
150 blob_data_handle.reset();
66 151
67 blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url3); 152 // The url registration should keep the blob alive even after
68 ASSERT_TRUE(blob_data_found != NULL); 153 // explicit references are dropped.
69 EXPECT_TRUE(*blob_data_found == *blob_data1); 154 host.DecrementBlobRefCount(kId);
155 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl);
156 EXPECT_TRUE(blob_data_handle);
157 blob_data_handle.reset();
70 158
71 // Test unregistering a blob URL. 159 // Finally get rid of the url registration and the blob.
72 blob_storage_controller.RemoveBlob(blob_url3); 160 host.RevokePublicBlobURL(kUrl);
73 blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url3); 161 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl);
74 EXPECT_TRUE(!blob_data_found); 162 EXPECT_TRUE(!blob_data_handle.get());
163 }
164
165 TEST(BlobStorageContextTest, HostCleanup) {
166 BlobStorageContext context;
167 scoped_ptr<BlobStorageHost> host(new BlobStorageHost(&context));
168 MessageLoop fake_io_message_loop;
169
170 // Build up a basic blob and register a url
171 const std::string kId("id");
172 GURL kUrl("blob:id");
173 SetupBasicBlob(host.get(), kId);
174 host->RegisterPublicBlobURL(kUrl, kId);
175
176 // All should disappear upon host deletion.
177 host.reset();
178 scoped_ptr<BlobDataHandle> handle = context.GetBlobDataFromPublicURL(kUrl);
179 EXPECT_TRUE(!handle.get());
180 handle = context.GetBlobDataFromUUID(kId);
181 EXPECT_TRUE(!handle.get());
182 }
183
184 TEST(BlobStorageContextTest, EarlyContextDeletion) {
185 scoped_ptr<BlobStorageContext> context(new BlobStorageContext);
186 BlobStorageHost host(context.get());
187 MessageLoop fake_io_message_loop;
188
189 // Deleting the context should not induce crashes.
190 context.reset();
191
192 const std::string kId("id");
193 GURL kUrl("blob:id");
194 SetupBasicBlob(&host, kId);
195 host.RegisterPublicBlobURL(kUrl, kId);
196 host.IncrementBlobRefCount(kId);
197 host.DecrementBlobRefCount(kId);
198 host.RevokePublicBlobURL(kUrl);
75 } 199 }
76 200
77 } // namespace webkit_blob 201 } // namespace webkit_blob
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698