Skip to content

Commit

Permalink
Add tests for FakeIAsyncOperations
Browse files Browse the repository at this point in the history
Adding tests for the FakeIAsyncOperation test class. This includes
pulling the templated test value logic out of the PostAsyncResultsTests
to a shared location and leveraging it in these new tests.

Bug: 1140544
Change-Id: I01b6918cf3694cf309301b2e56f39ef1d4670bf4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485621
Reviewed-by: Robert Liao <robliao@chromium.org>
Reviewed-by: Wez <wez@chromium.org>
Commit-Queue: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820202}
  • Loading branch information
mhochk authored and Commit Bot committed Oct 23, 2020
1 parent 2893176 commit bd67d08
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 104 deletions.
1 change: 1 addition & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,7 @@ test("base_unittests") {
"debug/gdi_debug_util_win_unittest.cc",
"file_version_info_win_unittest.cc",
"process/launch_unittest_win.cc",
"test/fake_iasync_operation_win_unittest.cc",
"test/test_reg_util_win_unittest.cc",
"threading/platform_thread_win_unittest.cc",
"time/time_win_unittest.cc",
Expand Down
1 change: 1 addition & 0 deletions base/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ static_library("test_support") {

if (is_win) {
sources += [
"async_results_test_values_win.h",
"fake_iasync_operation_win.h",
"scoped_os_info_override_win.cc",
"scoped_os_info_override_win.h",
Expand Down
180 changes: 180 additions & 0 deletions base/test/async_results_test_values_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// 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 BASE_TEST_ASYNC_RESULTS_TEST_VALUES_WIN_H_
#define BASE_TEST_ASYNC_RESULTS_TEST_VALUES_WIN_H_

#include <windows.foundation.collections.h>
#include <wrl/client.h>
#include <wrl/implements.h>

#include "testing/gtest/include/gtest/gtest.h"

// Declare the related template specializations for all the value types
// provided.
namespace ABI {
namespace Windows {
namespace Foundation {

template <>
struct __declspec(uuid("3895C200-8F26-4F5A-B29D-2B5D72E68F99"))
ABI::Windows::Foundation::IAsyncOperation<IUnknown*>
: ABI::Windows::Foundation::IAsyncOperation_impl<IUnknown*> {};

template <>
struct __declspec(uuid("CD99A253-6473-4810-AF0D-763DAB79AC42"))
ABI::Windows::Foundation::IAsyncOperationCompletedHandler<IUnknown*>
: ABI::Windows::Foundation::IAsyncOperationCompletedHandler_impl<
IUnknown*> {};

template <>
struct __declspec(uuid("CB52D855-8121-4AC8-A164-084A27FB377E"))
ABI::Windows::Foundation::IAsyncOperation<int*>
: ABI::Windows::Foundation::IAsyncOperation_impl<int*> {};

template <>
struct __declspec(uuid("EA868415-A724-40BC-950A-C7DB6B1723C6"))
ABI::Windows::Foundation::IAsyncOperationCompletedHandler<int*>
: ABI::Windows::Foundation::IAsyncOperationCompletedHandler_impl<int*> {};

// These specialization templates were included in windows.foundation.h, but
// removed in 10.0.19041.0 SDK, so are included here conditionally
#ifdef NTDDI_WIN10_VB // Windows 10.0.19041
template <>
struct __declspec(uuid("968b9665-06ed-5774-8f53-8edeabd5f7b5"))
ABI::Windows::Foundation::IAsyncOperation<int>
: ABI::Windows::Foundation::IAsyncOperation_impl<int> {};

template <>
struct __declspec(uuid("d60cae9d-88cb-59f1-8576-3fba44796be8"))
ABI::Windows::Foundation::IAsyncOperationCompletedHandler<int>
: ABI::Windows::Foundation::IAsyncOperationCompletedHandler_impl<int> {};
#endif

} // namespace Foundation
} // namespace Windows
} // namespace ABI

namespace base {
namespace test {
// Provides access to values of type |T| and variations of those values relevant
// to IAsyncOperations. Intended for use in TypedTestSuites concerning
// IAsyncOperations or related functionality. Example:
// template <typename T>
// class SuiteName : public ::testing::Test {};
//
// TYPED_TEST_SUITE_P(SuiteName);
//
// TYPED_TEST_P(SuiteName, TestName) {
// AsyncResultsTestValues<TypeParam> test_values;
// ... test_values.GetTestValue_T() ...
// }
//
// REGISTER_TYPED_TEST_SUITE_P(SuiteName, TestName);
// INSTANTIATE_TYPED_TEST_SUITE_P(Prefix,
// SuiteName,
// base::test::AsyncResultsTestValuesTypes);
template <typename T>
class AsyncResultsTestValues {
// This class body serves only to provide documentation for the functions.
// Actual use of this class is limited to the types for which it has been
// specialized.
private:
class AsyncResultsT;

public:
// Returns a value equal to a variable of type T constructed with an
// empty initializer.
//
// This value will be equal between all instances of the same type.
// AsyncResultsTestValues<T> instance1;
// AsyncResultsTestValues<T> instance2;
// instance1.GetDefaultValue_T() == instance2.GetDefaultValue_T();
T GetDefaultValue_T();

// Returns the same value as GetDefaultValue_T(), but in the format expected
// for the results of an IAsyncOperation<T>.
// AsyncResultsTestValues<T> instance;
// AsyncResultsT<T> converted_value = instance.GetDefaultValue_T();
// converted_value == instance.GetDefaultValue_AsyncResultsT();
AsyncResultsT GetDefaultValue_AsyncResultsT();

// Returns an arbitrary value NOT equal to GetDefaultValue_T().
//
// Multiple calls to this function on a single instance will return values
// equal to one another. Calls made on different instances may produce
// equal or non-equal values.
// AsyncResultsTestValues<T> instance1;
// AsyncResultsTestValues<T> instance2;
// instance1.GetTestValue_T() == instance1.GetTestValue_T();
// instance1.GetTestValue_T() == OR != instance2.GetTestValue_T();
T GetTestValue_T();

// Returns the same value as GetTestValue_T(), but in the format expected for
// the results of an IAsyncOperation<T>.
// AsyncResultsTestValues<T> instance;
// AsyncResultsT<T> converted_value = instance.GetTestValue_T();
// converted_value == instance.GetTestValue_AsyncResultsT();
AsyncResultsT GetTestValue_AsyncResultsT();
};

// The collection of value types supported by AsyncResultsTestValues.
using AsyncResultsTestValuesTypes = ::testing::Types<int, int*, IUnknown*>;

template <>
class AsyncResultsTestValues<int> {
public:
int GetDefaultValue_T() { return 0; }
int GetDefaultValue_AsyncResultsT() { return 0; }

int GetTestValue_T() { return 4; }
int GetTestValue_AsyncResultsT() { return 4; }
};

template <>
class AsyncResultsTestValues<int*> {
public:
int* GetDefaultValue_T() { return nullptr; }
int* GetDefaultValue_AsyncResultsT() { return nullptr; }

int* GetTestValue_T() { return &test_value_; }
int* GetTestValue_AsyncResultsT() { return &test_value_; }

private:
int test_value_ = 4;
};

template <>
class AsyncResultsTestValues<IUnknown*> {
public:
AsyncResultsTestValues() {
auto class_instance = Microsoft::WRL::Make<TestClassImplementingIUnknown>();
class_instance.As(&test_value_);
}

IUnknown* GetDefaultValue_T() { return nullptr; }
Microsoft::WRL::ComPtr<IUnknown> GetDefaultValue_AsyncResultsT() {
return Microsoft::WRL::ComPtr<IUnknown>();
}

IUnknown* GetTestValue_T() { return test_value_.Get(); }
Microsoft::WRL::ComPtr<IUnknown> GetTestValue_AsyncResultsT() {
return test_value_;
}

private:
class TestClassImplementingIUnknown
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRtClassicComMix |
Microsoft::WRL::InhibitRoOriginateError>,
IUnknown> {};

Microsoft::WRL::ComPtr<IUnknown> test_value_;
};

} // namespace test
} // namespace base

#endif // BASE_TEST_ASYNC_RESULTS_TEST_VALUES_WIN_H_
202 changes: 202 additions & 0 deletions base/test/fake_iasync_operation_win_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// 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.

#include "base/test/fake_iasync_operation_win.h"

#include <asyncinfo.h>
#include <wrl/event.h>
#include <wrl/implements.h>

#include "base/test/async_results_test_values_win.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"

using ABI::Windows::Foundation::IAsyncOperation;
using ABI::Windows::Foundation::IAsyncOperationCompletedHandler;
using Microsoft::WRL::Callback;
using Microsoft::WRL::Make;

namespace base {
namespace win {
namespace {
constexpr HRESULT kTestError = 0x87654321;
}

template <typename T>
class FakeIAsyncOperationTest : public ::testing::Test {};

TYPED_TEST_SUITE_P(FakeIAsyncOperationTest);

TYPED_TEST_P(FakeIAsyncOperationTest, MultipleCompletedHandlers) {
auto operation = Make<FakeIAsyncOperation<TypeParam>>();
auto handler = Callback<IAsyncOperationCompletedHandler<TypeParam>>(
[](auto async_operation, AsyncStatus async_status) { return S_OK; });
ASSERT_HRESULT_SUCCEEDED(operation->put_Completed(handler.Get()));
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_SUCCEEDED(operation->put_Completed(handler.Get()));
, "put_Completed");
}

TYPED_TEST_P(FakeIAsyncOperationTest, MultipleCompletions) {
auto operation = Make<FakeIAsyncOperation<TypeParam>>();
base::test::AsyncResultsTestValues<TypeParam> test_values;
ASSERT_NO_FATAL_FAILURE(
operation->CompleteWithResults(test_values.GetTestValue_T()));
// EXPECT_FATAL_FAILURE() can only reference globals and statics.
// https://chromium.googlesource.com/external/github.com/google/googletest/+/HEAD/googletest/docs/advanced.md#catching-failures
static auto test_value_t = test_values.GetTestValue_T();
static auto& static_operation = operation;
EXPECT_FATAL_FAILURE(static_operation->CompleteWithResults(test_value_t),
"already completed");

operation = Make<FakeIAsyncOperation<TypeParam>>();
static_operation = operation;
ASSERT_NO_FATAL_FAILURE(operation->CompleteWithError(E_FAIL));
EXPECT_FATAL_FAILURE(static_operation->CompleteWithError(E_FAIL),
"already completed");

operation = Make<FakeIAsyncOperation<TypeParam>>();
static_operation = operation;
ASSERT_NO_FATAL_FAILURE(operation->CompleteWithError(E_FAIL));
EXPECT_FATAL_FAILURE(static_operation->CompleteWithResults(test_value_t),
"already completed");

operation = Make<FakeIAsyncOperation<TypeParam>>();
static_operation = operation;
ASSERT_NO_FATAL_FAILURE(
operation->CompleteWithResults(test_values.GetTestValue_T()));
EXPECT_FATAL_FAILURE(static_operation->CompleteWithError(E_FAIL),
"already completed");
}

TYPED_TEST_P(FakeIAsyncOperationTest, CompleteWithResults_WithHandler) {
auto operation = Make<FakeIAsyncOperation<TypeParam>>();

bool completed_handler_called = false;
ASSERT_HRESULT_SUCCEEDED(operation->put_Completed(
Callback<IAsyncOperationCompletedHandler<TypeParam>>(
[operation, &completed_handler_called](auto async_operation,
AsyncStatus async_status) {
completed_handler_called = true;
EXPECT_EQ(operation.Get(), async_operation);
EXPECT_EQ(async_status, AsyncStatus::Completed);
return S_OK;
})
.Get()));
ASSERT_FALSE(completed_handler_called);
AsyncStatus async_status;
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Started);
HRESULT error_code;
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, S_OK);
base::test::AsyncResultsTestValues<TypeParam> test_values;
auto results = test_values.GetDefaultValue_AsyncResultsT();
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(operation->GetResults(&results)), "GetResults");

operation->CompleteWithResults(test_values.GetTestValue_AsyncResultsT());
ASSERT_TRUE(completed_handler_called);
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Completed);
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, S_OK);
ASSERT_HRESULT_SUCCEEDED(operation->GetResults(&results));
ASSERT_EQ(results, test_values.GetTestValue_AsyncResultsT());
}

TYPED_TEST_P(FakeIAsyncOperationTest, CompleteWithResults_WithoutHandler) {
auto operation = Make<FakeIAsyncOperation<TypeParam>>();

AsyncStatus async_status;
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Started);
HRESULT error_code;
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, S_OK);
base::test::AsyncResultsTestValues<TypeParam> test_values;
auto results = test_values.GetDefaultValue_AsyncResultsT();
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(operation->GetResults(&results)), "GetResults");

operation->CompleteWithResults(test_values.GetTestValue_AsyncResultsT());
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Completed);
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, S_OK);
ASSERT_HRESULT_SUCCEEDED(operation->GetResults(&results));
ASSERT_EQ(results, test_values.GetTestValue_AsyncResultsT());
}

TYPED_TEST_P(FakeIAsyncOperationTest, CompleteWithError_WithHandler) {
auto operation = Make<FakeIAsyncOperation<TypeParam>>();

bool completed_handler_called = false;
ASSERT_HRESULT_SUCCEEDED(operation->put_Completed(
Callback<IAsyncOperationCompletedHandler<TypeParam>>(
[operation, &completed_handler_called](auto async_operation,
AsyncStatus async_status) {
completed_handler_called = true;
EXPECT_EQ(operation.Get(), async_operation);
EXPECT_EQ(async_status, AsyncStatus::Error);
return S_OK;
})
.Get()));
ASSERT_FALSE(completed_handler_called);
AsyncStatus async_status;
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Started);
HRESULT error_code;
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, S_OK);
base::test::AsyncResultsTestValues<TypeParam> test_values;
auto results = test_values.GetDefaultValue_AsyncResultsT();
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(operation->GetResults(&results)), "GetResults");

operation->CompleteWithError(kTestError);
ASSERT_TRUE(completed_handler_called);
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Error);
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, kTestError);
ASSERT_HRESULT_FAILED(operation->GetResults(&results));
}

TYPED_TEST_P(FakeIAsyncOperationTest, CompleteWithError_WithoutHandler) {
auto operation = Make<FakeIAsyncOperation<TypeParam>>();

AsyncStatus async_status;
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Started);
HRESULT error_code;
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, S_OK);
base::test::AsyncResultsTestValues<TypeParam> test_values;
auto results = test_values.GetDefaultValue_AsyncResultsT();
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(operation->GetResults(&results)), "GetResults");

operation->CompleteWithError(kTestError);
ASSERT_HRESULT_SUCCEEDED(operation->get_Status(&async_status));
ASSERT_EQ(async_status, AsyncStatus::Error);
ASSERT_HRESULT_SUCCEEDED(operation->get_ErrorCode(&error_code));
ASSERT_EQ(error_code, kTestError);
ASSERT_HRESULT_FAILED(operation->GetResults(&results));
}

REGISTER_TYPED_TEST_SUITE_P(FakeIAsyncOperationTest,
MultipleCompletedHandlers,
MultipleCompletions,
CompleteWithResults_WithHandler,
CompleteWithResults_WithoutHandler,
CompleteWithError_WithHandler,
CompleteWithError_WithoutHandler);

INSTANTIATE_TYPED_TEST_SUITE_P(Win,
FakeIAsyncOperationTest,
base::test::AsyncResultsTestValuesTypes);

} // namespace win
} // namespace base
Loading

0 comments on commit bd67d08

Please sign in to comment.