chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 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 | |
Oystein Eftevaag | 6410f1a | 2018-03-01 18:06:30 | [diff] [blame] | 5 | #include "services/tracing/agent_registry.h" |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 12 | #include "base/run_loop.h" |
Oystein Eftevaag | 6410f1a | 2018-03-01 18:06:30 | [diff] [blame] | 13 | #include "services/service_manager/public/cpp/service_context_ref.h" |
| 14 | #include "services/tracing/public/mojom/tracing.mojom.h" |
| 15 | #include "services/tracing/test_util.h" |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace tracing { |
| 19 | |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 20 | class AgentRegistryTest : public testing::Test { |
| 21 | public: |
| 22 | void SetUp() override { |
| 23 | message_loop_.reset(new base::MessageLoop()); |
Oystein Eftevaag | ec68fcf2 | 2018-07-20 00:35:40 | [diff] [blame] | 24 | registry_ = std::make_unique<AgentRegistry>(); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void TearDown() override { |
| 28 | registry_.reset(); |
| 29 | message_loop_.reset(); |
| 30 | } |
| 31 | |
| 32 | void RegisterAgent(mojom::AgentPtr agent, |
| 33 | const std::string& label, |
Ehsan Chiniforooshan | ae5a594 | 2018-12-10 17:21:58 | [diff] [blame] | 34 | mojom::TraceDataType type) { |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 35 | registry_->RegisterAgent(std::move(agent), label, type, |
Alexei Filippov | c182b7c | 2018-06-28 21:44:53 | [diff] [blame] | 36 | base::kNullProcessId); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void RegisterAgent(mojom::AgentPtr agent) { |
| 40 | registry_->RegisterAgent(std::move(agent), "label", |
Ehsan Chiniforooshan | ae5a594 | 2018-12-10 17:21:58 | [diff] [blame] | 41 | mojom::TraceDataType::ARRAY, base::kNullProcessId); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | std::unique_ptr<AgentRegistry> registry_; |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | std::unique_ptr<base::MessageLoop> message_loop_; |
| 48 | }; |
| 49 | |
| 50 | TEST_F(AgentRegistryTest, RegisterAgent) { |
| 51 | MockAgent agent1; |
| 52 | RegisterAgent(agent1.CreateAgentPtr(), "TraceEvent", |
Ehsan Chiniforooshan | ae5a594 | 2018-12-10 17:21:58 | [diff] [blame] | 53 | mojom::TraceDataType::ARRAY); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 54 | size_t num_agents = 0; |
| 55 | registry_->ForAllAgents([&num_agents](AgentRegistry::AgentEntry* entry) { |
| 56 | num_agents++; |
| 57 | EXPECT_EQ("TraceEvent", entry->label()); |
| 58 | EXPECT_EQ(mojom::TraceDataType::ARRAY, entry->type()); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 59 | }); |
| 60 | EXPECT_EQ(1u, num_agents); |
| 61 | |
| 62 | MockAgent agent2; |
Ehsan Chiniforooshan | ae5a594 | 2018-12-10 17:21:58 | [diff] [blame] | 63 | RegisterAgent(agent2.CreateAgentPtr(), "Power", mojom::TraceDataType::STRING); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 64 | num_agents = 0; |
| 65 | registry_->ForAllAgents([&num_agents](AgentRegistry::AgentEntry* entry) { |
| 66 | num_agents++; |
| 67 | // Properties of |agent1| is already verified. |
| 68 | if (entry->label() == "TraceEvent") |
| 69 | return; |
| 70 | EXPECT_EQ("Power", entry->label()); |
| 71 | EXPECT_EQ(mojom::TraceDataType::STRING, entry->type()); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 72 | }); |
| 73 | EXPECT_EQ(2u, num_agents); |
| 74 | } |
| 75 | |
| 76 | TEST_F(AgentRegistryTest, UnregisterAgent) { |
| 77 | base::RunLoop run_loop; |
| 78 | MockAgent agent1; |
| 79 | RegisterAgent(agent1.CreateAgentPtr()); |
| 80 | { |
| 81 | MockAgent agent2; |
| 82 | RegisterAgent(agent2.CreateAgentPtr()); |
| 83 | size_t num_agents = 0; |
| 84 | registry_->ForAllAgents( |
| 85 | [&num_agents](AgentRegistry::AgentEntry* entry) { num_agents++; }); |
| 86 | EXPECT_EQ(2u, num_agents); |
| 87 | } |
| 88 | run_loop.RunUntilIdle(); |
| 89 | |
| 90 | // |agent2| is not alive anymore. |
| 91 | size_t num_agents = 0; |
| 92 | registry_->ForAllAgents( |
| 93 | [&num_agents](AgentRegistry::AgentEntry* entry) { num_agents++; }); |
| 94 | EXPECT_EQ(1u, num_agents); |
| 95 | } |
| 96 | |
| 97 | TEST_F(AgentRegistryTest, AgentInitialization) { |
| 98 | size_t num_calls = 0; |
| 99 | MockAgent agent1; |
| 100 | RegisterAgent(agent1.CreateAgentPtr()); |
Ehsan Chiniforooshan | f527d2a | 2018-12-14 20:40:45 | [diff] [blame] | 101 | size_t num_initialized_agents = registry_->SetAgentInitializationCallback( |
| 102 | base::BindRepeating( |
| 103 | [](size_t* num_calls, tracing::AgentRegistry::AgentEntry* entry) { |
| 104 | (*num_calls)++; |
| 105 | }, |
| 106 | base::Unretained(&num_calls)), |
| 107 | false); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 108 | // Since an agent was already registered, the callback should be run once. |
Ehsan Chiniforooshan | f527d2a | 2018-12-14 20:40:45 | [diff] [blame] | 109 | EXPECT_EQ(1u, num_initialized_agents); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 110 | EXPECT_EQ(1u, num_calls); |
| 111 | |
| 112 | // The callback should be run on future agents, too. |
| 113 | MockAgent agent2; |
| 114 | RegisterAgent(agent2.CreateAgentPtr()); |
| 115 | EXPECT_EQ(2u, num_calls); |
chiniforooshan | 536bfe1 | 2017-05-26 21:30:25 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | } // namespace tracing |