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

Unified Diff: Source/modules/fetch/FetchBlobDataConsumerHandleTest.cpp

Issue 1171913003: **** [WIP] Blink-side: Implement FetchBlobDataConsumerHandle **** (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Clean up. Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/fetch/FetchBlobDataConsumerHandle.cpp ('k') | Source/modules/fetch/FetchManager.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/fetch/FetchBlobDataConsumerHandleTest.cpp
diff --git a/Source/modules/fetch/FetchBlobDataConsumerHandleTest.cpp b/Source/modules/fetch/FetchBlobDataConsumerHandleTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..3a88b12b48593ccac7ab133cad03cc5300614498
--- /dev/null
+++ b/Source/modules/fetch/FetchBlobDataConsumerHandleTest.cpp
@@ -0,0 +1,170 @@
+// 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 "config.h"
+#include "modules/fetch/FetchBlobDataConsumerHandle.h"
+
+#include "core/dom/ExecutionContext.h"
+#include "core/fetch/ResourceLoaderOptions.h"
+#include "core/loader/ThreadableLoader.h"
+#include "core/testing/DummyPageHolder.h"
+#include "platform/blob/BlobData.h"
+#include "platform/network/ResourceRequest.h"
+#include "platform/network/ResourceResponse.h"
+#include "wtf/PassRefPtr.h"
+#include "wtf/RefPtr.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <string.h>
+
+/*
+namespace blink {
+namespace {
+
+using Result = WebDataConsumerHandle::Result;
+const Result kShouldWait = WebDataConsumerHandle::ShouldWait;
+const Result kOk = WebDataConsumerHandle::Ok;
+using Flags = WebDataConsumerHandle::Flags;
+const Flags kNone = WebDataConsumerHandle::FlagNone;
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::IgnoreResult;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::SaveArg;
+using ::testing::StrictMock;
+using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>;
+
+class MockDataConsumerHandle : public WebDataConsumerHandle {
+public:
+ MOCK_METHOD4(read, Result(void*, size_t, Flags, size_t*));
+ MOCK_METHOD3(beginRead, Result(const void**, Flags, size_t*));
+ MOCK_METHOD1(endRead, Result(size_t));
+ MOCK_METHOD1(registerClient, void(WebDataConsumerHandle::Client*));
+ MOCK_METHOD0(unregisterClient, void());
+};
+
+class MockLoader : public ThreadableLoader {
+public:
+ MOCK_METHOD4(create, void(ThreadableLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&));
+
+ MOCK_METHOD1(overrideTimeout, void(unsigned long));
+ MOCK_METHOD0(cancel, void());
+};
+
+class FetchBlobDataConsumerHandleWithCustomLoader : public FetchBlobDataConsumerHandle {
+public:
+ FetchBlobDataConsumerHandleWithCustomLoader(PassRefPtr<BlobDataHandle> handle, PassRefPtr<StrictMock<MockLoader>> loader)
+ : FetchBlobDataConsumerHandle(handle), m_loader(loader) { }
+
+ PassRefPtr<ThreadableLoader> createLoader(ExecutionContext&, ThreadableLoaderClient* client, const ResourceRequest& request, const ThreadableLoaderOptions& options, const ResourceLoaderOptions& resourceLoaderOptions) override
+ {
+ m_loader->create(client, request, options, resourceLoaderOptions);
+ return m_loader;
+ }
+
+private:
+ RefPtr<StrictMock<MockLoader>> m_loader;
+};
+
+class Write {
+public:
+ explicit Write(const std::string& data) : m_data(data) { }
+ Result operator() (void* p, size_t size, Flags, size_t* read)
+ {
+ *read = std::min(size, m_data.size());
+ memcpy(p, m_data.data(), *read);
+ return kOk;
+ }
+
+private:
+ const std::string m_data;
+};
+
+class FetchBlobDataConsumerHandleTest : public ::testing::Test {
+public:
+ FetchBlobDataConsumerHandleTest()
+ : m_page(DummyPageHolder::create(IntSize(1, 1)))
+ {
+ OwnPtr<BlobData> data = BlobData::create();
+ data->appendText("Once upon a time there was a", false);
+ auto size = data->length();
+ m_blobDataHandle = BlobDataHandle::create(data.release(), size);
+ m_loader = adoptRef(new StrictMock<MockLoader>);
+ m_handle = adoptPtr(new StrictMock<FetchBlobDataConsumerHandleWithCustomLoader>(m_blobDataHandle, m_loader));
+ }
+
+protected:
+ OwnPtr<DummyPageHolder> m_page;
+ RefPtr<BlobDataHandle> m_blobDataHandle;
+ OwnPtr<StrictMock<FetchBlobDataConsumerHandleWithCustomLoader>> m_handle;
+ RefPtr<StrictMock<MockLoader>> m_loader;
+};
+
+TEST_F(FetchBlobDataConsumerHandleTest, Start)
+{
+ ResourceRequest request;
+ ThreadableLoaderOptions options;
+ ResourceLoaderOptions resourceLoaderOptions;
+
+ Checkpoint checkpoint;
+ InSequence s;
+
+ EXPECT_CALL(checkpoint, Call(0));
+ EXPECT_CALL(*m_loader, create(m_handle.get(), _, _, _)).WillOnce(DoAll(
+ SaveArg<1>(&request), SaveArg<2>(&options), SaveArg<3>(&resourceLoaderOptions)));
+ EXPECT_CALL(checkpoint, Call(1));
+
+ checkpoint.Call(0);
+ m_handle->start(m_page->document());
+ checkpoint.Call(1);
+
+ EXPECT_TRUE(request.url().string().startsWith("blob:"));
+ EXPECT_TRUE(request.useStreamOnResponse());
+
+ EXPECT_EQ(ConsiderPreflight, options.preflightPolicy);
+ EXPECT_EQ(DenyCrossOriginRequests, options.crossOriginRequestPolicy);
+ EXPECT_EQ(DoNotEnforceContentSecurityPolicy, options.contentSecurityPolicyEnforcement);
+
+ EXPECT_EQ(DoNotBufferData, resourceLoaderOptions.dataBufferingPolicy);
+ EXPECT_EQ(DoNotAllowStoredCredentials, resourceLoaderOptions.allowCredentials);
+ EXPECT_EQ(ClientDidNotRequestCredentials, resourceLoaderOptions.credentialsRequested);
+ EXPECT_EQ(CheckContentSecurityPolicy, resourceLoaderOptions.contentSecurityPolicyOption);
+ EXPECT_EQ(DocumentContext, resourceLoaderOptions.requestInitiatorContext);
+ EXPECT_EQ(RequestAsynchronously, resourceLoaderOptions.synchronousPolicy);
+ EXPECT_EQ(NotCORSEnabled, resourceLoaderOptions.corsEnabled);
+}
+
+TEST_F(FetchBlobDataConsumerHandleTest, read)
+{
+ char buffer[20];
+ OwnPtr<StrictMock<MockDataConsumerHandle>> handle = adoptPtr(new StrictMock<MockDataConsumerHandle>);
+
+ InSequence s;
+ EXPECT_CALL(*m_loader, create(m_handle.get(), _, _, _));
+ EXPECT_CALL(*handle, read(buffer, 20, kNone, _)).WillOnce(Return(kShouldWait));
+ EXPECT_CALL(*handle, read(buffer, 20, kNone, _)).WillOnce(DoAll(IgnoreResult(Invoke(Write("hello"))), Return(kOk)));
+
+ size_t read = 99;
+ Result result;
+ ResourceResponse response;
+
+ m_handle->start(m_page->document());
+ result = m_handle->read(buffer, sizeof(buffer), kNone, &read);
+ EXPECT_EQ(kShouldWait, result);
+ m_handle->didReceiveResponse(0, response, handle.release());
+
+ result = m_handle->read(buffer, sizeof(buffer), kNone, &read);
+ EXPECT_EQ(kShouldWait, result);
+
+ result = m_handle->read(buffer, sizeof(buffer), kNone, &read);
+ EXPECT_EQ(kOk, result);
+}
+
+} // namespace
+} // namespace blink
+*/
« no previous file with comments | « Source/modules/fetch/FetchBlobDataConsumerHandle.cpp ('k') | Source/modules/fetch/FetchManager.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698