OLD | NEW |
---|---|
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 | |
kinuko
2013/04/23 05:30:39
nit: '// namespace' seems to be more common these
michaeln
2013/04/23 19:27:16
Done.
| |
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 it's there, finish building implies a ref of one. | |
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 host's ref to it. | |
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 scoped_ptr<BlobDataHandle> blob_data_handle; | |
43 | 119 |
44 // Test registering a blob URL referring to the blob data containing only | 120 // Test a blob referring to only data and a file. |
45 // data and file. | 121 blob_data_handle = context.AddFinishedBlob(blob_data1); |
46 GURL blob_url1("blob://url_1"); | 122 ASSERT_TRUE(blob_data_handle.get()); |
47 blob_storage_controller.AddFinishedBlob(blob_url1, blob_data1); | 123 EXPECT_TRUE(*(blob_data_handle->data()) == *blob_data1); |
48 | 124 |
49 BlobData* blob_data_found = | 125 // Test a blob composed in part with another blob. |
50 blob_storage_controller.GetBlobDataFromUrl(blob_url1); | 126 blob_data_handle = context.AddFinishedBlob(blob_data2); |
51 ASSERT_TRUE(blob_data_found != NULL); | 127 ASSERT_TRUE(blob_data_handle.get()); |
52 EXPECT_TRUE(*blob_data_found == *blob_data1); | 128 EXPECT_TRUE(*(blob_data_handle->data()) ==*canonicalized_blob_data2); |
129 } | |
53 | 130 |
54 // Test registering a blob URL referring to the blob data containing data, | 131 TEST(BlobStorageContextTest, PublicBlobUrls) { |
55 // file and blob. | 132 BlobStorageContext context; |
56 GURL blob_url2("blob://url_2"); | 133 BlobStorageHost host(&context); |
57 blob_storage_controller.AddFinishedBlob(blob_url2, blob_data2); | 134 MessageLoop fake_io_message_loop; |
58 | 135 |
59 blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url2); | 136 // Build up a basic blob. |
60 ASSERT_TRUE(blob_data_found != NULL); | 137 const std::string kId("id"); |
61 EXPECT_TRUE(*blob_data_found == *canonicalized_blob_data2); | 138 SetupBasicBlob(&host, kId); |
62 | 139 |
63 // Test registering a blob URL referring to existent blob URL. | 140 // Now register a url for that blob. |
64 GURL blob_url3("blob://url_3"); | 141 GURL kUrl("blob:id"); |
65 blob_storage_controller.CloneBlob(blob_url3, blob_url1); | 142 host.RegisterPublicBlobURL(kUrl, kId); |
143 scoped_ptr<BlobDataHandle> blob_data_handle = | |
144 context.GetBlobDataFromPublicURL(kUrl); | |
145 ASSERT_TRUE(blob_data_handle.get()); | |
146 EXPECT_EQ(kId, blob_data_handle->data()->uuid()); | |
147 blob_data_handle.reset(); | |
66 | 148 |
67 blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url3); | 149 // The url registration should keep the blob alive even after |
68 ASSERT_TRUE(blob_data_found != NULL); | 150 // explicit references are dropped. |
69 EXPECT_TRUE(*blob_data_found == *blob_data1); | 151 host.DecrementBlobRefCount(kId); |
152 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); | |
153 EXPECT_TRUE(blob_data_handle); | |
154 blob_data_handle.reset(); | |
70 | 155 |
71 // Test unregistering a blob URL. | 156 // Finally get rid of the url registration and the blob. |
72 blob_storage_controller.RemoveBlob(blob_url3); | 157 host.RevokePublicBlobURL(kUrl); |
73 blob_data_found = blob_storage_controller.GetBlobDataFromUrl(blob_url3); | 158 blob_data_handle = context.GetBlobDataFromPublicURL(kUrl); |
74 EXPECT_TRUE(!blob_data_found); | 159 EXPECT_TRUE(!blob_data_handle.get()); |
160 } | |
161 | |
162 TEST(BlobStorageContextTest, HostCleanup) { | |
163 BlobStorageContext context; | |
164 scoped_ptr<BlobStorageHost> host(new BlobStorageHost(&context)); | |
165 MessageLoop fake_io_message_loop; | |
166 | |
167 // Build up a basic blob and register a url | |
168 const std::string kId("id"); | |
169 GURL kUrl("blob:id"); | |
170 SetupBasicBlob(host.get(), kId); | |
171 host->RegisterPublicBlobURL(kUrl, kId); | |
172 | |
173 // All should disappear upon host deletion. | |
174 host.reset(); | |
175 scoped_ptr<BlobDataHandle> handle = context.GetBlobDataFromPublicURL(kUrl); | |
176 EXPECT_TRUE(!handle.get()); | |
177 handle = context.GetBlobDataFromUUID(kId); | |
178 EXPECT_TRUE(!handle.get()); | |
179 } | |
180 | |
181 TEST(BlobStorageContextTest, EarlyContextDeletion) { | |
182 scoped_ptr<BlobStorageContext> context(new BlobStorageContext); | |
183 BlobStorageHost host(context.get()); | |
184 MessageLoop fake_io_message_loop; | |
185 | |
186 // Deleting the context should not induce crashes. | |
187 context.reset(); | |
188 | |
189 const std::string kId("id"); | |
190 GURL kUrl("blob:id"); | |
191 SetupBasicBlob(&host, kId); | |
192 host.RegisterPublicBlobURL(kUrl, kId); | |
193 host.IncrementBlobRefCount(kId); | |
194 host.DecrementBlobRefCount(kId); | |
195 host.RevokePublicBlobURL(kUrl); | |
75 } | 196 } |
76 | 197 |
77 } // namespace webkit_blob | 198 } // namespace webkit_blob |
OLD | NEW |