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