File: boca_notification_handler_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 (189 lines) | stat: -rw-r--r-- 7,435 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 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/boca/notifications/boca_notification_handler.h"

#include "chromeos/ash/components/boca/boca_app_client.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/fake_message_center.h"
#include "ui/message_center/message_center.h"

using ::testing::StrictMock;

namespace ash::boca {
namespace {
class MockBocaAppClient : public BocaAppClient {
 public:
  MOCK_METHOD(signin::IdentityManager*, GetIdentityManager, (), (override));
  MOCK_METHOD(scoped_refptr<network::SharedURLLoaderFactory>,
              GetURLLoaderFactory,
              (),
              (override));
  MOCK_METHOD(void, LaunchApp, (), (override));
};

// TODO(crbug.com/376592382): Move this to shared library after to be reused by
// onTask and other components. Mock message center as we don't have the
// necessary dependencies to verify real notification.
class TestMessageCenter : public message_center::FakeMessageCenter {
 public:
  TestMessageCenter() = default;

  TestMessageCenter(const TestMessageCenter&) = delete;
  TestMessageCenter& operator=(const TestMessageCenter&) = delete;

  ~TestMessageCenter() override = default;

  // message_center::FakeMessageCenter:
  void AddNotification(
      std::unique_ptr<message_center::Notification> notification) override {
    notifications_.emplace(notification->id(), *notification);
  }

  void RemoveNotification(const std::string& id, bool by_user) override {
    if (!notifications_.contains(id)) {
      return;
    }
    notifications_.erase(id);
    for (auto& observer : observer_list()) {
      observer.OnNotificationRemoved(id, by_user);
    }
  }

  message_center::Notification* FindVisibleNotificationById(
      const std::string& id) override {
    if (notifications_.contains(id)) {
      return &notifications_.at(id);
    }
    return nullptr;
  }

  void ClickOnNotification(const std::string& id) override {
    auto* notification = &notifications_.at(id);
    EXPECT_TRUE(notification);
    for (auto& observer : observer_list()) {
      observer.OnNotificationClicked(id, std::nullopt, std::nullopt);
    }
  }

  void ClickOnNotificationButton(const std::string& id,
                                 int button_index) override {
    auto* notification = &notifications_.at(id);
    EXPECT_TRUE(notification);
    EXPECT_EQ(id, notification->id());
    notification->delegate()->Click(button_index, std::nullopt);

    for (auto& observer : observer_list()) {
      observer.OnNotificationClicked(id, button_index, std::nullopt);
    }
  }

 private:
  std::map<std::string, message_center::Notification> notifications_;
};

class BocaNotificationHandlerTest : public testing::Test {
 protected:
  BocaNotificationHandlerTest() = default;
  TestMessageCenter test_message_center_;
  BocaNotificationHandler handler_;
  StrictMock<MockBocaAppClient> boca_app_client_;
};

TEST_F(BocaNotificationHandlerTest,
       HandleSessionStartShouldCreateNotification) {
  handler_.HandleSessionStartedNotification(&test_message_center_);
  auto* notification = test_message_center_.FindVisibleNotificationById(
      handler_.kSessionNotificationId);
  EXPECT_TRUE(notification);
  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_BOCA_CONNECTED_TO_CLASS_NOTIFICATION_MESSAGE),
            notification->message());
}

TEST_F(BocaNotificationHandlerTest, HandleClickButtonShouldOpenApp) {
  handler_.HandleSessionStartedNotification(&test_message_center_);
  EXPECT_TRUE(test_message_center_.FindVisibleNotificationById(
      handler_.kSessionNotificationId));
  EXPECT_CALL(boca_app_client_, LaunchApp()).Times(1);
  test_message_center_.ClickOnNotificationButton(
      /*id=*/handler_.kSessionNotificationId,
      /*button_index=*/0);
}

TEST_F(BocaNotificationHandlerTest,
       HandleSessionStartShouldRemoveNotification) {
  handler_.HandleSessionEndedNotification(&test_message_center_);
  EXPECT_FALSE(test_message_center_.FindVisibleNotificationById(
      handler_.kSessionNotificationId));
  EXPECT_FALSE(test_message_center_.FindVisibleNotificationById(
      handler_.kCaptionNotificationId));
}

TEST_F(BocaNotificationHandlerTest,
       HandleCaptionOnShouldAddCaptionNotification) {
  handler_.HandleSessionStartedNotification(&test_message_center_);

  handler_.HandleCaptionNotification(&test_message_center_,
                                     /*is_local_caption_enabled=*/true,
                                     /*is_session_caption_enabled=*/true);
  auto* notification = test_message_center_.FindVisibleNotificationById(
      handler_.kCaptionNotificationId);
  EXPECT_TRUE(notification);
  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_BOCA_MICROPHONE_IN_USE_NOTIFICATION_MESSAGE),
            notification->message());
  EXPECT_TRUE(test_message_center_.FindVisibleNotificationById(
      handler_.kSessionNotificationId));
}

TEST_F(BocaNotificationHandlerTest, HandleCaptionOffShouldRemoveNotification) {
  handler_.HandleSessionStartedNotification(&test_message_center_);

  handler_.HandleCaptionNotification(&test_message_center_,
                                     /*is_local_caption_enabled=*/true,
                                     /*is_session_caption_enabled=*/true);
  handler_.HandleCaptionNotification(&test_message_center_,
                                     /*is_local_caption_enabled=*/false,
                                     /*is_session_caption_enabled=*/false);
  EXPECT_FALSE(test_message_center_.FindVisibleNotificationById(
      handler_.kCaptionNotificationId));
  EXPECT_TRUE(test_message_center_.FindVisibleNotificationById(
      handler_.kSessionNotificationId));
}

TEST_F(BocaNotificationHandlerTest,
       HandleSessionEndShouldRemoveSessionCaptionNotification) {
  handler_.HandleSessionStartedNotification(&test_message_center_);

  handler_.HandleCaptionNotification(&test_message_center_,
                                     /*is_local_caption_enabled=*/false,
                                     /*is_session_caption_enabled=*/true);
  handler_.HandleSessionEndedNotification(&test_message_center_);
  EXPECT_FALSE(test_message_center_.FindVisibleNotificationById(
      handler_.kCaptionNotificationId));
  EXPECT_FALSE(test_message_center_.FindVisibleNotificationById(
      handler_.kSessionNotificationId));
}

TEST_F(BocaNotificationHandlerTest,
       HandleSessionEndShouldNotRemoveLocalCaptionNotification) {
  handler_.HandleSessionStartedNotification(&test_message_center_);

  handler_.HandleCaptionNotification(&test_message_center_,
                                     /*is_local_caption_enabled=*/true,
                                     /*is_session_caption_enabled=*/true);
  handler_.HandleSessionEndedNotification(&test_message_center_);
  EXPECT_TRUE(test_message_center_.FindVisibleNotificationById(
      handler_.kCaptionNotificationId));
  EXPECT_FALSE(test_message_center_.FindVisibleNotificationById(
      handler_.kSessionNotificationId));
}
}  // namespace
}  // namespace ash::boca