async_results_test_values_win.h 6.2 KB

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