• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "include/base/cef_callback.h"
6 #include "include/cef_command_line.h"
7 #include "include/cef_task.h"
8 #include "include/wrapper/cef_closure_task.h"
9 #include "tests/ceftests/test_handler.h"
10 #include "tests/gtest/include/gtest/gtest.h"
11 
12 namespace {
13 
WaitForEvent(CefRefPtr<CefWaitableEvent> event)14 void WaitForEvent(CefRefPtr<CefWaitableEvent> event) {
15   if (CefCommandLine::GetGlobalCommandLine()->HasSwitch(
16           "disable-test-timeout")) {
17     event->Wait();
18   } else {
19     EXPECT_TRUE(event->TimedWait(1000));
20   }
21 }
22 
GetForCurrentThread(bool * ran_test,CefRefPtr<CefWaitableEvent> event)23 void GetForCurrentThread(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
24   // Currently on the FILE thread.
25   CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForCurrentThread();
26   EXPECT_TRUE(runner.get());
27   EXPECT_TRUE(runner->BelongsToCurrentThread());
28   EXPECT_TRUE(runner->BelongsToThread(TID_FILE_USER_VISIBLE));
29   EXPECT_FALSE(runner->BelongsToThread(TID_IO));
30   EXPECT_TRUE(runner->IsSame(runner));
31 
32   CefRefPtr<CefTaskRunner> runner2 = CefTaskRunner::GetForCurrentThread();
33   EXPECT_TRUE(runner2.get());
34   EXPECT_TRUE(runner->IsSame(runner2));
35   EXPECT_TRUE(runner2->IsSame(runner));
36 
37   // Not on the IO thread.
38   CefRefPtr<CefTaskRunner> runner3 = CefTaskRunner::GetForThread(TID_IO);
39   EXPECT_TRUE(runner3.get());
40   EXPECT_FALSE(runner->IsSame(runner3));
41   EXPECT_FALSE(runner3->IsSame(runner));
42 
43   *ran_test = true;
44   event->Signal();
45 }
46 
GetForThread(bool * ran_test,CefRefPtr<CefWaitableEvent> event)47 void GetForThread(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
48   // Currently on the FILE thread.
49   CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
50   EXPECT_TRUE(runner.get());
51   EXPECT_FALSE(runner->BelongsToCurrentThread());
52   EXPECT_TRUE(runner->BelongsToThread(TID_IO));
53   EXPECT_FALSE(runner->BelongsToThread(TID_FILE_USER_VISIBLE));
54   EXPECT_TRUE(runner->IsSame(runner));
55 
56   CefRefPtr<CefTaskRunner> runner2 = CefTaskRunner::GetForThread(TID_IO);
57   EXPECT_TRUE(runner2.get());
58   EXPECT_TRUE(runner->IsSame(runner2));
59   EXPECT_TRUE(runner2->IsSame(runner));
60 
61   CefRefPtr<CefTaskRunner> runner3 =
62       CefTaskRunner::GetForThread(TID_FILE_USER_VISIBLE);
63   EXPECT_TRUE(runner3.get());
64   EXPECT_FALSE(runner->IsSame(runner3));
65   EXPECT_FALSE(runner3->IsSame(runner));
66 
67   *ran_test = true;
68   event->Signal();
69 }
70 
PostTaskEvent1(bool * ran_test,CefRefPtr<CefWaitableEvent> event,CefRefPtr<CefTaskRunner> runner)71 void PostTaskEvent1(bool* ran_test,
72                     CefRefPtr<CefWaitableEvent> event,
73                     CefRefPtr<CefTaskRunner> runner) {
74   // Currently on the IO thread.
75   EXPECT_TRUE(runner->BelongsToCurrentThread());
76   EXPECT_TRUE(runner->BelongsToThread(TID_IO));
77   EXPECT_FALSE(runner->BelongsToThread(TID_FILE_USER_VISIBLE));
78 
79   // Current thread should be the IO thread.
80   CefRefPtr<CefTaskRunner> runner2 = CefTaskRunner::GetForCurrentThread();
81   EXPECT_TRUE(runner2.get());
82   EXPECT_TRUE(runner2->BelongsToCurrentThread());
83   EXPECT_TRUE(runner2->BelongsToThread(TID_IO));
84   EXPECT_FALSE(runner2->BelongsToThread(TID_FILE_USER_VISIBLE));
85   EXPECT_TRUE(runner->IsSame(runner2));
86   EXPECT_TRUE(runner2->IsSame(runner));
87 
88   // Current thread should be the IO thread.
89   CefRefPtr<CefTaskRunner> runner3 = CefTaskRunner::GetForThread(TID_IO);
90   EXPECT_TRUE(runner3.get());
91   EXPECT_TRUE(runner3->BelongsToCurrentThread());
92   EXPECT_TRUE(runner3->BelongsToThread(TID_IO));
93   EXPECT_FALSE(runner3->BelongsToThread(TID_FILE_USER_VISIBLE));
94   EXPECT_TRUE(runner->IsSame(runner3));
95   EXPECT_TRUE(runner3->IsSame(runner));
96 
97   // Current thread should not be the FILE thread.
98   CefRefPtr<CefTaskRunner> runner4 =
99       CefTaskRunner::GetForThread(TID_FILE_USER_VISIBLE);
100   EXPECT_TRUE(runner4.get());
101   EXPECT_FALSE(runner4->BelongsToCurrentThread());
102   EXPECT_FALSE(runner4->BelongsToThread(TID_IO));
103   EXPECT_TRUE(runner4->BelongsToThread(TID_FILE_USER_VISIBLE));
104   EXPECT_FALSE(runner->IsSame(runner4));
105   EXPECT_FALSE(runner4->IsSame(runner));
106 
107   *ran_test = true;
108   event->Signal();
109 }
110 
PostOnceTask1(bool * ran_test,CefRefPtr<CefWaitableEvent> event)111 void PostOnceTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
112   // Currently on the FILE thread.
113   CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
114   EXPECT_TRUE(runner.get());
115   EXPECT_FALSE(runner->BelongsToCurrentThread());
116   EXPECT_TRUE(runner->BelongsToThread(TID_IO));
117 
118   runner->PostTask(CefCreateClosureTask(
119       base::BindOnce(&PostTaskEvent1, ran_test, event, runner)));
120 }
121 
PostRepeatingTask1(bool * ran_test,CefRefPtr<CefWaitableEvent> event)122 void PostRepeatingTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
123   // Currently on the FILE thread.
124   CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
125   EXPECT_TRUE(runner.get());
126   EXPECT_FALSE(runner->BelongsToCurrentThread());
127   EXPECT_TRUE(runner->BelongsToThread(TID_IO));
128 
129   runner->PostTask(CefCreateClosureTask(
130       base::BindRepeating(&PostTaskEvent1, ran_test, event, runner)));
131 }
132 
PostOnceDelayedTask1(bool * ran_test,CefRefPtr<CefWaitableEvent> event)133 void PostOnceDelayedTask1(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
134   // Currently on the FILE thread.
135   CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
136   EXPECT_TRUE(runner.get());
137   EXPECT_FALSE(runner->BelongsToCurrentThread());
138   EXPECT_TRUE(runner->BelongsToThread(TID_IO));
139 
140   runner->PostDelayedTask(CefCreateClosureTask(base::BindOnce(
141                               &PostTaskEvent1, ran_test, event, runner)),
142                           0);
143 }
144 
PostRepeatingDelayedTask1(bool * ran_test,CefRefPtr<CefWaitableEvent> event)145 void PostRepeatingDelayedTask1(bool* ran_test,
146                                CefRefPtr<CefWaitableEvent> event) {
147   // Currently on the FILE thread.
148   CefRefPtr<CefTaskRunner> runner = CefTaskRunner::GetForThread(TID_IO);
149   EXPECT_TRUE(runner.get());
150   EXPECT_FALSE(runner->BelongsToCurrentThread());
151   EXPECT_TRUE(runner->BelongsToThread(TID_IO));
152 
153   runner->PostDelayedTask(CefCreateClosureTask(base::BindRepeating(
154                               &PostTaskEvent1, ran_test, event, runner)),
155                           0);
156 }
157 
PostTaskEvent2(bool * ran_test,CefRefPtr<CefWaitableEvent> event)158 void PostTaskEvent2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
159   EXPECT_TRUE(CefCurrentlyOn(TID_IO));
160   EXPECT_FALSE(CefCurrentlyOn(TID_FILE_USER_VISIBLE));
161 
162   *ran_test = true;
163   event->Signal();
164 }
165 
PostOnceTask2(bool * ran_test,CefRefPtr<CefWaitableEvent> event)166 void PostOnceTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
167   // Currently on the FILE thread.
168   EXPECT_FALSE(CefCurrentlyOn(TID_IO));
169 
170   CefPostTask(TID_IO, CefCreateClosureTask(
171                           base::BindOnce(&PostTaskEvent2, ran_test, event)));
172 }
173 
PostRepeatingTask2(bool * ran_test,CefRefPtr<CefWaitableEvent> event)174 void PostRepeatingTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
175   // Currently on the FILE thread.
176   EXPECT_FALSE(CefCurrentlyOn(TID_IO));
177 
178   CefPostTask(TID_IO, CefCreateClosureTask(base::BindRepeating(
179                           &PostTaskEvent2, ran_test, event)));
180 }
181 
PostOnceDelayedTask2(bool * ran_test,CefRefPtr<CefWaitableEvent> event)182 void PostOnceDelayedTask2(bool* ran_test, CefRefPtr<CefWaitableEvent> event) {
183   // Currently on the FILE thread.
184   EXPECT_FALSE(CefCurrentlyOn(TID_IO));
185 
186   CefPostDelayedTask(
187       TID_IO,
188       CefCreateClosureTask(base::BindOnce(&PostTaskEvent2, ran_test, event)),
189       0);
190 }
191 
PostRepeatingDelayedTask2(bool * ran_test,CefRefPtr<CefWaitableEvent> event)192 void PostRepeatingDelayedTask2(bool* ran_test,
193                                CefRefPtr<CefWaitableEvent> event) {
194   // Currently on the FILE thread.
195   EXPECT_FALSE(CefCurrentlyOn(TID_IO));
196 
197   CefPostDelayedTask(TID_IO,
198                      CefCreateClosureTask(
199                          base::BindRepeating(&PostTaskEvent2, ran_test, event)),
200                      0);
201 }
202 
203 }  // namespace
204 
TEST(TaskTest,GetOnceForCurrentThread)205 TEST(TaskTest, GetOnceForCurrentThread) {
206   bool ran_test = false;
207   CefRefPtr<CefWaitableEvent> event =
208       CefWaitableEvent::CreateWaitableEvent(true, false);
209   CefPostTask(TID_FILE_USER_VISIBLE,
210               CefCreateClosureTask(
211                   base::BindOnce(&GetForCurrentThread, &ran_test, event)));
212   WaitForEvent(event);
213   EXPECT_TRUE(ran_test);
214 }
215 
TEST(TaskTest,GetRepeatingForCurrentThread)216 TEST(TaskTest, GetRepeatingForCurrentThread) {
217   bool ran_test = false;
218   CefRefPtr<CefWaitableEvent> event =
219       CefWaitableEvent::CreateWaitableEvent(true, false);
220   CefPostTask(TID_FILE_USER_VISIBLE,
221               CefCreateClosureTask(
222                   base::BindRepeating(&GetForCurrentThread, &ran_test, event)));
223   WaitForEvent(event);
224   EXPECT_TRUE(ran_test);
225 }
226 
TEST(TaskTest,GetOnceForThread)227 TEST(TaskTest, GetOnceForThread) {
228   bool ran_test = false;
229   CefRefPtr<CefWaitableEvent> event =
230       CefWaitableEvent::CreateWaitableEvent(true, false);
231   CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindOnce(
232                                          &GetForThread, &ran_test, event)));
233   WaitForEvent(event);
234   EXPECT_TRUE(ran_test);
235 }
236 
TEST(TaskTest,GetRepeatingForThread)237 TEST(TaskTest, GetRepeatingForThread) {
238   bool ran_test = false;
239   CefRefPtr<CefWaitableEvent> event =
240       CefWaitableEvent::CreateWaitableEvent(true, false);
241   CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindRepeating(
242                                          &GetForThread, &ran_test, event)));
243   WaitForEvent(event);
244   EXPECT_TRUE(ran_test);
245 }
246 
TEST(TaskTest,PostOnceTask1)247 TEST(TaskTest, PostOnceTask1) {
248   bool ran_test = false;
249   CefRefPtr<CefWaitableEvent> event =
250       CefWaitableEvent::CreateWaitableEvent(true, false);
251   CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindOnce(
252                                          &PostOnceTask1, &ran_test, event)));
253   WaitForEvent(event);
254   EXPECT_TRUE(ran_test);
255 }
256 
TEST(TaskTest,PostRepeatingTask1)257 TEST(TaskTest, PostRepeatingTask1) {
258   bool ran_test = false;
259   CefRefPtr<CefWaitableEvent> event =
260       CefWaitableEvent::CreateWaitableEvent(true, false);
261   CefPostTask(TID_FILE_USER_VISIBLE,
262               CefCreateClosureTask(
263                   base::BindRepeating(&PostRepeatingTask1, &ran_test, event)));
264   WaitForEvent(event);
265   EXPECT_TRUE(ran_test);
266 }
267 
TEST(TaskTest,PostOnceDelayedTask1)268 TEST(TaskTest, PostOnceDelayedTask1) {
269   bool ran_test = false;
270   CefRefPtr<CefWaitableEvent> event =
271       CefWaitableEvent::CreateWaitableEvent(true, false);
272   CefPostTask(TID_FILE_USER_VISIBLE,
273               CefCreateClosureTask(
274                   base::BindOnce(&PostOnceDelayedTask1, &ran_test, event)));
275   WaitForEvent(event);
276   EXPECT_TRUE(ran_test);
277 }
278 
TEST(TaskTest,PostRepeatingDelayedTask1)279 TEST(TaskTest, PostRepeatingDelayedTask1) {
280   bool ran_test = false;
281   CefRefPtr<CefWaitableEvent> event =
282       CefWaitableEvent::CreateWaitableEvent(true, false);
283   CefPostTask(TID_FILE_USER_VISIBLE,
284               CefCreateClosureTask(base::BindRepeating(
285                   &PostRepeatingDelayedTask1, &ran_test, event)));
286   WaitForEvent(event);
287   EXPECT_TRUE(ran_test);
288 }
289 
TEST(TaskTest,PostOnceTask2)290 TEST(TaskTest, PostOnceTask2) {
291   bool ran_test = false;
292   CefRefPtr<CefWaitableEvent> event =
293       CefWaitableEvent::CreateWaitableEvent(true, false);
294   CefPostTask(TID_FILE_USER_VISIBLE, CefCreateClosureTask(base::BindOnce(
295                                          &PostOnceTask2, &ran_test, event)));
296   WaitForEvent(event);
297   EXPECT_TRUE(ran_test);
298 }
299 
TEST(TaskTest,PostRepeatingTask2)300 TEST(TaskTest, PostRepeatingTask2) {
301   bool ran_test = false;
302   CefRefPtr<CefWaitableEvent> event =
303       CefWaitableEvent::CreateWaitableEvent(true, false);
304   CefPostTask(TID_FILE_USER_VISIBLE,
305               CefCreateClosureTask(
306                   base::BindRepeating(&PostRepeatingTask2, &ran_test, event)));
307   WaitForEvent(event);
308   EXPECT_TRUE(ran_test);
309 }
310 
TEST(TaskTest,PostOnceDelayedTask2)311 TEST(TaskTest, PostOnceDelayedTask2) {
312   bool ran_test = false;
313   CefRefPtr<CefWaitableEvent> event =
314       CefWaitableEvent::CreateWaitableEvent(true, false);
315   CefPostTask(TID_FILE_USER_VISIBLE,
316               CefCreateClosureTask(
317                   base::BindOnce(&PostOnceDelayedTask2, &ran_test, event)));
318   WaitForEvent(event);
319   EXPECT_TRUE(ran_test);
320 }
321 
TEST(TaskTest,PostRepeatingDelayedTask2)322 TEST(TaskTest, PostRepeatingDelayedTask2) {
323   bool ran_test = false;
324   CefRefPtr<CefWaitableEvent> event =
325       CefWaitableEvent::CreateWaitableEvent(true, false);
326   CefPostTask(TID_FILE_USER_VISIBLE,
327               CefCreateClosureTask(base::BindRepeating(
328                   &PostRepeatingDelayedTask2, &ran_test, event)));
329   WaitForEvent(event);
330   EXPECT_TRUE(ran_test);
331 }
332