Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1661)

Side by Side Diff: services/resource_coordinator/public/cpp/tracing/chrome_agent_unittest.cc

Issue 2878533003: tracing: the client lib of the tracing service (Closed)
Patch Set: review Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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/resource_coordinator/public/cpp/tracing/chrome_agent.h"
6 #include "base/bind.h"
7 #include "base/callback_forward.h"
8 #include "base/json/json_reader.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/trace_event/trace_config.h"
13 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_log.h"
15 #include "base/values.h"
16 #include "services/resource_coordinator/public/interfaces/tracing/tracing.mojom. h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace tracing {
20
21 namespace {
22 const char kTestCategory[] = "ChromeAgentTestCategory";
23 const char kTestMetadataKey[] = "ChromeAgentTestMetadata";
24 } // namespace
25
26 class MockRecorder : public mojom::Recorder {
27 public:
28 MockRecorder(mojom::RecorderRequest request)
29 : binding_(this, std::move(request)) {
30 binding_.set_connection_error_handler(base::BindRepeating(
31 &MockRecorder::OnConnectionError, base::Unretained(this)));
32 }
33
34 std::string events() const { return events_; }
35 std::string metadata() const { return metadata_; }
36 void set_quit_closure(base::Closure quit_closure) {
37 quit_closure_ = quit_closure;
38 }
39
40 private:
41 void Append(std::string* dest, const std::string& chunk) {
42 if (chunk.empty())
43 return;
44 if (!dest->empty())
45 dest->push_back(',');
46 dest->append(chunk);
47 }
48
49 void AddChunk(const std::string& chunk) override {
50 const auto value = base::JSONReader::Read("[" + chunk + "]");
51 base::ListValue* events = nullptr;
52 EXPECT_TRUE(value->GetAsList(&events));
53 for (auto& event : *events) {
54 base::DictionaryValue* event_dict = nullptr;
55 EXPECT_TRUE(event.GetAsDictionary(&event_dict));
56 std::string category;
57 EXPECT_TRUE(event_dict->GetString("cat", &category));
Primiano Tucci (use gerrit) 2017/05/16 04:45:12 instead of reparsing all this and depending on the
chiniforooshan 2017/05/16 15:14:45 Done.
58 if (category != kTestCategory)
59 continue;
60 std::string name;
61 EXPECT_TRUE(event_dict->GetString("name", &name));
62 Append(&events_, name);
63 }
64 }
65
66 void AddMetadata(std::unique_ptr<base::DictionaryValue> metadata) override {
67 LOG(ERROR) << "meta call";
Primiano Tucci (use gerrit) 2017/05/16 04:45:12 looks like a leftover from local debugging :)
chiniforooshan 2017/05/16 15:14:45 Done.
68 base::DictionaryValue* dict = nullptr;
69 EXPECT_TRUE(metadata->GetAsDictionary(&dict));
70 std::string value;
71 if (dict->GetString(kTestMetadataKey, &value))
72 Append(&metadata_, value);
73 }
74
75 void OnConnectionError() {
76 if (quit_closure_)
77 quit_closure_.Run();
78 }
79
80 mojo::Binding<mojom::Recorder> binding_;
81 std::string events_;
82 std::string metadata_;
83 base::Closure quit_closure_;
84 };
85
86 class ChromeAgentTest : public testing::Test {
87 public:
88 void SetUp() override {
89 message_loop_.reset(new base::MessageLoop());
90 agent_.reset(new ChromeAgent(nullptr));
91 }
92
93 void TearDown() override {
94 recorder_.reset();
95 agent_.reset();
96 message_loop_.reset();
97 }
98
99 void StartTracing(const std::string& categories, base::Closure quit_closure) {
100 mojom::RecorderPtr recorder_ptr;
101 recorder_.reset(new MockRecorder(MakeRequest(&recorder_ptr)));
102 recorder_->set_quit_closure(quit_closure);
103 agent_->StartTracing(
104 base::trace_event::TraceConfig(categories, "").ToString(),
105 std::move(recorder_ptr), base::BindRepeating([] {}));
106 }
107
108 void StopAndFlush() { agent_->StopAndFlush(); }
109
110 void AddMetadataGeneratorFunction(
111 ChromeAgent::MetadataGeneratorFunction generator) {
112 agent_->AddMetadataGeneratorFunction(generator);
113 }
114
115 void GetCategories(const std::string& expected_category,
116 base::Closure quit_closure) {
117 agent_->GetCategories(base::BindRepeating(
118 &ChromeAgentTest::OnGetCategoriesReply, base::Unretained(this),
119 expected_category, quit_closure));
120 }
121
122 void OnGetCategoriesReply(const std::string& expected_category,
123 base::Closure quit_closure,
124 const std::string& categories) {
125 EXPECT_FALSE(categories.rfind(expected_category) == std::string::npos);
126 quit_closure.Run();
127 }
128
129 MockRecorder* recorder() const { return recorder_.get(); }
130
131 private:
132 std::unique_ptr<base::MessageLoop> message_loop_;
133 std::unique_ptr<ChromeAgent> agent_;
134 std::unique_ptr<MockRecorder> recorder_;
135 };
136
137 TEST_F(ChromeAgentTest, StartTracing) {
138 EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
139 base::RunLoop run_loop;
140 StartTracing("*", run_loop.QuitClosure());
141 EXPECT_TRUE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
142 StopAndFlush();
143 run_loop.Run();
144 }
145
146 TEST_F(ChromeAgentTest, StopAndFlushEvents) {
147 EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
148 base::RunLoop run_loop;
149 StartTracing(kTestCategory, run_loop.QuitClosure());
150 TRACE_EVENT_INSTANT0(kTestCategory, "event1", TRACE_EVENT_SCOPE_THREAD);
151 TRACE_EVENT_INSTANT0(kTestCategory, "event2", TRACE_EVENT_SCOPE_THREAD);
152 StopAndFlush();
153 run_loop.Run();
154
155 auto* mock_recorder = recorder();
156 EXPECT_EQ("event1,event2", mock_recorder->events());
157 EXPECT_EQ("", mock_recorder->metadata());
158 EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
159 }
160
161 TEST_F(ChromeAgentTest, GetCategories) {
162 base::RunLoop run_loop;
163 TRACE_EVENT_INSTANT0(kTestCategory, "event1", TRACE_EVENT_SCOPE_THREAD);
164 GetCategories(kTestCategory, run_loop.QuitClosure());
165 run_loop.Run();
166 }
167
168 TEST_F(ChromeAgentTest, StopAndFlushMetadata) {
169 EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
170 base::RunLoop run_loop;
171 AddMetadataGeneratorFunction(base::BindRepeating([] {
172 std::unique_ptr<base::DictionaryValue> metadata_dict(
173 new base::DictionaryValue());
174 metadata_dict->SetString(kTestMetadataKey, "test metadata");
175 return metadata_dict;
176 }));
177 StartTracing(kTestCategory, run_loop.QuitClosure());
178 StopAndFlush();
179 run_loop.Run();
180
181 auto* mock_recorder = recorder();
182 EXPECT_EQ("", mock_recorder->events());
183 EXPECT_EQ("test metadata", mock_recorder->metadata());
184 EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
185 }
186 } // namespace tracing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698