File: eche_app_notification_controller_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (268 lines) | stat: -rw-r--r-- 11,364 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/eche_app/eche_app_notification_controller.h"

#include "ash/public/cpp/test/test_new_window_delegate.h"
#include "ash/webui/eche_app_ui/eche_alert_generator.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace ash {
namespace eche_app {

namespace {

void RelaunchEcheApp(Profile* profile) {}

}  // namespace

class TestableNotificationController : public EcheAppNotificationController {
 public:
  explicit TestableNotificationController(
      Profile* profile,
      const base::RepeatingCallback<void(Profile*)>& relaunch_callback)
      : EcheAppNotificationController(profile, relaunch_callback) {}
  ~TestableNotificationController() override = default;
  TestableNotificationController(const TestableNotificationController&) =
      delete;
  TestableNotificationController& operator=(
      const TestableNotificationController&) = delete;

  // EcheAppNotificationController:
  MOCK_METHOD0(LaunchSettings, void());
  MOCK_METHOD0(LaunchTryAgain, void());
  MOCK_METHOD0(LaunchNetworkSettings, void());
};

class MockNewWindowDelegate : public testing::NiceMock<TestNewWindowDelegate> {
 public:
  // TestNewWindowDelegate:
  MOCK_METHOD(void,
              OpenUrl,
              (const GURL& url, OpenUrlFrom from, Disposition disposition),
              (override));
};

class EcheAppNotificationControllerTest : public BrowserWithTestWindowTest {
 protected:
  EcheAppNotificationControllerTest() = default;

  ~EcheAppNotificationControllerTest() override = default;
  EcheAppNotificationControllerTest(const EcheAppNotificationControllerTest&) =
      delete;
  EcheAppNotificationControllerTest& operator=(
      const EcheAppNotificationControllerTest&) = delete;

  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();

    display_service_ =
        std::make_unique<NotificationDisplayServiceTester>(profile());
    notification_controller_ =
        std::make_unique<testing::StrictMock<TestableNotificationController>>(
            profile(), base::BindRepeating(&RelaunchEcheApp));
  }

  std::unique_ptr<testing::StrictMock<TestableNotificationController>>
      notification_controller_;
  std::unique_ptr<NotificationDisplayServiceTester> display_service_;

  void Initialize(mojom::WebNotificationType type) {
    std::optional<std::u16string> title = u"title";
    std::optional<std::u16string> message = u"message";
    notification_controller_->ShowNotificationFromWebUI(title, message, type);
  }

  void VerifyNotificationHasAction(
      std::optional<message_center::Notification>& notification) {
    ASSERT_TRUE(notification);
    ASSERT_EQ(2u, notification->buttons().size());
    EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());

    // Clicking the notification button should launch try again.
    EXPECT_CALL(*notification_controller_, LaunchTryAgain());
    notification->delegate()->Click(0, std::nullopt);
  }

 private:
  MockNewWindowDelegate new_window_delegate_;
};

TEST_F(EcheAppNotificationControllerTest, ShowNotificationFromWebUI) {
  std::optional<std::u16string> title = u"Connection Fail Title";
  std::optional<std::u16string> message = u"Connection Fail Message";
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::CONNECTION_FAILED);
  std::optional<message_center::Notification> notification =
      display_service_->GetNotification(kEcheAppRetryConnectionNotifierId);
  ASSERT_TRUE(notification);
  ASSERT_EQ(1u, notification->buttons().size());
  EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
  EXPECT_EQ(notification->title(), title);
  EXPECT_EQ(notification->message(), message);

  // Clicking the notification button should relaunch again.
  EXPECT_CALL(*notification_controller_, LaunchTryAgain());
  notification->delegate()->Click(std::nullopt, std::nullopt);

  title = u"Connection Lost Title";
  message = u"Connection Lost Message";
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::CONNECTION_LOST);
  notification =
      display_service_->GetNotification(kEcheAppRetryConnectionNotifierId);
  ASSERT_TRUE(notification.has_value());
  ASSERT_EQ(1u, notification->buttons().size());
  EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
  EXPECT_EQ(notification->title(), title);
  EXPECT_EQ(notification->message(), message);

  // Clicking the notification button should relaunch again.
  EXPECT_CALL(*notification_controller_, LaunchTryAgain());
  notification->delegate()->Click(std::nullopt, std::nullopt);

  title = u"Inactivity Title";
  message = u"Inactivity Message";
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::DEVICE_IDLE);
  notification =
      display_service_->GetNotification(kEcheAppInactivityNotifierId);
  ASSERT_TRUE(notification.has_value());
  ASSERT_EQ(1u, notification->buttons().size());
  EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
  EXPECT_EQ(notification->title(), title);
  EXPECT_EQ(notification->message(), message);

  // Clicking the first notification button should relaunch again.
  EXPECT_CALL(*notification_controller_, LaunchTryAgain());
  notification->delegate()->Click(std::nullopt, std::nullopt);

  title = u"Check WIFI Title";
  message = u"Check WIFI Message";
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::WIFI_NOT_READY);
  notification =
      display_service_->GetNotification(kEcheAppNetworkSettingNotifierId);
  ASSERT_TRUE(notification.has_value());
  ASSERT_EQ(1u, notification->buttons().size());
  EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
  EXPECT_EQ(notification->title(), title);
  EXPECT_EQ(notification->message(), message);

  // Clicking the notification button should launch network settings.
  EXPECT_CALL(*notification_controller_, LaunchNetworkSettings());
  notification->delegate()->Click(std::nullopt, std::nullopt);
}

TEST_F(EcheAppNotificationControllerTest, ShowScreenLockNotification) {
  std::u16string title = u"title";
  notification_controller_->ShowScreenLockNotification(title);
  std::optional<message_center::Notification> notification =
      display_service_->GetNotification(kEcheAppScreenLockNotifierId);
  ASSERT_TRUE(notification.has_value());
  ASSERT_TRUE(notification->title().size() > 0);
  ASSERT_TRUE(notification->message().size() > 0);
  ASSERT_EQ(1u, notification->buttons().size());
  EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());

  // Clicking the notification button should launch settings.
  EXPECT_CALL(*notification_controller_, LaunchSettings());
  notification->delegate()->Click(std::nullopt, std::nullopt);
}

TEST_F(EcheAppNotificationControllerTest,
       ShowScreenLockNotificationWithNullValue) {
  // Null value for title should still show a degraded message.
  std::u16string title;
  notification_controller_->ShowScreenLockNotification(title);
  std::optional<message_center::Notification> notification =
      display_service_->GetNotification(kEcheAppScreenLockNotifierId);
  ASSERT_TRUE(notification.has_value());
  ASSERT_TRUE(notification->title().size() > 0);
  ASSERT_TRUE(notification->message().size() > 0);
  ASSERT_EQ(1u, notification->buttons().size());
  EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());

  // Clicking the notification button should launch settings.
  EXPECT_CALL(*notification_controller_, LaunchSettings());
  notification->delegate()->Click(std::nullopt, std::nullopt);
}

TEST_F(EcheAppNotificationControllerTest, CloseNotification) {
  std::u16string title = u"title";
  notification_controller_->ShowScreenLockNotification(title);
  notification_controller_->CloseNotification(kEcheAppScreenLockNotifierId);
  std::optional<message_center::Notification> notification =
      display_service_->GetNotification(kEcheAppScreenLockNotifierId);
  ASSERT_FALSE(notification.has_value());

  std::optional<std::u16string> message = u"message";
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::CONNECTION_FAILED);
  notification_controller_->CloseNotification(
      kEcheAppRetryConnectionNotifierId);
  notification =
      display_service_->GetNotification(kEcheAppRetryConnectionNotifierId);
  ASSERT_FALSE(notification.has_value());

  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::DEVICE_IDLE);
  notification_controller_->CloseNotification(kEcheAppInactivityNotifierId);
  notification =
      display_service_->GetNotification(kEcheAppInactivityNotifierId);
  ASSERT_FALSE(notification.has_value());

  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::INVALID_NOTIFICATION);
  notification_controller_->CloseNotification(
      kEcheAppFromWebWithoutButtonNotifierId);
  notification =
      display_service_->GetNotification(kEcheAppFromWebWithoutButtonNotifierId);
  ASSERT_FALSE(notification.has_value());

  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::WIFI_NOT_READY);
  notification_controller_->CloseNotification(
      kEcheAppFromWebWithoutButtonNotifierId);
  notification =
      display_service_->GetNotification(kEcheAppFromWebWithoutButtonNotifierId);
  ASSERT_FALSE(notification.has_value());
}

TEST_F(EcheAppNotificationControllerTest,
       CloseConnectionOrLaunchErrorNotifications) {
  std::u16string title = u"title";
  std::optional<std::u16string> message = u"message";
  notification_controller_->ShowScreenLockNotification(title);
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::CONNECTION_FAILED);
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::DEVICE_IDLE);
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::INVALID_NOTIFICATION);
  notification_controller_->ShowNotificationFromWebUI(
      title, message, mojom::WebNotificationType::WIFI_NOT_READY);
  notification_controller_->CloseConnectionOrLaunchErrorNotifications();

  std::optional<message_center::Notification> notification =
      display_service_->GetNotification(kEcheAppScreenLockNotifierId);
  ASSERT_TRUE(notification.has_value());
  notification =
      display_service_->GetNotification(kEcheAppRetryConnectionNotifierId);
  ASSERT_FALSE(notification.has_value());
  notification =
      display_service_->GetNotification(kEcheAppInactivityNotifierId);
  ASSERT_FALSE(notification.has_value());
  notification =
      display_service_->GetNotification(kEcheAppFromWebWithoutButtonNotifierId);
  ASSERT_FALSE(notification.has_value());
  notification =
      display_service_->GetNotification(kEcheAppNetworkSettingNotifierId);
  ASSERT_FALSE(notification.has_value());
}

}  // namespace eche_app
}  // namespace ash