1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "test/run_loop.h"
12 
13 #include "rtc_base/task_queue.h"
14 #include "rtc_base/task_utils/to_queued_task.h"
15 #include "test/gtest.h"
16 
17 namespace webrtc {
18 
TEST(RunLoopTest,TaskQueueOnThread)19 TEST(RunLoopTest, TaskQueueOnThread) {
20   test::RunLoop loop;
21   EXPECT_EQ(TaskQueueBase::Current(), loop.task_queue());
22   EXPECT_TRUE(loop.task_queue()->IsCurrent());
23 }
24 
TEST(RunLoopTest,Flush)25 TEST(RunLoopTest, Flush) {
26   test::RunLoop loop;
27   int counter = 0;
28   loop.PostTask([&counter]() { ++counter; });
29   EXPECT_EQ(counter, 0);
30   loop.Flush();
31   EXPECT_EQ(counter, 1);
32 }
33 
TEST(RunLoopTest,Delayed)34 TEST(RunLoopTest, Delayed) {
35   test::RunLoop loop;
36   bool ran = false;
37   loop.PostDelayedTask(
38       [&ran, &loop]() {
39         ran = true;
40         loop.Quit();
41       },
42       100);
43   loop.Flush();
44   EXPECT_FALSE(ran);
45   loop.Run();
46   EXPECT_TRUE(ran);
47 }
48 
TEST(RunLoopTest,PostAndQuit)49 TEST(RunLoopTest, PostAndQuit) {
50   test::RunLoop loop;
51   bool ran = false;
52   loop.PostTask([&ran, &loop]() {
53     ran = true;
54     loop.Quit();
55   });
56   loop.Run();
57   EXPECT_TRUE(ran);
58 }
59 
60 }  // namespace webrtc
61