post_async_results_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include "base/win/post_async_results.h"
  5. #include "base/test/async_results_test_values_win.h"
  6. #include "base/test/bind.h"
  7. #include "base/test/fake_iasync_operation_win.h"
  8. #include "base/test/task_environment.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. using ABI::Windows::Foundation::IAsyncOperation;
  11. using Microsoft::WRL::ComPtr;
  12. namespace base {
  13. namespace win {
  14. template <typename T>
  15. class PostAsyncResultsTest : public ::testing::Test {};
  16. TYPED_TEST_SUITE_P(PostAsyncResultsTest);
  17. TYPED_TEST_P(PostAsyncResultsTest, GetAsyncResultsT_Success) {
  18. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  19. ComPtr<IAsyncOperation<TypeParam>> async_op;
  20. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  21. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  22. ASSERT_NO_FATAL_FAILURE(
  23. fake_iasync_op->CompleteWithResults(templated_values.GetTestValue_T()));
  24. AsyncStatus async_status;
  25. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op->get_Status(&async_status));
  26. auto value_received = templated_values.GetDefaultValue_AsyncResultsT();
  27. ASSERT_HRESULT_SUCCEEDED(internal::GetAsyncResultsT(
  28. async_op.Get(), async_status, &value_received));
  29. ASSERT_EQ(templated_values.GetTestValue_AsyncResultsT(), value_received);
  30. }
  31. TYPED_TEST_P(PostAsyncResultsTest, GetAsyncResultsT_Failure) {
  32. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  33. ComPtr<IAsyncOperation<TypeParam>> async_op;
  34. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  35. HRESULT test_error = (HRESULT)0x87654321L;
  36. ASSERT_NO_FATAL_FAILURE(fake_iasync_op->CompleteWithError(test_error));
  37. AsyncStatus async_status;
  38. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op->get_Status(&async_status));
  39. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  40. auto value_received = templated_values.GetTestValue_AsyncResultsT();
  41. ASSERT_EQ(
  42. internal::GetAsyncResultsT(async_op.Get(), async_status, &value_received),
  43. test_error);
  44. ASSERT_EQ(templated_values.GetDefaultValue_AsyncResultsT(), value_received);
  45. }
  46. TYPED_TEST_P(PostAsyncResultsTest, PostAsyncOperationCompletedHandler_Success) {
  47. base::test::SingleThreadTaskEnvironment task_environment;
  48. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  49. ComPtr<IAsyncOperation<TypeParam>> async_op;
  50. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  51. RunLoop run_loop;
  52. auto quit_closure = run_loop.QuitClosure();
  53. IAsyncOperation<TypeParam>* async_op_received;
  54. AsyncStatus async_status_received = AsyncStatus::Started;
  55. internal::IAsyncOperationCompletedHandlerT<TypeParam> completed_handler =
  56. base::BindLambdaForTesting(
  57. [&](IAsyncOperation<TypeParam>* async_operation,
  58. AsyncStatus async_status) {
  59. async_op_received = async_operation;
  60. async_status_received = async_status;
  61. std::move(quit_closure).Run();
  62. });
  63. ASSERT_HRESULT_SUCCEEDED(internal::PostAsyncOperationCompletedHandler(
  64. async_op.Get(), std::move(completed_handler)));
  65. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  66. ASSERT_NO_FATAL_FAILURE(
  67. fake_iasync_op->CompleteWithResults(templated_values.GetTestValue_T()));
  68. run_loop.Run();
  69. ASSERT_EQ(async_op.Get(), async_op_received);
  70. ASSERT_EQ(AsyncStatus::Completed, async_status_received);
  71. }
  72. TYPED_TEST_P(PostAsyncResultsTest, PostAsyncOperationCompletedHandler_Failure) {
  73. base::test::SingleThreadTaskEnvironment task_environment;
  74. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  75. ComPtr<IAsyncOperation<TypeParam>> async_op;
  76. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  77. RunLoop run_loop;
  78. auto quit_closure = run_loop.QuitClosure();
  79. IAsyncOperation<TypeParam>* async_op_received;
  80. AsyncStatus async_status_received = AsyncStatus::Started;
  81. internal::IAsyncOperationCompletedHandlerT<TypeParam> completed_handler =
  82. base::BindLambdaForTesting(
  83. [&](IAsyncOperation<TypeParam>* async_operation,
  84. AsyncStatus async_status) {
  85. async_op_received = async_operation;
  86. async_status_received = async_status;
  87. std::move(quit_closure).Run();
  88. });
  89. ASSERT_HRESULT_SUCCEEDED(internal::PostAsyncOperationCompletedHandler(
  90. async_op.Get(), std::move(completed_handler)));
  91. ASSERT_NO_FATAL_FAILURE(fake_iasync_op->CompleteWithError(E_FAIL));
  92. run_loop.Run();
  93. ASSERT_EQ(async_op.Get(), async_op_received);
  94. ASSERT_EQ(AsyncStatus::Error, async_status_received);
  95. }
  96. TYPED_TEST_P(PostAsyncResultsTest,
  97. PostAsyncHandlers_OnlySuccessHandler_Success) {
  98. base::test::SingleThreadTaskEnvironment task_environment;
  99. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  100. ComPtr<IAsyncOperation<TypeParam>> async_op;
  101. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  102. RunLoop run_loop;
  103. auto quit_closure = run_loop.QuitClosure();
  104. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  105. auto value_received = templated_values.GetDefaultValue_AsyncResultsT();
  106. ASSERT_HRESULT_SUCCEEDED(PostAsyncHandlers(
  107. async_op.Get(), base::BindLambdaForTesting(
  108. [&](internal::AsyncResultsT<TypeParam> result) {
  109. value_received = result;
  110. std::move(quit_closure).Run();
  111. })));
  112. ASSERT_NO_FATAL_FAILURE(
  113. fake_iasync_op->CompleteWithResults(templated_values.GetTestValue_T()));
  114. run_loop.Run();
  115. ASSERT_EQ(templated_values.GetTestValue_AsyncResultsT(), value_received);
  116. }
  117. TYPED_TEST_P(PostAsyncResultsTest,
  118. PostAsyncHandlers_OnlySuccessHandler_Failure) {
  119. base::test::SingleThreadTaskEnvironment task_environment;
  120. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  121. ComPtr<IAsyncOperation<TypeParam>> async_op;
  122. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  123. RunLoop run_loop;
  124. bool success_handler_called = false;
  125. ASSERT_HRESULT_SUCCEEDED(PostAsyncHandlers(
  126. async_op.Get(), base::BindLambdaForTesting(
  127. [&](internal::AsyncResultsT<TypeParam> result) {
  128. success_handler_called = true;
  129. })));
  130. HRESULT test_error = (HRESULT)0x87654321L;
  131. ASSERT_NO_FATAL_FAILURE(fake_iasync_op->CompleteWithError(test_error));
  132. run_loop.RunUntilIdle();
  133. ASSERT_FALSE(success_handler_called);
  134. }
  135. TYPED_TEST_P(PostAsyncResultsTest,
  136. PostAsyncHandlers_NoArgsFailureHandler_Success) {
  137. base::test::SingleThreadTaskEnvironment task_environment;
  138. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  139. ComPtr<IAsyncOperation<TypeParam>> async_op;
  140. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  141. RunLoop run_loop;
  142. auto quit_closure = run_loop.QuitClosure();
  143. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  144. auto value_received = templated_values.GetDefaultValue_AsyncResultsT();
  145. bool failure_handler_called = false;
  146. ASSERT_HRESULT_SUCCEEDED(
  147. PostAsyncHandlers(async_op.Get(),
  148. base::BindLambdaForTesting(
  149. [&](internal::AsyncResultsT<TypeParam> result) {
  150. value_received = result;
  151. std::move(quit_closure).Run();
  152. }),
  153. base::BindLambdaForTesting([&]() {
  154. failure_handler_called = true;
  155. std::move(quit_closure).Run();
  156. })));
  157. ASSERT_NO_FATAL_FAILURE(
  158. fake_iasync_op->CompleteWithResults(templated_values.GetTestValue_T()));
  159. run_loop.Run();
  160. ASSERT_EQ(templated_values.GetTestValue_AsyncResultsT(), value_received);
  161. ASSERT_FALSE(failure_handler_called);
  162. }
  163. TYPED_TEST_P(PostAsyncResultsTest,
  164. PostAsyncHandlers_NoArgsFailureHandler_Failure) {
  165. base::test::SingleThreadTaskEnvironment task_environment;
  166. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  167. ComPtr<IAsyncOperation<TypeParam>> async_op;
  168. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  169. RunLoop run_loop;
  170. auto quit_closure = run_loop.QuitClosure();
  171. bool failure_handler_called = false;
  172. bool success_handler_called = false;
  173. ASSERT_HRESULT_SUCCEEDED(
  174. PostAsyncHandlers(async_op.Get(),
  175. base::BindLambdaForTesting(
  176. [&](internal::AsyncResultsT<TypeParam> result) {
  177. success_handler_called = true;
  178. std::move(quit_closure).Run();
  179. }),
  180. base::BindLambdaForTesting([&]() {
  181. failure_handler_called = true;
  182. std::move(quit_closure).Run();
  183. })));
  184. HRESULT test_error = (HRESULT)0x87654321L;
  185. ASSERT_NO_FATAL_FAILURE(fake_iasync_op->CompleteWithError(test_error));
  186. run_loop.Run();
  187. ASSERT_FALSE(success_handler_called);
  188. ASSERT_TRUE(failure_handler_called);
  189. }
  190. TYPED_TEST_P(PostAsyncResultsTest,
  191. PostAsyncHandlers_HRESULTFailureHandler_Success) {
  192. base::test::SingleThreadTaskEnvironment task_environment;
  193. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  194. ComPtr<IAsyncOperation<TypeParam>> async_op;
  195. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  196. RunLoop run_loop;
  197. auto quit_closure = run_loop.QuitClosure();
  198. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  199. auto value_received = templated_values.GetDefaultValue_AsyncResultsT();
  200. bool failure_handler_called = false;
  201. ASSERT_HRESULT_SUCCEEDED(
  202. PostAsyncHandlers(async_op.Get(),
  203. base::BindLambdaForTesting(
  204. [&](internal::AsyncResultsT<TypeParam> result) {
  205. value_received = result;
  206. std::move(quit_closure).Run();
  207. }),
  208. base::BindLambdaForTesting([&](HRESULT hr) {
  209. failure_handler_called = true;
  210. std::move(quit_closure).Run();
  211. })));
  212. ASSERT_NO_FATAL_FAILURE(
  213. fake_iasync_op->CompleteWithResults(templated_values.GetTestValue_T()));
  214. run_loop.Run();
  215. ASSERT_EQ(templated_values.GetTestValue_AsyncResultsT(), value_received);
  216. ASSERT_FALSE(failure_handler_called);
  217. }
  218. TYPED_TEST_P(PostAsyncResultsTest,
  219. PostAsyncHandlers_HRESULTFailureHandler_Failure) {
  220. base::test::SingleThreadTaskEnvironment task_environment;
  221. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  222. ComPtr<IAsyncOperation<TypeParam>> async_op;
  223. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  224. RunLoop run_loop;
  225. auto quit_closure = run_loop.QuitClosure();
  226. bool success_handler_called = false;
  227. HRESULT hr_received = S_OK;
  228. ASSERT_HRESULT_SUCCEEDED(
  229. PostAsyncHandlers(async_op.Get(),
  230. base::BindLambdaForTesting(
  231. [&](internal::AsyncResultsT<TypeParam> result) {
  232. success_handler_called = true;
  233. std::move(quit_closure).Run();
  234. }),
  235. base::BindLambdaForTesting([&](HRESULT hr) {
  236. hr_received = hr;
  237. std::move(quit_closure).Run();
  238. })));
  239. HRESULT test_error = (HRESULT)0x87654321L;
  240. ASSERT_NO_FATAL_FAILURE(fake_iasync_op->CompleteWithError(test_error));
  241. run_loop.Run();
  242. ASSERT_FALSE(success_handler_called);
  243. ASSERT_EQ(test_error, hr_received);
  244. }
  245. TYPED_TEST_P(PostAsyncResultsTest,
  246. PostAsyncHandlers_HRESULTAndResultFailureHandler_Success) {
  247. base::test::SingleThreadTaskEnvironment task_environment;
  248. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  249. ComPtr<IAsyncOperation<TypeParam>> async_op;
  250. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  251. RunLoop run_loop;
  252. auto quit_closure = run_loop.QuitClosure();
  253. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  254. auto value_received = templated_values.GetDefaultValue_AsyncResultsT();
  255. bool failure_handler_called = false;
  256. ASSERT_HRESULT_SUCCEEDED(PostAsyncHandlers(
  257. async_op.Get(),
  258. base::BindLambdaForTesting(
  259. [&](internal::AsyncResultsT<TypeParam> result) {
  260. value_received = result;
  261. std::move(quit_closure).Run();
  262. }),
  263. base::BindLambdaForTesting(
  264. [&](HRESULT hr, internal::AsyncResultsT<TypeParam> result) {
  265. failure_handler_called = true;
  266. std::move(quit_closure).Run();
  267. })));
  268. ASSERT_NO_FATAL_FAILURE(
  269. fake_iasync_op->CompleteWithResults(templated_values.GetTestValue_T()));
  270. run_loop.Run();
  271. ASSERT_EQ(templated_values.GetTestValue_AsyncResultsT(), value_received);
  272. ASSERT_FALSE(failure_handler_called);
  273. }
  274. TYPED_TEST_P(PostAsyncResultsTest,
  275. PostAsyncHandlers_HRESULTAndResultFailureHandler_Failure) {
  276. base::test::SingleThreadTaskEnvironment task_environment;
  277. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  278. ComPtr<IAsyncOperation<TypeParam>> async_op;
  279. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  280. RunLoop run_loop;
  281. auto quit_closure = run_loop.QuitClosure();
  282. bool success_handler_called = false;
  283. HRESULT hr_received = S_OK;
  284. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  285. auto value_received = templated_values.GetDefaultValue_AsyncResultsT();
  286. ASSERT_HRESULT_SUCCEEDED(PostAsyncHandlers(
  287. async_op.Get(),
  288. base::BindLambdaForTesting(
  289. [&](internal::AsyncResultsT<TypeParam> result) {
  290. success_handler_called = true;
  291. std::move(quit_closure).Run();
  292. }),
  293. base::BindLambdaForTesting(
  294. [&](HRESULT hr, internal::AsyncResultsT<TypeParam> result) {
  295. hr_received = hr;
  296. value_received = result;
  297. std::move(quit_closure).Run();
  298. })));
  299. ASSERT_NO_FATAL_FAILURE(fake_iasync_op->CompleteWithErrorResult(
  300. templated_values.GetTestValue_T()));
  301. run_loop.Run();
  302. ASSERT_FALSE(success_handler_called);
  303. ASSERT_HRESULT_SUCCEEDED(hr_received);
  304. ASSERT_EQ(templated_values.GetTestValue_AsyncResultsT(), value_received);
  305. }
  306. TYPED_TEST_P(PostAsyncResultsTest, PostAsyncResults_Success) {
  307. base::test::SingleThreadTaskEnvironment task_environment;
  308. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  309. ComPtr<IAsyncOperation<TypeParam>> async_op;
  310. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  311. RunLoop run_loop;
  312. auto quit_closure = run_loop.QuitClosure();
  313. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  314. auto value_received = templated_values.GetDefaultValue_AsyncResultsT();
  315. ASSERT_HRESULT_SUCCEEDED(PostAsyncResults(
  316. async_op, base::BindLambdaForTesting(
  317. [&](internal::AsyncResultsT<TypeParam> result) {
  318. value_received = result;
  319. std::move(quit_closure).Run();
  320. })));
  321. ASSERT_NO_FATAL_FAILURE(
  322. fake_iasync_op->CompleteWithResults(templated_values.GetTestValue_T()));
  323. run_loop.Run();
  324. ASSERT_EQ(templated_values.GetTestValue_AsyncResultsT(), value_received);
  325. }
  326. TYPED_TEST_P(PostAsyncResultsTest, PostAsyncResults_Failure) {
  327. base::test::SingleThreadTaskEnvironment task_environment;
  328. auto fake_iasync_op = Microsoft::WRL::Make<FakeIAsyncOperation<TypeParam>>();
  329. ComPtr<IAsyncOperation<TypeParam>> async_op;
  330. ASSERT_HRESULT_SUCCEEDED(fake_iasync_op.As(&async_op));
  331. RunLoop run_loop;
  332. auto quit_closure = run_loop.QuitClosure();
  333. base::test::AsyncResultsTestValues<TypeParam> templated_values;
  334. auto value_received = templated_values.GetTestValue_AsyncResultsT();
  335. ASSERT_HRESULT_SUCCEEDED(PostAsyncResults(
  336. async_op, base::BindLambdaForTesting(
  337. [&](internal::AsyncResultsT<TypeParam> result) {
  338. value_received = result;
  339. std::move(quit_closure).Run();
  340. })));
  341. ASSERT_NO_FATAL_FAILURE(fake_iasync_op->CompleteWithError(E_FAIL));
  342. run_loop.Run();
  343. ASSERT_EQ(templated_values.GetDefaultValue_AsyncResultsT(), value_received);
  344. }
  345. REGISTER_TYPED_TEST_SUITE_P(
  346. PostAsyncResultsTest,
  347. GetAsyncResultsT_Success,
  348. GetAsyncResultsT_Failure,
  349. PostAsyncOperationCompletedHandler_Success,
  350. PostAsyncOperationCompletedHandler_Failure,
  351. PostAsyncHandlers_OnlySuccessHandler_Success,
  352. PostAsyncHandlers_OnlySuccessHandler_Failure,
  353. PostAsyncHandlers_NoArgsFailureHandler_Success,
  354. PostAsyncHandlers_NoArgsFailureHandler_Failure,
  355. PostAsyncHandlers_HRESULTFailureHandler_Success,
  356. PostAsyncHandlers_HRESULTFailureHandler_Failure,
  357. PostAsyncHandlers_HRESULTAndResultFailureHandler_Success,
  358. PostAsyncHandlers_HRESULTAndResultFailureHandler_Failure,
  359. PostAsyncResults_Success,
  360. PostAsyncResults_Failure);
  361. INSTANTIATE_TYPED_TEST_SUITE_P(Win,
  362. PostAsyncResultsTest,
  363. base::test::AsyncResultsTestValuesTypes);
  364. } // namespace win
  365. } // namespace base