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

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, 7 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
« no previous file with comments | « webkit/blob/blob_storage_context.cc ('k') | webkit/blob/blob_storage_host.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_handle.h"
11 #include "webkit/blob/blob_storage_controller.h" 12 #include "webkit/blob/blob_storage_context.h"
13 #include "webkit/blob/blob_storage_host.h"
12 14
13 namespace webkit_blob { 15 namespace webkit_blob {
14 16
15 TEST(BlobStorageControllerTest, RegisterBlobUrl) { 17 namespace {
18 void SetupBasicBlob(BlobStorageHost* host, const std::string& id) {
19 EXPECT_TRUE(host->StartBuildingBlob(id));
20 BlobData::Item item;
21 item.SetToBytes("1", 1);
22 EXPECT_TRUE(host->AppendBlobDataItem(id, item));
23 EXPECT_TRUE(host->FinishBuildingBlob(id, "text/plain"));
24 EXPECT_FALSE(host->StartBuildingBlob(id));
25 }
26 } // namespace
27
28 TEST(BlobStorageContextTest, IncrementDecrementRef) {
29 BlobStorageContext context;
30 BlobStorageHost host(&context);
31 MessageLoop fake_io_message_loop;
32
33 // Build up a basic blob.
34 const std::string kId("id");
35 SetupBasicBlob(&host, kId);
36
37 // Make sure it's there, finish building implies a ref of one.
38 scoped_ptr<BlobDataHandle> blob_data_handle;
39 blob_data_handle = context.GetBlobDataFromUUID(kId);
40 EXPECT_TRUE(blob_data_handle);
41 blob_data_handle.reset();
42
43 // Make sure its still there after inc/dec.
44 EXPECT_TRUE(host.IncrementBlobRefCount(kId));
45 EXPECT_TRUE(host.DecrementBlobRefCount(kId));
46 blob_data_handle = context.GetBlobDataFromUUID(kId);
47 EXPECT_TRUE(blob_data_handle);
48 blob_data_handle.reset();
49
50 // Make sure it goes away in the end.
51 EXPECT_TRUE(host.DecrementBlobRefCount(kId));
52 blob_data_handle = context.GetBlobDataFromUUID(kId);
53 EXPECT_FALSE(blob_data_handle);
54 EXPECT_FALSE(host.DecrementBlobRefCount(kId));
55 EXPECT_FALSE(host.IncrementBlobRefCount(kId));
56 }
57
58 TEST(BlobStorageContextTest, BlobDataHandle) {
59 BlobStorageContext context;
60 BlobStorageHost host(&context);
61 MessageLoop fake_io_message_loop;
62
63 // Build up a basic blob.
64 const std::string kId("id");
65 SetupBasicBlob(&host, kId);
66
67 // Get a handle to it.
68 scoped_ptr<BlobDataHandle> blob_data_handle =
69 context.GetBlobDataFromUUID(kId);
70 EXPECT_TRUE(blob_data_handle);
71
72 // Drop the host's ref to it.
73 EXPECT_TRUE(host.DecrementBlobRefCount(kId));
74
75 // Should still be there due to the handle.
76 scoped_ptr<BlobDataHandle> another_handle =
77 context.GetBlobDataFromUUID(kId);
78 EXPECT_TRUE(another_handle);
79
80 // Should disappear after dropping both handles.
81 blob_data_handle.reset();
82 another_handle.reset();
83 blob_data_handle = context.GetBlobDataFromUUID(kId);
84 EXPECT_FALSE(blob_data_handle);
85 }
86
87 TEST(BlobStorageContextTest, CompoundBlobs) {
88 const std::string kId1("id1");
89 const std::string kId2("id2");
90 const std::string kId2Prime("id2.prime");
91
92 MessageLoop fake_io_message_loop;
93
16 // Setup a set of blob data for testing. 94 // Setup a set of blob data for testing.
17 base::Time time1, time2; 95 base::Time time1, time2;
18 base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT", &time1); 96 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); 97 base::Time::FromString("Mon, 14 Nov 1994, 11:30:49 GMT", &time2);
20 98
21 scoped_refptr<BlobData> blob_data1(new BlobData()); 99 scoped_refptr<BlobData> blob_data1(new BlobData(kId1));
22 blob_data1->AppendData("Data1"); 100 blob_data1->AppendData("Data1");
23 blob_data1->AppendData("Data2"); 101 blob_data1->AppendData("Data2");
24 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")), 102 blob_data1->AppendFile(base::FilePath(FILE_PATH_LITERAL("File1.txt")),
25 10, 1024, time1); 103 10, 1024, time1);
26 104
27 scoped_refptr<BlobData> blob_data2(new BlobData()); 105 scoped_refptr<BlobData> blob_data2(new BlobData(kId2));
28 blob_data2->AppendData("Data3"); 106 blob_data2->AppendData("Data3");
29 blob_data2->AppendBlob(GURL("blob://url_1"), 8, 100); 107 blob_data2->AppendBlob(kId1, 8, 100);
30 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")), 108 blob_data2->AppendFile(base::FilePath(FILE_PATH_LITERAL("File2.txt")),
31 0, 20, time2); 109 0, 20, time2);
32 110
33 scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData()); 111 scoped_refptr<BlobData> canonicalized_blob_data2(new BlobData(kId2Prime));
34 canonicalized_blob_data2->AppendData("Data3"); 112 canonicalized_blob_data2->AppendData("Data3");
35 canonicalized_blob_data2->AppendData("a2___", 2); 113 canonicalized_blob_data2->AppendData("a2___", 2);
36 canonicalized_blob_data2->AppendFile( 114 canonicalized_blob_data2->AppendFile(
37 base::FilePath(FILE_PATH_LITERAL("File1.txt")), 115 base::FilePath(FILE_PATH_LITERAL("File1.txt")),
38 10, 98, time1); 116 10, 98, time1);
39 canonicalized_blob_data2->AppendFile( 117 canonicalized_blob_data2->AppendFile(
40 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2); 118 base::FilePath(FILE_PATH_LITERAL("File2.txt")), 0, 20, time2);
41 119
42 BlobStorageController blob_storage_controller; 120 BlobStorageContext context;
121 scoped_ptr<BlobDataHandle> blob_data_handle;
43 122
44 // Test registering a blob URL referring to the blob data containing only 123 // Test a blob referring to only data and a file.
45 // data and file. 124 blob_data_handle = context.AddFinishedBlob(blob_data1);
46 GURL blob_url1("blob://url_1"); 125 ASSERT_TRUE(blob_data_handle.get());
47 blob_storage_controller.AddFinishedBlob(blob_url1, blob_data1); 126 EXPECT_TRUE(*(blob_data_handle->data()) == *blob_data1);
48 127
49 BlobData* blob_data_found = 128 // Test a blob composed in part with another blob.
50 blob_storage_controller.GetBlobDataFromUrl(blob_url1); 129 blob_data_handle = context.AddFinishedBlob(blob_data2);
51 ASSERT_TRUE(blob_data_found != NULL); 130 ASSERT_TRUE(blob_data_handle.get());
52 EXPECT_TRUE(*blob_data_found == *blob_data1); 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 EXPECT_TRUE(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 EXPECT_TRUE(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 EXPECT_TRUE(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 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl));
164 }
165
166 TEST(BlobStorageContextTest, HostCleanup) {
167 BlobStorageContext context;
168 scoped_ptr<BlobStorageHost> host(new BlobStorageHost(&context));
169 MessageLoop fake_io_message_loop;
170
171 // Build up a basic blob and register a url
172 const std::string kId("id");
173 GURL kUrl("blob:id");
174 SetupBasicBlob(host.get(), kId);
175 EXPECT_TRUE(host->RegisterPublicBlobURL(kUrl, kId));
176
177 // All should disappear upon host deletion.
178 host.reset();
179 scoped_ptr<BlobDataHandle> handle = context.GetBlobDataFromPublicURL(kUrl);
180 EXPECT_TRUE(!handle.get());
181 handle = context.GetBlobDataFromUUID(kId);
182 EXPECT_TRUE(!handle.get());
183 }
184
185 TEST(BlobStorageContextTest, EarlyContextDeletion) {
186 scoped_ptr<BlobStorageContext> context(new BlobStorageContext);
187 BlobStorageHost host(context.get());
188 MessageLoop fake_io_message_loop;
189
190 // Deleting the context should not induce crashes.
191 context.reset();
192
193 const std::string kId("id");
194 GURL kUrl("blob:id");
195 EXPECT_FALSE(host.StartBuildingBlob(kId));
196 BlobData::Item item;
197 item.SetToBytes("1", 1);
198 EXPECT_FALSE(host.AppendBlobDataItem(kId, item));
199 EXPECT_FALSE(host.FinishBuildingBlob(kId, "text/plain"));
200 EXPECT_FALSE(host.RegisterPublicBlobURL(kUrl, kId));
201 EXPECT_FALSE(host.IncrementBlobRefCount(kId));
202 EXPECT_FALSE(host.DecrementBlobRefCount(kId));
203 EXPECT_FALSE(host.RevokePublicBlobURL(kUrl));
75 } 204 }
76 205
77 } // namespace webkit_blob 206 } // namespace webkit_blob
OLDNEW
« no previous file with comments | « webkit/blob/blob_storage_context.cc ('k') | webkit/blob/blob_storage_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698