xref: /aosp_15_r20/external/cronet/base/test/test_pending_task_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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/test/test_pending_task.h"
6 
7 #include "base/functional/bind.h"
8 #include "base/trace_event/base_tracing.h"
9 #include "base/tracing_buildflags.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest-spi.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace base {
15 
16 #if BUILDFLAG(ENABLE_BASE_TRACING)
TEST(TestPendingTaskTest,TraceSupport)17 TEST(TestPendingTaskTest, TraceSupport) {
18   base::TestPendingTask task;
19 
20   // Check that TestPendingTask can be sent to the trace subsystem.
21   TRACE_EVENT1("test", "TestPendingTask::TraceSupport", "task", task.AsValue());
22 
23   // Just a basic check that the trace output has *something* in it.
24   base::trace_event::TracedValueJSON task_value;
25   task.AsValueInto(&task_value);
26   EXPECT_THAT(task_value.ToJSON(), ::testing::HasSubstr("post_time"));
27 }
28 #endif  // BUILDFLAG(ENABLE_BASE_TRACING)
29 
TEST(TestPendingTaskTest,ToString)30 TEST(TestPendingTaskTest, ToString) {
31   base::TestPendingTask task;
32 
33   // Just a basic check that ToString has *something* in it.
34   EXPECT_THAT(task.ToString(), ::testing::StartsWith("TestPendingTask("));
35 }
36 
TEST(TestPendingTaskTest,GTestPrettyPrint)37 TEST(TestPendingTaskTest, GTestPrettyPrint) {
38   base::TestPendingTask task;
39 
40   // Check that gtest is calling the TestPendingTask's PrintTo method.
41   EXPECT_THAT(::testing::PrintToString(task),
42               ::testing::StartsWith("TestPendingTask("));
43 
44   // Check that pretty printing works with the gtest iostreams operator.
45   EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << task, "TestPendingTask(");
46 }
47 
TEST(TestPendingTaskTest,ShouldRunBefore)48 TEST(TestPendingTaskTest, ShouldRunBefore) {
49   base::TestPendingTask task_first;
50   task_first.delay = base::Milliseconds(1);
51   base::TestPendingTask task_after;
52   task_after.delay = base::Milliseconds(2);
53 
54   EXPECT_FALSE(task_after.ShouldRunBefore(task_first))
55       << task_after << ".ShouldRunBefore(" << task_first << ")\n";
56   EXPECT_TRUE(task_first.ShouldRunBefore(task_after))
57       << task_first << ".ShouldRunBefore(" << task_after << ")\n";
58 }
59 
60 }  // namespace base
61