Commit 5ce0fc03 authored by Hoch Hochkeppel's avatar Hoch Hochkeppel Committed by Commit Bot

Adding fake RandomAccessStream for testing

Adding a class to represent the Windows RandomAccessStream class
within tests, as well as basic unit tests to validate the test class
behaviors. This class behaves similarly to the Windows equivalent,
except it includes gtest validations for expected usage and schedules
all async operations through a ThreadTaskRunner.

Bug: 1035527
Change-Id: Id0ce1af8995260fdcb0c2a06c6408819afc4dfbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2487818Reviewed-by: default avatarEric Willigers <ericwilligers@chromium.org>
Commit-Queue: Hoch Hochkeppel <mhochk@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#819254}
parent bf52877c
// Copyright 2020 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.
#ifndef CHROME_BROWSER_WEBSHARE_WIN_FAKE_IASYNC_OPERATION_WITH_PROGRESS_H_
#define CHROME_BROWSER_WEBSHARE_WIN_FAKE_IASYNC_OPERATION_WITH_PROGRESS_H_
#include <wrl/client.h>
#include "base/notreached.h"
#include "base/win/winrt_foundation_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace webshare {
namespace internal {
// Templates used to allow easy reference to the correct types.
// See base/win/winrt_foundation_helpers.h for explanation.
template <typename TResult, typename TProgress>
using AsyncOperationWithProgressComplex = typename ABI::Windows::Foundation::
IAsyncOperationWithProgress<TResult, TProgress>::TResult_complex;
template <typename TResult, typename TProgress>
using AsyncOperationWithProgressAbi = base::win::internal::AbiType<
AsyncOperationWithProgressComplex<TResult, TProgress>>;
template <typename TResult, typename TProgress>
using AsyncOperationWithProgressOptionalStorage =
base::win::internal::OptionalStorageType<
AsyncOperationWithProgressComplex<TResult, TProgress>>;
template <typename TResult, typename TProgress>
using AsyncOperationWithProgressStorage = base::win::internal::StorageType<
AsyncOperationWithProgressComplex<TResult, TProgress>>;
} // namespace internal
// Provides an implementation of IAsyncOperationWithProgress for use in GTests.
template <typename TResult, typename TProgress>
class FakeIAsyncOperationWithProgress final
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix>,
ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult,
TProgress>,
ABI::Windows::Foundation::IAsyncInfo> {
public:
FakeIAsyncOperationWithProgress() = default;
FakeIAsyncOperationWithProgress(const FakeIAsyncOperationWithProgress&) =
delete;
FakeIAsyncOperationWithProgress& operator=(
const FakeIAsyncOperationWithProgress&) = delete;
// ABI::Windows::Foundation::IAsyncOperationWithProgress:
IFACEMETHODIMP put_Progress(
ABI::Windows::Foundation::IAsyncOperationProgressHandler<TResult,
TProgress>*
handler) final {
NOTREACHED();
return E_NOTIMPL;
}
IFACEMETHODIMP get_Progress(
ABI::Windows::Foundation::IAsyncOperationProgressHandler<TResult,
TProgress>**
handler) final {
NOTREACHED();
return E_NOTIMPL;
}
IFACEMETHODIMP put_Completed(
ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<
TResult,
TProgress>* handler) final {
EXPECT_EQ(nullptr, handler_)
<< "put_Completed called on IAsyncOperation with a CompletedHandler "
"already defined.";
handler_ = handler;
return S_OK;
}
IFACEMETHODIMP get_Completed(
ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<
TResult,
TProgress>** handler) final {
NOTREACHED();
return E_NOTIMPL;
}
IFACEMETHODIMP GetResults(
internal::AsyncOperationWithProgressAbi<TResult, TProgress>* results)
final {
if (!is_complete_) {
ADD_FAILURE()
<< "GetResults called on incomplete IAsyncOperationWithProgress.";
return E_PENDING;
}
if (status_ != AsyncStatus::Completed)
return E_UNEXPECTED;
return base::win::internal::CopyTo(results_, results);
}
// ABI::Windows::Foundation::IAsyncInfo:
IFACEMETHODIMP get_Id(uint32_t* id) final {
NOTREACHED();
return E_NOTIMPL;
}
IFACEMETHODIMP get_Status(AsyncStatus* status) final {
*status = status_;
return S_OK;
}
IFACEMETHODIMP get_ErrorCode(HRESULT* error_code) final {
*error_code = error_code_;
return S_OK;
}
IFACEMETHODIMP Cancel() final {
NOTREACHED();
return E_NOTIMPL;
}
IFACEMETHODIMP Close() final {
NOTREACHED();
return E_NOTIMPL;
}
// Completes the operation with |error_code|.
//
// The get_ErrorCode API will be set to return |error_code|, the remainder of
// the APIs will be set to represent an error state, and the CompletedHandler
// (if defined) will be run.
void CompleteWithError(HRESULT error_code) {
error_code_ = error_code;
status_ = AsyncStatus::Error;
InvokeCompletedHandler();
}
// Completes the operation with |results|.
//
// The GetResults API will be set to return |results|, the remainder of the
// APIs will be set to represent a successfully completed state, and the
// CompletedHandler (if defined) will be run.
void CompleteWithResults(
internal::AsyncOperationWithProgressStorage<TResult, TProgress> results) {
error_code_ = S_OK;
results_ = std::move(results);
status_ = AsyncStatus::Completed;
InvokeCompletedHandler();
}
private:
void InvokeCompletedHandler() {
ASSERT_FALSE(is_complete_)
<< "Attempted to invoke completion on an already "
"completed IAsyncOperationWithProgress.";
is_complete_ = true;
if (handler_)
handler_->Invoke(this, status_);
}
HRESULT error_code_ = S_OK;
Microsoft::WRL::ComPtr<
ABI::Windows::Foundation::
IAsyncOperationWithProgressCompletedHandler<TResult, TProgress>>
handler_;
bool is_complete_ = false;
internal::AsyncOperationWithProgressOptionalStorage<TResult, TProgress>
results_;
AsyncStatus status_ = AsyncStatus::Started;
};
} // namespace webshare
#endif // CHROME_BROWSER_WEBSHARE_WIN_FAKE_IASYNC_OPERATION_WITH_PROGRESS_H_
This diff is collapsed.
// Copyright 2020 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.
#ifndef CHROME_BROWSER_WEBSHARE_WIN_FAKE_RANDOM_ACCESS_STREAM_H_
#define CHROME_BROWSER_WEBSHARE_WIN_FAKE_RANDOM_ACCESS_STREAM_H_
#include <windows.foundation.h>
#include <windows.storage.streams.h>
#include <wrl/implements.h>
#include "base/callback.h"
#include "base/memory/scoped_refptr.h"
namespace webshare {
class StreamData;
// Provides an implementation of IRandomAccessStream for use in GTests.
class __declspec(uuid("66DAD26A-BEDE-4A54-8316-088838CC65A0"))
FakeRandomAccessStream
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix>,
ABI::Windows::Storage::Streams::IRandomAccessStream,
ABI::Windows::Foundation::IClosable,
ABI::Windows::Storage::Streams::IInputStream,
ABI::Windows::Storage::Streams::IOutputStream> {
public:
FakeRandomAccessStream();
FakeRandomAccessStream(const FakeRandomAccessStream& other) = delete;
FakeRandomAccessStream& operator=(const FakeRandomAccessStream&) = delete;
~FakeRandomAccessStream() final;
// ABI::Windows::Storage::Streams::IRandomAccessStream:
IFACEMETHODIMP get_Size(UINT64* value) final;
IFACEMETHODIMP put_Size(UINT64 value) final;
IFACEMETHODIMP
GetInputStreamAt(UINT64 position,
ABI::Windows::Storage::Streams::IInputStream** stream) final;
IFACEMETHODIMP
GetOutputStreamAt(
UINT64 position,
ABI::Windows::Storage::Streams::IOutputStream** stream) final;
IFACEMETHODIMP get_Position(UINT64* value) final;
IFACEMETHODIMP Seek(UINT64 position) final;
IFACEMETHODIMP
CloneStream(IRandomAccessStream** stream) final;
IFACEMETHODIMP get_CanRead(boolean* value) final;
IFACEMETHODIMP get_CanWrite(boolean* value) final;
// ABI::Windows::Foundation::IClosable:
IFACEMETHODIMP Close() final;
// ABI::Windows::Storage::Streams::IInputStream:
IFACEMETHODIMP ReadAsync(
ABI::Windows::Storage::Streams::IBuffer* buffer,
UINT32 count,
ABI::Windows::Storage::Streams::InputStreamOptions options,
ABI::Windows::Foundation::IAsyncOperationWithProgress<
ABI::Windows::Storage::Streams::IBuffer*,
UINT32>** operation) final;
// ABI::Windows::Storage::Streams::IOutputStream:
IFACEMETHODIMP
WriteAsync(
ABI::Windows::Storage::Streams::IBuffer* buffer,
ABI::Windows::Foundation::IAsyncOperationWithProgress<UINT32, UINT32>**
operation) final;
IFACEMETHODIMP
FlushAsync(ABI::Windows::Foundation::IAsyncOperation<bool>** operation) final;
// Sets a callback to be invoked when |Close| is called.
void OnClose(base::OnceClosure on_close);
private:
scoped_refptr<StreamData> shared_data_;
scoped_refptr<base::RefCountedData<UINT64>> position_;
bool is_closed_ = false;
base::OnceClosure on_close_;
};
} // namespace webshare
#endif // CHROME_BROWSER_WEBSHARE_WIN_FAKE_RANDOM_ACCESS_STREAM_H_
...@@ -327,6 +327,9 @@ static_library("test_support") { ...@@ -327,6 +327,9 @@ static_library("test_support") {
"../browser/webshare/win/fake_data_transfer_manager.h", "../browser/webshare/win/fake_data_transfer_manager.h",
"../browser/webshare/win/fake_data_transfer_manager_interop.cc", "../browser/webshare/win/fake_data_transfer_manager_interop.cc",
"../browser/webshare/win/fake_data_transfer_manager_interop.h", "../browser/webshare/win/fake_data_transfer_manager_interop.h",
"../browser/webshare/win/fake_iasync_operation_with_progress.h",
"../browser/webshare/win/fake_random_access_stream.cc",
"../browser/webshare/win/fake_random_access_stream.h",
"../browser/webshare/win/scoped_fake_data_transfer_manager_interop.cc", "../browser/webshare/win/scoped_fake_data_transfer_manager_interop.cc",
"../browser/webshare/win/scoped_fake_data_transfer_manager_interop.h", "../browser/webshare/win/scoped_fake_data_transfer_manager_interop.h",
"//chrome/app/chrome_crash_reporter_client_win.cc", "//chrome/app/chrome_crash_reporter_client_win.cc",
...@@ -5815,6 +5818,7 @@ test("unit_tests") { ...@@ -5815,6 +5818,7 @@ test("unit_tests") {
"../browser/webshare/win/fake_buffer_unittest.cc", "../browser/webshare/win/fake_buffer_unittest.cc",
"../browser/webshare/win/fake_data_transfer_manager_interop_unittest.cc", "../browser/webshare/win/fake_data_transfer_manager_interop_unittest.cc",
"../browser/webshare/win/fake_data_transfer_manager_unittest.cc", "../browser/webshare/win/fake_data_transfer_manager_unittest.cc",
"../browser/webshare/win/fake_random_access_stream_unittest.cc",
"../browser/webshare/win/show_share_ui_for_window_operation_unittest.cc", "../browser/webshare/win/show_share_ui_for_window_operation_unittest.cc",
] ]
deps += [ deps += [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment