1 // Copyright 2015 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 "services/tracing/public/cpp/background_tracing/background_tracing_agent_impl.h"
6
7 #include "base/run_loop.h"
8
9 #include "base/metrics/histogram_macros.h"
10 #include "base/test/task_environment.h"
11 #include "mojo/public/cpp/bindings/unique_receiver_set.h"
12 #include "services/tracing/public/cpp/background_tracing/background_tracing_agent_provider_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace tracing {
16
17 class BackgroundTracingAgentClientRecorder
18 : public mojom::BackgroundTracingAgentClient {
19 public:
OnInitialized()20 void OnInitialized() override { ++on_initialized_count_; }
21
OnTriggerBackgroundTrace(const std::string & histogram_name)22 void OnTriggerBackgroundTrace(const std::string& histogram_name) override {
23 ++on_trigger_background_trace_count_;
24 on_trigger_background_trace_histogram_name_ = histogram_name;
25 }
26
OnAbortBackgroundTrace()27 void OnAbortBackgroundTrace() override { ++on_abort_background_trace_count_; }
28
on_initialized_count() const29 int on_initialized_count() const { return on_initialized_count_; }
on_trigger_background_trace_count() const30 int on_trigger_background_trace_count() const {
31 return on_trigger_background_trace_count_;
32 }
on_abort_background_trace_count() const33 int on_abort_background_trace_count() const {
34 return on_abort_background_trace_count_;
35 }
36
on_trigger_background_trace_histogram_name() const37 const std::string& on_trigger_background_trace_histogram_name() const {
38 return on_trigger_background_trace_histogram_name_;
39 }
40
41 private:
42 int on_initialized_count_ = 0;
43 int on_trigger_background_trace_count_ = 0;
44 int on_abort_background_trace_count_ = 0;
45 std::string on_trigger_background_trace_histogram_name_;
46 };
47
48 class BackgroundTracingAgentImplTest : public testing::Test {
49 public:
BackgroundTracingAgentImplTest()50 BackgroundTracingAgentImplTest() {
51 provider_set_.Add(
52 std::make_unique<tracing::BackgroundTracingAgentProviderImpl>(),
53 provider_.BindNewPipeAndPassReceiver());
54
55 auto recorder = std::make_unique<BackgroundTracingAgentClientRecorder>();
56 recorder_ = recorder.get();
57
58 mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentClient> client;
59 client_set_.Add(std::move(recorder),
60 client.InitWithNewPipeAndPassReceiver());
61
62 provider_->Create(0, std::move(client),
63 agent_.BindNewPipeAndPassReceiver());
64 }
65
agent()66 tracing::mojom::BackgroundTracingAgent* agent() { return agent_.get(); }
67
recorder() const68 BackgroundTracingAgentClientRecorder* recorder() const { return recorder_; }
69
70 private:
71 base::test::TaskEnvironment task_environment_;
72 mojo::Remote<tracing::mojom::BackgroundTracingAgentProvider> provider_;
73 mojo::Remote<tracing::mojom::BackgroundTracingAgent> agent_;
74 mojo::UniqueReceiverSet<tracing::mojom::BackgroundTracingAgentProvider>
75 provider_set_;
76 mojo::UniqueReceiverSet<tracing::mojom::BackgroundTracingAgentClient>
77 client_set_;
78 BackgroundTracingAgentClientRecorder* recorder_ = nullptr;
79 };
80
TEST_F(BackgroundTracingAgentImplTest,TestInitialize)81 TEST_F(BackgroundTracingAgentImplTest, TestInitialize) {
82 base::RunLoop().RunUntilIdle();
83 EXPECT_EQ(1, recorder()->on_initialized_count());
84 }
85
TEST_F(BackgroundTracingAgentImplTest,TestHistogramDoesNotTrigger)86 TEST_F(BackgroundTracingAgentImplTest, TestHistogramDoesNotTrigger) {
87 LOCAL_HISTOGRAM_COUNTS("foo1", 10);
88
89 agent()->SetUMACallback("foo1", 20000, 25000, true);
90
91 base::RunLoop().RunUntilIdle();
92
93 EXPECT_EQ(1, recorder()->on_initialized_count());
94 EXPECT_EQ(0, recorder()->on_trigger_background_trace_count());
95 EXPECT_EQ(0, recorder()->on_abort_background_trace_count());
96 }
97
TEST_F(BackgroundTracingAgentImplTest,TestHistogramTriggers)98 TEST_F(BackgroundTracingAgentImplTest, TestHistogramTriggers) {
99 LOCAL_HISTOGRAM_COUNTS("foo2", 2);
100
101 agent()->SetUMACallback("foo2", 1, 3, true);
102
103 base::RunLoop().RunUntilIdle();
104
105 EXPECT_EQ(1, recorder()->on_initialized_count());
106 EXPECT_EQ(1, recorder()->on_trigger_background_trace_count());
107 EXPECT_EQ(0, recorder()->on_abort_background_trace_count());
108 EXPECT_EQ("foo2", recorder()->on_trigger_background_trace_histogram_name());
109 }
110
TEST_F(BackgroundTracingAgentImplTest,TestHistogramAborts)111 TEST_F(BackgroundTracingAgentImplTest, TestHistogramAborts) {
112 LOCAL_HISTOGRAM_COUNTS("foo3", 10);
113
114 agent()->SetUMACallback("foo3", 1, 3, false);
115
116 base::RunLoop().RunUntilIdle();
117
118 EXPECT_EQ(1, recorder()->on_initialized_count());
119 EXPECT_EQ(0, recorder()->on_trigger_background_trace_count());
120 EXPECT_EQ(1, recorder()->on_abort_background_trace_count());
121 }
122
123 } // namespace tracing
124