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