[go: nahoru, domu]

blob: 43822a1a3b295f5783e23e2072dd3afb059c04e1 [file] [log] [blame]
chiniforooshan536bfe12017-05-26 21:30:251// 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 Eftevaag6410f1a2018-03-01 18:06:305#include "services/tracing/agent_registry.h"
chiniforooshan536bfe12017-05-26 21:30:256
7#include <memory>
8#include <string>
9#include <utility>
10
Alexander Timin4f9c35c2018-11-01 20:15:2011#include "base/message_loop/message_loop.h"
chiniforooshan536bfe12017-05-26 21:30:2512#include "base/run_loop.h"
Oystein Eftevaag6410f1a2018-03-01 18:06:3013#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"
chiniforooshan536bfe12017-05-26 21:30:2516#include "testing/gtest/include/gtest/gtest.h"
17
18namespace tracing {
19
chiniforooshan536bfe12017-05-26 21:30:2520class AgentRegistryTest : public testing::Test {
21 public:
22 void SetUp() override {
23 message_loop_.reset(new base::MessageLoop());
Oystein Eftevaagec68fcf22018-07-20 00:35:4024 registry_ = std::make_unique<AgentRegistry>();
chiniforooshan536bfe12017-05-26 21:30:2525 }
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 Chiniforooshanae5a5942018-12-10 17:21:5834 mojom::TraceDataType type) {
chiniforooshan536bfe12017-05-26 21:30:2535 registry_->RegisterAgent(std::move(agent), label, type,
Alexei Filippovc182b7c2018-06-28 21:44:5336 base::kNullProcessId);
chiniforooshan536bfe12017-05-26 21:30:2537 }
38
39 void RegisterAgent(mojom::AgentPtr agent) {
40 registry_->RegisterAgent(std::move(agent), "label",
Ehsan Chiniforooshanae5a5942018-12-10 17:21:5841 mojom::TraceDataType::ARRAY, base::kNullProcessId);
chiniforooshan536bfe12017-05-26 21:30:2542 }
43
44 std::unique_ptr<AgentRegistry> registry_;
chiniforooshan536bfe12017-05-26 21:30:2545
46 private:
47 std::unique_ptr<base::MessageLoop> message_loop_;
48};
49
50TEST_F(AgentRegistryTest, RegisterAgent) {
51 MockAgent agent1;
52 RegisterAgent(agent1.CreateAgentPtr(), "TraceEvent",
Ehsan Chiniforooshanae5a5942018-12-10 17:21:5853 mojom::TraceDataType::ARRAY);
chiniforooshan536bfe12017-05-26 21:30:2554 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());
chiniforooshan536bfe12017-05-26 21:30:2559 });
60 EXPECT_EQ(1u, num_agents);
61
62 MockAgent agent2;
Ehsan Chiniforooshanae5a5942018-12-10 17:21:5863 RegisterAgent(agent2.CreateAgentPtr(), "Power", mojom::TraceDataType::STRING);
chiniforooshan536bfe12017-05-26 21:30:2564 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());
chiniforooshan536bfe12017-05-26 21:30:2572 });
73 EXPECT_EQ(2u, num_agents);
74}
75
76TEST_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
97TEST_F(AgentRegistryTest, AgentInitialization) {
98 size_t num_calls = 0;
99 MockAgent agent1;
100 RegisterAgent(agent1.CreateAgentPtr());
Ehsan Chiniforooshanf527d2a2018-12-14 20:40:45101 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);
chiniforooshan536bfe12017-05-26 21:30:25108 // Since an agent was already registered, the callback should be run once.
Ehsan Chiniforooshanf527d2a2018-12-14 20:40:45109 EXPECT_EQ(1u, num_initialized_agents);
chiniforooshan536bfe12017-05-26 21:30:25110 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);
chiniforooshan536bfe12017-05-26 21:30:25116}
117
118} // namespace tracing