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

Unified Diff: content/browser/blob_storage/blob_async_builder_host_unittest.cc

Issue 1098853003: [BlobAsync] Patch 4: Browser Classes & Logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments, and simplification Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | content/browser/blob_storage/blob_async_transport_strategy_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/blob_storage/blob_async_builder_host_unittest.cc
diff --git a/content/browser/blob_storage/blob_async_builder_host_unittest.cc b/content/browser/blob_storage/blob_async_builder_host_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b061d5a7176b4e229ce059a4866b65f4ddb4dad8
--- /dev/null
+++ b/content/browser/blob_storage/blob_async_builder_host_unittest.cc
@@ -0,0 +1,243 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "storage/browser/blob/blob_async_builder_host.h"
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/memory/shared_memory.h"
+#include "storage/common/blob_storage/blob_storage_constants.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace storage {
+namespace {
+const std::string kBlobUUID = "blobUUIDYAY";
+const std::string kFakeBlobUUID = "fakeBlob";
+const std::string kBlobType = "blobtypeYAY";
+
+constexpr size_t kTestBlobStorageIPCThresholdBytes = 5;
+constexpr size_t kTestBlobStorageMaxSharedMemoryBytes = 20;
+constexpr uint64_t kTestBlobStorageMaxFileSizeBytes = 100;
kinuko 2015/11/19 15:22:25 constexptr is banned (https://chromium-cpp.appspot
dmurph 2015/11/19 22:13:39 Done.
+
+static void PopulateBytes(char* bytes, size_t start_num, size_t length) {
kinuko 2015/11/19 15:22:25 nit: no need for static inside anon namespace
dmurph 2015/11/19 22:13:39 Done.
+ for (size_t i = 0; i < length; i++) {
+ bytes[i] = static_cast<char>(start_num + i);
+ }
+}
+
+static void PopulateBytes(char* bytes, size_t length) {
+ PopulateBytes(bytes, 0, length);
+}
+
+static void AddMemoryItem(size_t length, std::vector<DataElement>* out) {
+ DataElement bytes;
+ bytes.SetToBytesDescription(length);
+ out->push_back(bytes);
+}
+
+static void AddShortcutMemoryItem(size_t length,
+ std::vector<DataElement>* out) {
+ DataElement bytes;
+ bytes.SetToAllocatedBytes(length);
+ PopulateBytes(bytes.mutable_bytes(), length);
+ out->push_back(bytes);
+}
+
+static void AddShortcutMemoryItem(size_t length, BlobDataBuilder* out) {
+ DataElement bytes;
+ bytes.SetToAllocatedBytes(length);
+ PopulateBytes(bytes.mutable_bytes(), length);
+ out->AppendData(bytes.bytes(), length);
+}
+
+static void AddBlobItem(std::vector<DataElement>* out) {
+ DataElement blob;
+ blob.SetToBlob(kFakeBlobUUID);
+ out->push_back(blob);
+}
+
+class BlobAsyncBuilderHostTest : public testing::Test {
+ protected:
+ BlobAsyncBuilderHostTest()
+ : matching_builder_(nullptr),
+ done_called_(false),
+ cancel_called_(false),
+ cancel_code_(IPCBlobCreationCancelCode::UNKNOWN),
+ request_called_(false) {}
+ ~BlobAsyncBuilderHostTest() override {}
+
+ void SetUp() override {
+ matching_builder_ = nullptr;
+ done_called_ = false;
+ cancel_called_ = false;
+ cancel_code_ = IPCBlobCreationCancelCode::UNKNOWN;
+ request_called_ = false;
+ requests_.clear();
+ memory_handles_.clear();
+ file_handles_.clear();
+ host_.SetMemoryConstantsForTesting(kTestBlobStorageIPCThresholdBytes,
+ kTestBlobStorageMaxSharedMemoryBytes,
+ kTestBlobStorageMaxFileSizeBytes);
+ }
+
+ void SetMatchingBuilder(BlobDataBuilder* builder) {
+ matching_builder_ = builder;
+ }
+
+ void CancelCallback(IPCBlobCreationCancelCode code) {
+ cancel_called_ = true;
+ cancel_code_ = code;
+ }
+
+ void DoneCallback(BlobDataBuilder* builder) {
+ ASSERT_TRUE(builder);
+ if (matching_builder_)
+ EXPECT_EQ(*matching_builder_, *builder);
+ done_called_ = true;
+ }
+
+ void RequestMemoryCallback(
+ const std::vector<storage::BlobItemBytesRequest>& requests,
+ const std::vector<base::SharedMemoryHandle>& sms,
+ const std::vector<IPC::PlatformFileForTransit>& pfs) {
+ this->requests_ = requests;
+ memory_handles_ = sms;
+ file_handles_ = pfs;
+ request_called_ = true;
+ }
+
+ void BuildBlobAsync(const std::vector<DataElement>& descriptions,
+ size_t memory_available) {
+ host_.StartBuildingBlob(
+ kBlobUUID, kBlobType, descriptions, memory_available,
+ base::Bind(&BlobAsyncBuilderHostTest::RequestMemoryCallback,
+ base::Unretained(this)),
+ base::Bind(&BlobAsyncBuilderHostTest::DoneCallback,
+ base::Unretained(this)),
+ base::Bind(&BlobAsyncBuilderHostTest::CancelCallback,
+ base::Unretained(this)));
+ }
+
+ BlobDataBuilder* matching_builder_;
+ BlobAsyncBuilderHost host_;
+ bool done_called_;
+ bool cancel_called_;
+ IPCBlobCreationCancelCode cancel_code_;
+
+ bool request_called_;
+ std::vector<storage::BlobItemBytesRequest> requests_;
+ std::vector<base::SharedMemoryHandle> memory_handles_;
+ std::vector<IPC::PlatformFileForTransit> file_handles_;
+};
+
+TEST_F(BlobAsyncBuilderHostTest, TestShortcut) {
+ std::vector<DataElement> descriptions;
+
+ AddShortcutMemoryItem(10, &descriptions);
+ AddBlobItem(&descriptions);
+ AddShortcutMemoryItem(5000, &descriptions);
+
+ BlobDataBuilder expected(kBlobUUID);
+ expected.set_content_type(kBlobType);
+ AddShortcutMemoryItem(10, &expected);
+ expected.AppendBlob(kFakeBlobUUID);
+ AddShortcutMemoryItem(5000, &expected);
+ SetMatchingBuilder(&expected);
+
+ BuildBlobAsync(descriptions, 5010);
+
+ EXPECT_TRUE(done_called_);
+ EXPECT_FALSE(cancel_called_);
+ EXPECT_FALSE(request_called_);
+ EXPECT_EQ(0u, host_.blob_building_count());
+};
+
+TEST_F(BlobAsyncBuilderHostTest, TestSingleSharedMemRequest) {
+ std::vector<DataElement> descriptions;
+ const size_t kSize = kTestBlobStorageIPCThresholdBytes + 1;
+ AddMemoryItem(kSize, &descriptions);
+
+ BuildBlobAsync(descriptions, kTestBlobStorageIPCThresholdBytes + 1);
+
+ EXPECT_FALSE(done_called_);
+ EXPECT_FALSE(cancel_called_);
+ EXPECT_TRUE(request_called_);
+ EXPECT_EQ(1u, host_.blob_building_count());
+ ASSERT_EQ(1u, requests_.size());
+ request_called_ = false;
+
+ EXPECT_EQ(
+ BlobItemBytesRequest::CreateSharedMemoryRequest(0, 0, 0, kSize, 0, 0),
+ requests_.at(0));
+};
+
+TEST_F(BlobAsyncBuilderHostTest, TestMultipleSharedMemRequests) {
+ std::vector<DataElement> descriptions;
+ const size_t kSize = kTestBlobStorageMaxSharedMemoryBytes + 1;
+ const size_t kFirstBlockByte = 7;
+ const size_t kSecondBlockByte = 19;
kinuko 2015/11/19 15:22:25 Are k{First,Second}BlockByte used as test byte dat
dmurph 2015/11/19 22:13:39 Whoops, they should be char.
+ AddMemoryItem(kSize, &descriptions);
+
+ BlobDataBuilder expected(kBlobUUID);
+ expected.set_content_type(kBlobType);
+ char* data = new char[kSize];
kinuko 2015/11/19 15:22:25 nit: given that kSize is small we should be able t
dmurph 2015/11/19 22:13:39 Done.
+ char lastByte = kSecondBlockByte;
+ memset(data, kFirstBlockByte, kTestBlobStorageMaxSharedMemoryBytes);
+ expected.AppendData(data, kTestBlobStorageMaxSharedMemoryBytes);
+ expected.AppendData(&lastByte, 1);
+ delete[] data;
+ SetMatchingBuilder(&expected);
+
+ BuildBlobAsync(descriptions, kTestBlobStorageMaxSharedMemoryBytes + 1);
+
+ EXPECT_FALSE(done_called_);
+ EXPECT_FALSE(cancel_called_);
+ EXPECT_TRUE(request_called_);
+ EXPECT_EQ(1u, host_.blob_building_count());
+ ASSERT_EQ(1u, requests_.size());
+ request_called_ = false;
+
+ // We need to grab a duplicate handle so we can have two blocks open at the
+ // same time.
+ base::SharedMemoryHandle handle =
+ base::SharedMemory::DuplicateHandle(memory_handles_.at(0));
+ EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle));
+ base::SharedMemory shared_memory(handle, false);
+ EXPECT_TRUE(shared_memory.Map(kTestBlobStorageMaxSharedMemoryBytes));
+
+ EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest(
+ 0, 0, 0, kTestBlobStorageMaxSharedMemoryBytes, 0, 0),
+ requests_.at(0));
+
+ memset(shared_memory.memory(), kFirstBlockByte,
+ kTestBlobStorageMaxSharedMemoryBytes);
+
+ BlobItemBytesResponse response(0);
+ std::vector<BlobItemBytesResponse> responses = {response};
+ host_.OnMemoryResponses(kBlobUUID, responses);
+
+ EXPECT_FALSE(done_called_);
+ EXPECT_FALSE(cancel_called_);
+ EXPECT_TRUE(request_called_);
+ EXPECT_EQ(1u, host_.blob_building_count());
+ ASSERT_EQ(1u, requests_.size());
+ request_called_ = false;
+
+ EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest(
+ 1, 0, kTestBlobStorageMaxSharedMemoryBytes, 1, 0, 0),
+ requests_.at(0));
+
+ memset(shared_memory.memory(), kSecondBlockByte, 1);
+
+ response.request_number = 1;
+ responses[0] = response;
+ host_.OnMemoryResponses(kBlobUUID, responses);
+ EXPECT_TRUE(done_called_);
+ EXPECT_FALSE(cancel_called_);
+ EXPECT_FALSE(request_called_);
+ EXPECT_EQ(0u, host_.blob_building_count());
+};
+
+} // namespace
+} // namespace storage
« no previous file with comments | « no previous file | content/browser/blob_storage/blob_async_transport_strategy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698