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