1 // Copyright 2020 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_TEST_ASYNC_RESULTS_TEST_VALUES_WIN_H_ 6 #define BASE_TEST_ASYNC_RESULTS_TEST_VALUES_WIN_H_ 7 8 #include <windows.foundation.collections.h> 9 #include <wrl/client.h> 10 #include <wrl/implements.h> 11 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 // Declare the related template specializations for all the value types 15 // provided. 16 namespace ABI { 17 namespace Windows { 18 namespace Foundation { 19 20 template <> 21 struct __declspec(uuid("3895C200-8F26-4F5A-B29D-2B5D72E68F99")) 22 ABI::Windows::Foundation::IAsyncOperation<IUnknown*> 23 : ABI::Windows::Foundation::IAsyncOperation_impl<IUnknown*> {}; 24 25 template <> 26 struct __declspec(uuid("CD99A253-6473-4810-AF0D-763DAB79AC42")) 27 ABI::Windows::Foundation::IAsyncOperationCompletedHandler<IUnknown*> 28 : ABI::Windows::Foundation::IAsyncOperationCompletedHandler_impl< 29 IUnknown*> {}; 30 31 template <> 32 struct __declspec(uuid("CB52D855-8121-4AC8-A164-084A27FB377E")) 33 ABI::Windows::Foundation::IAsyncOperation<int*> 34 : ABI::Windows::Foundation::IAsyncOperation_impl<int*> {}; 35 36 template <> 37 struct __declspec(uuid("EA868415-A724-40BC-950A-C7DB6B1723C6")) 38 ABI::Windows::Foundation::IAsyncOperationCompletedHandler<int*> 39 : ABI::Windows::Foundation::IAsyncOperationCompletedHandler_impl<int*> {}; 40 41 // These specialization templates were included in windows.foundation.h, but 42 // removed in 10.0.19041.0 SDK, so are included here conditionally 43 #ifdef NTDDI_WIN10_VB // Windows 10.0.19041 44 template <> 45 struct __declspec(uuid("968b9665-06ed-5774-8f53-8edeabd5f7b5")) 46 ABI::Windows::Foundation::IAsyncOperation<int> 47 : ABI::Windows::Foundation::IAsyncOperation_impl<int> {}; 48 49 template <> 50 struct __declspec(uuid("d60cae9d-88cb-59f1-8576-3fba44796be8")) 51 ABI::Windows::Foundation::IAsyncOperationCompletedHandler<int> 52 : ABI::Windows::Foundation::IAsyncOperationCompletedHandler_impl<int> {}; 53 #endif 54 55 } // namespace Foundation 56 } // namespace Windows 57 } // namespace ABI 58 59 namespace base { 60 namespace test { 61 // Provides access to values of type |T| and variations of those values relevant 62 // to IAsyncOperations. Intended for use in TypedTestSuites concerning 63 // IAsyncOperations or related functionality. Example: 64 // template <typename T> 65 // class SuiteName : public ::testing::Test {}; 66 // 67 // TYPED_TEST_SUITE_P(SuiteName); 68 // 69 // TYPED_TEST_P(SuiteName, TestName) { 70 // AsyncResultsTestValues<TypeParam> test_values; 71 // ... test_values.GetTestValue_T() ... 72 // } 73 // 74 // REGISTER_TYPED_TEST_SUITE_P(SuiteName, TestName); 75 // INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, 76 // SuiteName, 77 // base::test::AsyncResultsTestValuesTypes); 78 template <typename T> 79 class AsyncResultsTestValues { 80 // This class body serves only to provide documentation for the functions. 81 // Actual use of this class is limited to the types for which it has been 82 // specialized. 83 private: 84 class AsyncResultsT; 85 86 public: 87 // Returns a value equal to a variable of type T constructed with an 88 // empty initializer. 89 // 90 // This value will be equal between all instances of the same type. 91 // AsyncResultsTestValues<T> instance1; 92 // AsyncResultsTestValues<T> instance2; 93 // instance1.GetDefaultValue_T() == instance2.GetDefaultValue_T(); 94 T GetDefaultValue_T(); 95 96 // Returns the same value as GetDefaultValue_T(), but in the format expected 97 // for the results of an IAsyncOperation<T>. 98 // AsyncResultsTestValues<T> instance; 99 // AsyncResultsT<T> converted_value = instance.GetDefaultValue_T(); 100 // converted_value == instance.GetDefaultValue_AsyncResultsT(); 101 AsyncResultsT GetDefaultValue_AsyncResultsT(); 102 103 // Returns an arbitrary value NOT equal to GetDefaultValue_T(). 104 // 105 // Multiple calls to this function on a single instance will return values 106 // equal to one another. Calls made on different instances may produce 107 // equal or non-equal values. 108 // AsyncResultsTestValues<T> instance1; 109 // AsyncResultsTestValues<T> instance2; 110 // instance1.GetTestValue_T() == instance1.GetTestValue_T(); 111 // instance1.GetTestValue_T() == OR != instance2.GetTestValue_T(); 112 T GetTestValue_T(); 113 114 // Returns the same value as GetTestValue_T(), but in the format expected for 115 // the results of an IAsyncOperation<T>. 116 // AsyncResultsTestValues<T> instance; 117 // AsyncResultsT<T> converted_value = instance.GetTestValue_T(); 118 // converted_value == instance.GetTestValue_AsyncResultsT(); 119 AsyncResultsT GetTestValue_AsyncResultsT(); 120 }; 121 122 // The collection of value types supported by AsyncResultsTestValues. 123 using AsyncResultsTestValuesTypes = ::testing::Types<int, int*, IUnknown*>; 124 125 template <> 126 class AsyncResultsTestValues<int> { 127 public: 128 int GetDefaultValue_T() { return 0; } 129 int GetDefaultValue_AsyncResultsT() { return 0; } 130 131 int GetTestValue_T() { return 4; } 132 int GetTestValue_AsyncResultsT() { return 4; } 133 }; 134 135 template <> 136 class AsyncResultsTestValues<int*> { 137 public: 138 int* GetDefaultValue_T() { return nullptr; } 139 int* GetDefaultValue_AsyncResultsT() { return nullptr; } 140 141 int* GetTestValue_T() { return &test_value_; } 142 int* GetTestValue_AsyncResultsT() { return &test_value_; } 143 144 private: 145 int test_value_ = 4; 146 }; 147 148 template <> 149 class AsyncResultsTestValues<IUnknown*> { 150 public: 151 AsyncResultsTestValues() { 152 auto class_instance = Microsoft::WRL::Make<TestClassImplementingIUnknown>(); 153 class_instance.As(&test_value_); 154 } 155 156 IUnknown* GetDefaultValue_T() { return nullptr; } 157 Microsoft::WRL::ComPtr<IUnknown> GetDefaultValue_AsyncResultsT() { 158 return Microsoft::WRL::ComPtr<IUnknown>(); 159 } 160 161 IUnknown* GetTestValue_T() { return test_value_.Get(); } 162 Microsoft::WRL::ComPtr<IUnknown> GetTestValue_AsyncResultsT() { 163 return test_value_; 164 } 165 166 private: 167 class TestClassImplementingIUnknown 168 : public Microsoft::WRL::RuntimeClass< 169 Microsoft::WRL::RuntimeClassFlags< 170 Microsoft::WRL::WinRtClassicComMix | 171 Microsoft::WRL::InhibitRoOriginateError>, 172 IUnknown> {}; 173 174 Microsoft::WRL::ComPtr<IUnknown> test_value_; 175 }; 176 177 } // namespace test 178 } // namespace base 179 180 #endif // BASE_TEST_ASYNC_RESULTS_TEST_VALUES_WIN_H_ 181