| Index: services/resource_coordinator/public/cpp/tracing/trace_event_agent_unittest.cc
|
| diff --git a/services/resource_coordinator/public/cpp/tracing/trace_event_agent_unittest.cc b/services/resource_coordinator/public/cpp/tracing/trace_event_agent_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8c6daf2f97eba7d9e033b1ace63aa718560f32d8
|
| --- /dev/null
|
| +++ b/services/resource_coordinator/public/cpp/tracing/trace_event_agent_unittest.cc
|
| @@ -0,0 +1,193 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "services/resource_coordinator/public/cpp/tracing/trace_event_agent.h"
|
| +#include "base/bind.h"
|
| +#include "base/callback_forward.h"
|
| +#include "base/json/json_reader.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/trace_event/trace_config.h"
|
| +#include "base/trace_event/trace_event.h"
|
| +#include "base/trace_event/trace_log.h"
|
| +#include "base/values.h"
|
| +#include "services/resource_coordinator/public/interfaces/tracing/tracing.mojom.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace resource_coordinator {
|
| +namespace tracing {
|
| +
|
| +namespace {
|
| +const char kTestCategory[] = "TraceEventAgentTestCategory";
|
| +const char kTestMetadataKey[] = "TraceEventAgentTestMetadata";
|
| +} // namespace
|
| +
|
| +class MockRecorder : public mojom::Recorder {
|
| + public:
|
| + MockRecorder(mojom::RecorderRequest request)
|
| + : binding_(this, std::move(request)) {
|
| + binding_.set_connection_error_handler(base::BindRepeating(
|
| + &MockRecorder::OnConnectionError, base::Unretained(this)));
|
| + }
|
| +
|
| + std::string categories() const { return categories_; }
|
| + std::string events() const { return events_; }
|
| + std::string metadata() const { return metadata_; }
|
| + void set_quit_closure(base::Closure quit_closure) {
|
| + quit_closure_ = quit_closure;
|
| + }
|
| +
|
| + private:
|
| + void Append(std::string* dest, const std::string& chunk) {
|
| + if (chunk.empty())
|
| + return;
|
| + if (!dest->empty())
|
| + dest->push_back(',');
|
| + dest->append(chunk);
|
| + }
|
| +
|
| + void AddCategories(const std::string& categories) override {
|
| + Append(&categories_, categories);
|
| + }
|
| +
|
| + void AddChunk(const std::string& chunk) override {
|
| + const auto value = base::JSONReader::Read("[" + chunk + "]");
|
| + base::ListValue* events = nullptr;
|
| + EXPECT_TRUE(value->GetAsList(&events));
|
| + for (auto& event : *events) {
|
| + base::DictionaryValue* event_dict = nullptr;
|
| + EXPECT_TRUE(event.GetAsDictionary(&event_dict));
|
| + std::string category;
|
| + EXPECT_TRUE(event_dict->GetString("cat", &category));
|
| + if (category != kTestCategory)
|
| + continue;
|
| + std::string name;
|
| + EXPECT_TRUE(event_dict->GetString("name", &name));
|
| + Append(&events_, name);
|
| + }
|
| + }
|
| +
|
| + void AddMetadata(std::unique_ptr<base::DictionaryValue> metadata) override {
|
| + LOG(ERROR) << "meta call";
|
| + base::DictionaryValue* dict = nullptr;
|
| + EXPECT_TRUE(metadata->GetAsDictionary(&dict));
|
| + std::string value;
|
| + if (dict->GetString(kTestMetadataKey, &value))
|
| + Append(&metadata_, value);
|
| + }
|
| +
|
| + void OnConnectionError() {
|
| + if (quit_closure_)
|
| + quit_closure_.Run();
|
| + }
|
| +
|
| + mojo::Binding<mojom::Recorder> binding_;
|
| + std::string categories_;
|
| + std::string events_;
|
| + std::string metadata_;
|
| + base::Closure quit_closure_;
|
| +};
|
| +
|
| +class TraceEventAgentTest : public testing::Test {
|
| + public:
|
| + void SetUp() override {
|
| + message_loop_.reset(new base::MessageLoop());
|
| + agent_.reset(new TraceEventAgent(nullptr));
|
| + }
|
| +
|
| + void TearDown() override {
|
| + recorder_.reset();
|
| + agent_.reset();
|
| + message_loop_.reset();
|
| + }
|
| +
|
| + void StartTracing(const std::string& categories,
|
| + bool report_categories_only,
|
| + base::Closure quit_closure) {
|
| + mojom::RecorderPtr recorder_ptr;
|
| + recorder_.reset(new MockRecorder(MakeRequest(&recorder_ptr)));
|
| + recorder_->set_quit_closure(quit_closure);
|
| + agent_->StartTracing(
|
| + base::trace_event::TraceConfig(categories, "").ToString(),
|
| + std::move(recorder_ptr), report_categories_only,
|
| + base::BindRepeating([] {}));
|
| + }
|
| +
|
| + void StopAndFlush() { agent_->StopAndFlush(); }
|
| +
|
| + void AddMetadataGenerator(TraceEventAgent::MetadataGenerator generator) {
|
| + agent_->AddMetadataGenerator(generator);
|
| + }
|
| +
|
| + MockRecorder* recorder() const { return recorder_.get(); }
|
| +
|
| + private:
|
| + std::unique_ptr<base::MessageLoop> message_loop_;
|
| + std::unique_ptr<TraceEventAgent> agent_;
|
| + std::unique_ptr<MockRecorder> recorder_;
|
| +};
|
| +
|
| +TEST_F(TraceEventAgentTest, StartTracing) {
|
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| + base::RunLoop run_loop;
|
| + StartTracing("*", true, run_loop.QuitClosure());
|
| + EXPECT_TRUE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| + StopAndFlush();
|
| + run_loop.Run();
|
| +}
|
| +
|
| +TEST_F(TraceEventAgentTest, StopAndFlushEvents) {
|
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| + base::RunLoop run_loop;
|
| + StartTracing(kTestCategory, false, run_loop.QuitClosure());
|
| + TRACE_EVENT_INSTANT0(kTestCategory, "event1", TRACE_EVENT_SCOPE_THREAD);
|
| + TRACE_EVENT_INSTANT0(kTestCategory, "event2", TRACE_EVENT_SCOPE_THREAD);
|
| + StopAndFlush();
|
| + run_loop.Run();
|
| +
|
| + auto* mock_recorder = recorder();
|
| + EXPECT_EQ("event1,event2", mock_recorder->events());
|
| + EXPECT_EQ("", mock_recorder->categories());
|
| + EXPECT_EQ("", mock_recorder->metadata());
|
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| +}
|
| +
|
| +TEST_F(TraceEventAgentTest, StopAndFlushCategoriesOnly) {
|
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| + base::RunLoop run_loop;
|
| + StartTracing("*", true, run_loop.QuitClosure());
|
| + TRACE_EVENT_INSTANT0(kTestCategory, "event1", TRACE_EVENT_SCOPE_THREAD);
|
| + StopAndFlush();
|
| + run_loop.Run();
|
| +
|
| + auto* mock_recorder = recorder();
|
| + EXPECT_EQ("", mock_recorder->events());
|
| + EXPECT_TRUE(mock_recorder->categories().rfind(kTestCategory) !=
|
| + std::string::npos);
|
| + EXPECT_EQ("", mock_recorder->metadata());
|
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| +}
|
| +
|
| +TEST_F(TraceEventAgentTest, StopAndFlushMetadata) {
|
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| + base::RunLoop run_loop;
|
| + AddMetadataGenerator(base::BindRepeating([] {
|
| + std::unique_ptr<base::DictionaryValue> metadata_dict(
|
| + new base::DictionaryValue());
|
| + metadata_dict->SetString(kTestMetadataKey, "test metadata");
|
| + return metadata_dict;
|
| + }));
|
| + StartTracing(kTestCategory, false, run_loop.QuitClosure());
|
| + StopAndFlush();
|
| + run_loop.Run();
|
| +
|
| + auto* mock_recorder = recorder();
|
| + EXPECT_EQ("", mock_recorder->events());
|
| + EXPECT_EQ("", mock_recorder->categories());
|
| + EXPECT_EQ("test metadata", mock_recorder->metadata());
|
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled());
|
| +}
|
| +} // namespace tracing
|
| +} // namespace resource_coordinator
|
|
|