[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "base/task_runner_util.h" |
| 6 | |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 9 | #include "base/bind.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 10 | #include "base/location.h" |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 11 | #include "base/run_loop.h" |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace base { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | int ReturnFourtyTwo() { |
| 19 | return 42; |
| 20 | } |
| 21 | |
| 22 | void StoreValue(int* destination, int value) { |
| 23 | *destination = value; |
| 24 | } |
| 25 | |
[email protected] | 89590681 | 2012-11-28 03:29:01 | [diff] [blame] | 26 | void StoreDoubleValue(double* destination, double value) { |
| 27 | *destination = value; |
| 28 | } |
| 29 | |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 30 | int g_foo_destruct_count = 0; |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 31 | int g_foo_free_count = 0; |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 32 | |
| 33 | struct Foo { |
| 34 | ~Foo() { |
| 35 | ++g_foo_destruct_count; |
| 36 | } |
| 37 | }; |
| 38 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 39 | std::unique_ptr<Foo> CreateFoo() { |
| 40 | return std::unique_ptr<Foo>(new Foo); |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 41 | } |
| 42 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 43 | void ExpectFoo(std::unique_ptr<Foo> foo) { |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 44 | EXPECT_TRUE(foo.get()); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 45 | std::unique_ptr<Foo> local_foo(std::move(foo)); |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 46 | EXPECT_TRUE(local_foo.get()); |
| 47 | EXPECT_FALSE(foo.get()); |
| 48 | } |
| 49 | |
[email protected] | 6ae7ee76 | 2014-02-16 22:18:43 | [diff] [blame] | 50 | struct FooDeleter { |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 51 | void operator()(Foo* foo) const { |
| 52 | ++g_foo_free_count; |
[email protected] | 7431930 | 2012-05-10 20:08:03 | [diff] [blame] | 53 | delete foo; |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 54 | }; |
| 55 | }; |
| 56 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 57 | std::unique_ptr<Foo, FooDeleter> CreateScopedFoo() { |
| 58 | return std::unique_ptr<Foo, FooDeleter>(new Foo); |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 59 | } |
| 60 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 61 | void ExpectScopedFoo(std::unique_ptr<Foo, FooDeleter> foo) { |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 62 | EXPECT_TRUE(foo.get()); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 63 | std::unique_ptr<Foo, FooDeleter> local_foo(std::move(foo)); |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 64 | EXPECT_TRUE(local_foo.get()); |
| 65 | EXPECT_FALSE(foo.get()); |
| 66 | } |
| 67 | |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 68 | } // namespace |
| 69 | |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 70 | TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResult) { |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 71 | int result = 0; |
| 72 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 73 | MessageLoop message_loop; |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 74 | PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE, |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 75 | Bind(&ReturnFourtyTwo), |
| 76 | Bind(&StoreValue, &result)); |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 77 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 78 | RunLoop().RunUntilIdle(); |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 79 | |
| 80 | EXPECT_EQ(42, result); |
| 81 | } |
| 82 | |
[email protected] | 89590681 | 2012-11-28 03:29:01 | [diff] [blame] | 83 | TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultImplicitConvert) { |
[email protected] | 89590681 | 2012-11-28 03:29:01 | [diff] [blame] | 84 | double result = 0; |
| 85 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 86 | MessageLoop message_loop; |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 87 | PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE, |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 88 | Bind(&ReturnFourtyTwo), |
| 89 | Bind(&StoreDoubleValue, &result)); |
[email protected] | 89590681 | 2012-11-28 03:29:01 | [diff] [blame] | 90 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 91 | RunLoop().RunUntilIdle(); |
[email protected] | 89590681 | 2012-11-28 03:29:01 | [diff] [blame] | 92 | |
| 93 | EXPECT_DOUBLE_EQ(42.0, result); |
| 94 | } |
| 95 | |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 96 | TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassed) { |
| 97 | g_foo_destruct_count = 0; |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 98 | g_foo_free_count = 0; |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 99 | |
| 100 | MessageLoop message_loop; |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 101 | PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE, |
| 102 | Bind(&CreateFoo), Bind(&ExpectFoo)); |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 103 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 104 | RunLoop().RunUntilIdle(); |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 105 | |
| 106 | EXPECT_EQ(1, g_foo_destruct_count); |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 107 | EXPECT_EQ(0, g_foo_free_count); |
[email protected] | 7431930 | 2012-05-10 20:08:03 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST(TaskRunnerHelpersTest, PostTaskAndReplyWithResultPassedFreeProc) { |
| 111 | g_foo_destruct_count = 0; |
| 112 | g_foo_free_count = 0; |
| 113 | |
| 114 | MessageLoop message_loop; |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 115 | PostTaskAndReplyWithResult(message_loop.task_runner().get(), FROM_HERE, |
| 116 | Bind(&CreateScopedFoo), Bind(&ExpectScopedFoo)); |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 117 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 118 | RunLoop().RunUntilIdle(); |
[email protected] | 89ae3d88 | 2012-05-09 00:16:25 | [diff] [blame] | 119 | |
| 120 | EXPECT_EQ(1, g_foo_destruct_count); |
| 121 | EXPECT_EQ(1, g_foo_free_count); |
[email protected] | d53e6a6 | 2012-05-03 22:34:17 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 95991b1 | 2012-04-17 02:48:06 | [diff] [blame] | 124 | } // namespace base |