| OLD | NEW |
| (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/tracing/agent_registry.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "services/resource_coordinator/public/interfaces/tracing/tracing.mojom.
h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace tracing { |
| 18 |
| 19 class MockAgent : public mojom::Agent { |
| 20 public: |
| 21 MockAgent() : binding_(this) {} |
| 22 |
| 23 mojom::AgentPtr CreateAgentPtr() { |
| 24 return binding_.CreateInterfacePtrAndBind(); |
| 25 } |
| 26 |
| 27 private: |
| 28 // mojom::Agent |
| 29 void StartTracing(const std::string& config, |
| 30 mojom::RecorderPtr recorder, |
| 31 const StartTracingCallback& callback) override {} |
| 32 void StopAndFlush() override {} |
| 33 void RequestClockSyncMarker( |
| 34 const std::string& sync_id, |
| 35 const RequestClockSyncMarkerCallback& callback) override {} |
| 36 void RequestBufferStatus( |
| 37 const RequestBufferStatusCallback& callback) override {} |
| 38 void GetCategories(const GetCategoriesCallback& callback) override {} |
| 39 |
| 40 mojo::Binding<mojom::Agent> binding_; |
| 41 }; |
| 42 |
| 43 class AgentRegistryTest : public testing::Test { |
| 44 public: |
| 45 void SetUp() override { |
| 46 message_loop_.reset(new base::MessageLoop()); |
| 47 registry_.reset(new AgentRegistry()); |
| 48 } |
| 49 |
| 50 void TearDown() override { |
| 51 registry_.reset(); |
| 52 message_loop_.reset(); |
| 53 } |
| 54 |
| 55 void RegisterAgent(mojom::AgentPtr agent, |
| 56 const std::string& label, |
| 57 mojom::TraceDataType type, |
| 58 bool supports_explicit_clock_sync) { |
| 59 registry_->RegisterAgent(std::move(agent), label, type, |
| 60 supports_explicit_clock_sync); |
| 61 } |
| 62 |
| 63 void RegisterAgent(mojom::AgentPtr agent) { |
| 64 registry_->RegisterAgent(std::move(agent), "label", |
| 65 mojom::TraceDataType::ARRAY, false); |
| 66 } |
| 67 |
| 68 std::unique_ptr<AgentRegistry> registry_; |
| 69 |
| 70 private: |
| 71 std::unique_ptr<base::MessageLoop> message_loop_; |
| 72 }; |
| 73 |
| 74 TEST_F(AgentRegistryTest, RegisterAgent) { |
| 75 MockAgent agent1; |
| 76 RegisterAgent(agent1.CreateAgentPtr(), "TraceEvent", |
| 77 mojom::TraceDataType::ARRAY, false); |
| 78 size_t num_agents = 0; |
| 79 registry_->ForAllAgents([&num_agents](AgentRegistry::AgentEntry* entry) { |
| 80 num_agents++; |
| 81 EXPECT_EQ("TraceEvent", entry->label()); |
| 82 EXPECT_EQ(mojom::TraceDataType::ARRAY, entry->type()); |
| 83 EXPECT_FALSE(entry->supports_explicit_clock_sync()); |
| 84 }); |
| 85 EXPECT_EQ(1u, num_agents); |
| 86 |
| 87 MockAgent agent2; |
| 88 RegisterAgent(agent2.CreateAgentPtr(), "Power", mojom::TraceDataType::STRING, |
| 89 true); |
| 90 num_agents = 0; |
| 91 registry_->ForAllAgents([&num_agents](AgentRegistry::AgentEntry* entry) { |
| 92 num_agents++; |
| 93 // Properties of |agent1| is already verified. |
| 94 if (entry->label() == "TraceEvent") |
| 95 return; |
| 96 EXPECT_EQ("Power", entry->label()); |
| 97 EXPECT_EQ(mojom::TraceDataType::STRING, entry->type()); |
| 98 EXPECT_TRUE(entry->supports_explicit_clock_sync()); |
| 99 }); |
| 100 EXPECT_EQ(2u, num_agents); |
| 101 } |
| 102 |
| 103 TEST_F(AgentRegistryTest, UnregisterAgent) { |
| 104 base::RunLoop run_loop; |
| 105 MockAgent agent1; |
| 106 RegisterAgent(agent1.CreateAgentPtr()); |
| 107 { |
| 108 MockAgent agent2; |
| 109 RegisterAgent(agent2.CreateAgentPtr()); |
| 110 size_t num_agents = 0; |
| 111 registry_->ForAllAgents( |
| 112 [&num_agents](AgentRegistry::AgentEntry* entry) { num_agents++; }); |
| 113 EXPECT_EQ(2u, num_agents); |
| 114 } |
| 115 run_loop.RunUntilIdle(); |
| 116 |
| 117 // |agent2| is not alive anymore. |
| 118 size_t num_agents = 0; |
| 119 registry_->ForAllAgents( |
| 120 [&num_agents](AgentRegistry::AgentEntry* entry) { num_agents++; }); |
| 121 EXPECT_EQ(1u, num_agents); |
| 122 } |
| 123 |
| 124 TEST_F(AgentRegistryTest, AgentInitialization) { |
| 125 size_t num_calls = 0; |
| 126 MockAgent agent1; |
| 127 RegisterAgent(agent1.CreateAgentPtr()); |
| 128 registry_->SetAgentInitializationCallback(base::BindRepeating( |
| 129 [](size_t* num_calls, tracing::AgentRegistry::AgentEntry* entry) { |
| 130 (*num_calls)++; |
| 131 }, |
| 132 base::Unretained(&num_calls))); |
| 133 // Since an agent was already registered, the callback should be run once. |
| 134 EXPECT_EQ(1u, num_calls); |
| 135 |
| 136 // The callback should be run on future agents, too. |
| 137 MockAgent agent2; |
| 138 RegisterAgent(agent2.CreateAgentPtr()); |
| 139 EXPECT_EQ(2u, num_calls); |
| 140 |
| 141 // The callback should not be run on future agents if it is removed. |
| 142 registry_->RemoveAgentInitializationCallback(); |
| 143 MockAgent agent3; |
| 144 RegisterAgent(agent3.CreateAgentPtr()); |
| 145 EXPECT_EQ(2u, num_calls); |
| 146 } |
| 147 |
| 148 } // namespace tracing |
| OLD | NEW |