Chromium Code Reviews| Index: services/resource_coordinator/public/cpp/tracing/chrome_agent_unittest.cc |
| diff --git a/services/resource_coordinator/public/cpp/tracing/chrome_agent_unittest.cc b/services/resource_coordinator/public/cpp/tracing/chrome_agent_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d8c8d7080c13b752ffab7f577a0c68e7bb65243c |
| --- /dev/null |
| +++ b/services/resource_coordinator/public/cpp/tracing/chrome_agent_unittest.cc |
| @@ -0,0 +1,186 @@ |
| +// 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/chrome_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 tracing { |
| + |
| +namespace { |
| +const char kTestCategory[] = "ChromeAgentTestCategory"; |
| +const char kTestMetadataKey[] = "ChromeAgentTestMetadata"; |
| +} // 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 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 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)); |
|
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.
|
| + 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"; |
|
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.
|
| + 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 events_; |
| + std::string metadata_; |
| + base::Closure quit_closure_; |
| +}; |
| + |
| +class ChromeAgentTest : public testing::Test { |
| + public: |
| + void SetUp() override { |
| + message_loop_.reset(new base::MessageLoop()); |
| + agent_.reset(new ChromeAgent(nullptr)); |
| + } |
| + |
| + void TearDown() override { |
| + recorder_.reset(); |
| + agent_.reset(); |
| + message_loop_.reset(); |
| + } |
| + |
| + void StartTracing(const std::string& categories, 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), base::BindRepeating([] {})); |
| + } |
| + |
| + void StopAndFlush() { agent_->StopAndFlush(); } |
| + |
| + void AddMetadataGeneratorFunction( |
| + ChromeAgent::MetadataGeneratorFunction generator) { |
| + agent_->AddMetadataGeneratorFunction(generator); |
| + } |
| + |
| + void GetCategories(const std::string& expected_category, |
| + base::Closure quit_closure) { |
| + agent_->GetCategories(base::BindRepeating( |
| + &ChromeAgentTest::OnGetCategoriesReply, base::Unretained(this), |
| + expected_category, quit_closure)); |
| + } |
| + |
| + void OnGetCategoriesReply(const std::string& expected_category, |
| + base::Closure quit_closure, |
| + const std::string& categories) { |
| + EXPECT_FALSE(categories.rfind(expected_category) == std::string::npos); |
| + quit_closure.Run(); |
| + } |
| + |
| + MockRecorder* recorder() const { return recorder_.get(); } |
| + |
| + private: |
| + std::unique_ptr<base::MessageLoop> message_loop_; |
| + std::unique_ptr<ChromeAgent> agent_; |
| + std::unique_ptr<MockRecorder> recorder_; |
| +}; |
| + |
| +TEST_F(ChromeAgentTest, StartTracing) { |
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled()); |
| + base::RunLoop run_loop; |
| + StartTracing("*", run_loop.QuitClosure()); |
| + EXPECT_TRUE(base::trace_event::TraceLog::GetInstance()->IsEnabled()); |
| + StopAndFlush(); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ChromeAgentTest, StopAndFlushEvents) { |
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled()); |
| + base::RunLoop run_loop; |
| + StartTracing(kTestCategory, 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->metadata()); |
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled()); |
| +} |
| + |
| +TEST_F(ChromeAgentTest, GetCategories) { |
| + base::RunLoop run_loop; |
| + TRACE_EVENT_INSTANT0(kTestCategory, "event1", TRACE_EVENT_SCOPE_THREAD); |
| + GetCategories(kTestCategory, run_loop.QuitClosure()); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ChromeAgentTest, StopAndFlushMetadata) { |
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled()); |
| + base::RunLoop run_loop; |
| + AddMetadataGeneratorFunction(base::BindRepeating([] { |
| + std::unique_ptr<base::DictionaryValue> metadata_dict( |
| + new base::DictionaryValue()); |
| + metadata_dict->SetString(kTestMetadataKey, "test metadata"); |
| + return metadata_dict; |
| + })); |
| + StartTracing(kTestCategory, run_loop.QuitClosure()); |
| + StopAndFlush(); |
| + run_loop.Run(); |
| + |
| + auto* mock_recorder = recorder(); |
| + EXPECT_EQ("", mock_recorder->events()); |
| + EXPECT_EQ("test metadata", mock_recorder->metadata()); |
| + EXPECT_FALSE(base::trace_event::TraceLog::GetInstance()->IsEnabled()); |
| +} |
| +} // namespace tracing |