File: telemetry_event_service_ash_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (196 lines) | stat: -rw-r--r-- 6,776 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/telemetry_extension/events/telemetry_event_service_ash.h"

#include <cstdint>
#include <memory>
#include <string>
#include <utility>

#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/mojo_service_manager/fake_mojo_service_manager.h"
#include "chromeos/ash/services/cros_healthd/public/cpp/fake_cros_healthd.h"
#include "chromeos/ash/services/cros_healthd/public/mojom/cros_healthd_events.mojom.h"
#include "chromeos/ash/services/cros_healthd/public/mojom/cros_healthd_exception.mojom.h"
#include "chromeos/crosapi/mojom/telemetry_event_service.mojom.h"
#include "chromeos/crosapi/mojom/telemetry_extension_exception.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

class TestEventObserver : public crosapi::mojom::TelemetryEventObserver {
 public:
  TestEventObserver() : receiver_(this) {}
  TestEventObserver(const TestEventObserver&) = delete;
  TestEventObserver& operator=(const TestEventObserver&) = delete;
  ~TestEventObserver() override = default;

  // crosapi::mojom::TelemetryEventObserver:
  void OnEvent(crosapi::mojom::TelemetryEventInfoPtr info) override {
    event_future_.SetValue(std::move(info));
  }

  crosapi::mojom::TelemetryEventInfoPtr WaitAndGetEvent() {
    return event_future_.Take();
  }

  mojo::PendingRemote<crosapi::mojom::TelemetryEventObserver> GetRemote() {
    return receiver_.BindNewPipeAndPassRemote();
  }

  mojo::Receiver<crosapi::mojom::TelemetryEventObserver>& GetReceiver() {
    return receiver_;
  }

  void Reset() { receiver_.reset(); }

 private:
  mojo::Receiver<crosapi::mojom::TelemetryEventObserver> receiver_;
  base::test::TestFuture<crosapi::mojom::TelemetryEventInfoPtr> event_future_;
};

}  // namespace

class TelemetryEventServiceAshTest : public testing::Test {
 public:
  // testing::Test:
  void SetUp() override { cros_healthd::FakeCrosHealthd::Initialize(); }
  void TearDown() override { cros_healthd::FakeCrosHealthd::Shutdown(); }

  crosapi::mojom::TelemetryEventServiceProxy* event_service() const {
    return remote_event_service_.get();
  }

 protected:
  TestEventObserver& observer() { return observer_; }

  void FlushForTesting() { remote_event_service_.FlushForTesting(); }

 private:
  base::test::TaskEnvironment task_environment_;
  TestEventObserver observer_;

  mojo::Remote<crosapi::mojom::TelemetryEventService> remote_event_service_;
  std::unique_ptr<crosapi::mojom::TelemetryEventService> event_service_{
      TelemetryEventServiceAsh::Factory::Create(
          remote_event_service_.BindNewPipeAndPassReceiver())};
  mojo_service_manager::FakeMojoServiceManager fake_service_manager_;
};

TEST_F(TelemetryEventServiceAshTest, AddEventObserver) {
  event_service()->AddEventObserver(
      crosapi::mojom::TelemetryEventCategoryEnum::kAudioJack,
      observer().GetRemote());

  // Flush so that the registration shows up.
  observer().GetReceiver().FlushForTesting();
  FlushForTesting();

  auto audio_jack_info = cros_healthd::mojom::AudioJackEventInfo::New();
  audio_jack_info->state =
      cros_healthd::mojom::AudioJackEventInfo::State::kRemove;
  audio_jack_info->device_type =
      cros_healthd::mojom::AudioJackEventInfo::DeviceType::kHeadphone;

  auto info = cros_healthd::mojom::EventInfo::NewAudioJackEventInfo(
      std::move(audio_jack_info));
  cros_healthd::FakeCrosHealthd::Get()->EmitEventForCategory(
      cros_healthd::mojom::EventCategoryEnum::kAudioJack, std::move(info));

  // Flush so that the result shows up.
  FlushForTesting();

  EXPECT_EQ(observer().WaitAndGetEvent(),
            crosapi::mojom::TelemetryEventInfo::NewAudioJackEventInfo(
                crosapi::mojom::TelemetryAudioJackEventInfo::New(
                    crosapi::mojom::TelemetryAudioJackEventInfo::State::kRemove,
                    crosapi::mojom::TelemetryAudioJackEventInfo::DeviceType::
                        kHeadphone)));
}

TEST_F(TelemetryEventServiceAshTest, IsEventSupported) {
  // Set the expected result in cros_healthd.
  auto expected_result = cros_healthd::mojom::SupportStatus::NewSupported(
      cros_healthd::mojom::Supported::New());
  cros_healthd::FakeCrosHealthd::Get()->SetIsEventSupportedResponseForTesting(
      expected_result);

  base::test::TestFuture<crosapi::mojom::TelemetryExtensionSupportStatusPtr>
      future;

  event_service()->IsEventSupported(
      crosapi::mojom::TelemetryEventCategoryEnum::kAudioJack,
      future.GetCallback());

  EXPECT_TRUE(future.Get()->is_supported());
}

TEST_F(TelemetryEventServiceAshTest, OnCrosapiDisconnect) {
  event_service()->AddEventObserver(
      crosapi::mojom::TelemetryEventCategoryEnum::kAudioJack,
      observer().GetRemote());

  // Flush so that the registration shows up.
  observer().GetReceiver().FlushForTesting();
  FlushForTesting();

  mojo::RemoteSet<cros_healthd::mojom::EventObserver>* observers =
      cros_healthd::FakeCrosHealthd::Get()->GetObserversByCategory(
          cros_healthd::mojom::EventCategoryEnum::kAudioJack);

  ASSERT_TRUE(observers);
  EXPECT_EQ(observers->size(), 1UL);
  observer().Reset();

  // Flush so that the reset shows up.
  FlushForTesting();

  EXPECT_EQ(observers->size(), 0UL);
}

TEST_F(TelemetryEventServiceAshTest, OnCrosHealthdDisconnect) {
  constexpr uint32_t kReason = 123;
  constexpr char kMsg[] = "test";

  event_service()->AddEventObserver(
      crosapi::mojom::TelemetryEventCategoryEnum::kAudioJack,
      observer().GetRemote());

  base::test::TestFuture<void> future;
  observer().GetReceiver().set_disconnect_with_reason_handler(
      base::BindLambdaForTesting(
          [&future, kReason, kMsg](uint32_t reason, const std::string& msg) {
            EXPECT_EQ(reason, kReason);
            EXPECT_EQ(msg, kMsg);
            future.SetValue();
          }));

  // Flush so that the registration shows up.
  observer().GetReceiver().FlushForTesting();
  FlushForTesting();

  mojo::RemoteSet<cros_healthd::mojom::EventObserver>* observers =
      cros_healthd::FakeCrosHealthd::Get()->GetObserversByCategory(
          cros_healthd::mojom::EventCategoryEnum::kAudioJack);

  ASSERT_TRUE(observers);
  EXPECT_EQ(observers->size(), 1UL);
  observers->ClearWithReason(kReason, kMsg);

  // Flush so that the reset shows up.
  observers->FlushForTesting();

  EXPECT_TRUE(future.Wait());
}

}  // namespace ash