[M85 Dev][EFL] Fix crashes at webview launch
[platform/framework/web/chromium-efl.git] / base / task_runner_unittest.cc
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 <utility>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/run_loop.h"
12 #include "base/test/task_environment.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace base {
17
18 namespace {
19
20 int ReturnFourtyTwo() {
21   return 42;
22 }
23
24 void StoreValue(int* destination, int value) {
25   *destination = value;
26 }
27
28 void StoreDoubleValue(double* destination, double value) {
29   *destination = value;
30 }
31
32 int g_foo_destruct_count = 0;
33 int g_foo_free_count = 0;
34
35 struct Foo {
36   ~Foo() { ++g_foo_destruct_count; }
37 };
38
39 std::unique_ptr<Foo> CreateFoo() {
40   return std::unique_ptr<Foo>(new Foo);
41 }
42
43 void ExpectFoo(std::unique_ptr<Foo> foo) {
44   EXPECT_TRUE(foo.get());
45   std::unique_ptr<Foo> local_foo(std::move(foo));
46   EXPECT_TRUE(local_foo.get());
47   EXPECT_FALSE(foo.get());
48 }
49
50 struct FooDeleter {
51   void operator()(Foo* foo) const {
52     ++g_foo_free_count;
53     delete foo;
54   }
55 };
56
57 std::unique_ptr<Foo, FooDeleter> CreateScopedFoo() {
58   return std::unique_ptr<Foo, FooDeleter>(new Foo);
59 }
60
61 void ExpectScopedFoo(std::unique_ptr<Foo, FooDeleter> foo) {
62   EXPECT_TRUE(foo.get());
63   std::unique_ptr<Foo, FooDeleter> local_foo(std::move(foo));
64   EXPECT_TRUE(local_foo.get());
65   EXPECT_FALSE(foo.get());
66 }
67
68 struct FooWithoutDefaultConstructor {
69   explicit FooWithoutDefaultConstructor(int value) : value(value) {}
70   int value;
71 };
72
73 FooWithoutDefaultConstructor CreateFooWithoutDefaultConstructor(int value) {
74   return FooWithoutDefaultConstructor(value);
75 }
76
77 void SaveFooWithoutDefaultConstructor(int* output_value,
78                                       FooWithoutDefaultConstructor input) {
79   *output_value = input.value;
80 }
81
82 class TaskRunnerTest : public testing::Test {
83  public:
84   TaskRunnerTest() = default;
85
86   void SetUp() override {
87     g_foo_destruct_count = 0;
88     g_foo_free_count = 0;
89   }
90 };
91
92 }  // namespace
93
94 TEST_F(TaskRunnerTest, PostTaskAndReplyWithResult) {
95   int result = 0;
96
97   test::SingleThreadTaskEnvironment task_environment;
98   ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
99       FROM_HERE, BindOnce(&ReturnFourtyTwo), BindOnce(&StoreValue, &result));
100
101   RunLoop().RunUntilIdle();
102
103   EXPECT_EQ(42, result);
104 }
105
106 TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultImplicitConvert) {
107   double result = 0;
108
109   test::SingleThreadTaskEnvironment task_environment;
110   ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
111       FROM_HERE, BindOnce(&ReturnFourtyTwo),
112       BindOnce(&StoreDoubleValue, &result));
113
114   RunLoop().RunUntilIdle();
115
116   EXPECT_DOUBLE_EQ(42.0, result);
117 }
118
119 TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultPassed) {
120   test::SingleThreadTaskEnvironment task_environment;
121   ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
122       FROM_HERE, BindOnce(&CreateFoo), BindOnce(&ExpectFoo));
123
124   RunLoop().RunUntilIdle();
125
126   EXPECT_EQ(1, g_foo_destruct_count);
127   EXPECT_EQ(0, g_foo_free_count);
128 }
129
130 TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultPassedFreeProc) {
131   test::SingleThreadTaskEnvironment task_environment;
132   ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
133       FROM_HERE, BindOnce(&CreateScopedFoo), BindOnce(&ExpectScopedFoo));
134
135   RunLoop().RunUntilIdle();
136
137   EXPECT_EQ(1, g_foo_destruct_count);
138   EXPECT_EQ(1, g_foo_free_count);
139 }
140
141 TEST_F(TaskRunnerTest, PostTaskAndReplyWithResultWithoutDefaultConstructor) {
142   const int kSomeVal = 17;
143
144   test::SingleThreadTaskEnvironment task_environment;
145   int actual = 0;
146
147   ThreadTaskRunnerHandle::Get()->PostTaskAndReplyWithResult(
148       FROM_HERE, BindOnce(&CreateFooWithoutDefaultConstructor, kSomeVal),
149       BindOnce(&SaveFooWithoutDefaultConstructor, &actual));
150
151   RunLoop().RunUntilIdle();
152
153   EXPECT_EQ(kSomeVal, actual);
154 }
155
156 }  // namespace base