File: eol_notification_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 (400 lines) | stat: -rw-r--r-- 13,982 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2019 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/eol/eol_notification.h"

#include "ash/constants/ash_features.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "chrome/browser/ash/extended_updates/test/mock_extended_updates_controller.h"
#include "chrome/browser/ash/extended_updates/test/scoped_extended_updates_controller.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/components/dbus/update_engine/fake_update_engine_client.h"
#include "chromeos/ash/components/dbus/update_engine/update_engine_client.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/chromeos/devicetype_utils.h"
#include "ui/message_center/public/cpp/notification_types.h"

namespace ash {

class EolNotificationTest : public BrowserWithTestWindowTest {
 public:
  EolNotificationTest() = default;
  ~EolNotificationTest() override = default;

  void SetUp() override {
    fake_update_engine_client_ = UpdateEngineClient::InitializeFakeForTest();
    ConciergeClient::InitializeFake(/*fake_cicerone_client=*/nullptr);
    BrowserWithTestWindowTest::SetUp();

    TestingBrowserProcess::GetGlobal()->SetSystemNotificationHelper(
        std::make_unique<SystemNotificationHelper>());
    tester_ = std::make_unique<NotificationDisplayServiceTester>(profile());

    eol_notification_ = std::make_unique<EolNotification>(profile());
    clock_ = std::make_unique<base::SimpleTestClock>();
    eol_notification_->clock_ = clock_.get();
  }

  void CheckEolInfo() {
    // The callback passed from |eol_notification_| to
    // |fake_update_engine_client_| should be invoked before a notification can
    // appear.
    eol_notification_->CheckEolInfo();
    base::RunLoop().RunUntilIdle();
  }

  void TearDown() override {
    eol_notification_.reset();
    tester_.reset();
    fake_update_engine_client_ = nullptr;
    BrowserWithTestWindowTest::TearDown();
    ConciergeClient::Shutdown();
    UpdateEngineClient::Shutdown();
  }

  void DismissNotification() {
    eol_notification_->Click(EolNotification::ButtonIndex::BUTTON_DISMISS,
                             std::nullopt);
  }

  void SetCurrentTimeToUtc(const char* utc_date_string) {
    base::Time utc_time;
    ASSERT_TRUE(base::Time::FromUTCString(utc_date_string, &utc_time));
    clock_->SetNow(utc_time);
  }

  void SetEolDateUtc(const char* utc_date_string) {
    base::Time utc_date;
    ASSERT_TRUE(base::Time::FromUTCString(utc_date_string, &utc_date));
    fake_update_engine_client_->set_eol_date(utc_date);
  }

 protected:
  raw_ptr<FakeUpdateEngineClient> fake_update_engine_client_;
  std::unique_ptr<NotificationDisplayServiceTester> tester_;
  std::unique_ptr<EolNotification> eol_notification_;
  std::unique_ptr<base::SimpleTestClock> clock_;
};

TEST_F(EolNotificationTest, TestNoNotifciationBeforeEol) {
  SetCurrentTimeToUtc("1 January 2019");
  SetEolDateUtc("1 December 2019");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestFirstWarningNotification) {
  SetCurrentTimeToUtc("1 August 2019");
  SetEolDateUtc("1 December 2019");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  SetCurrentTimeToUtc("15 August 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestSecondWarningNotification) {
  SetCurrentTimeToUtc("1 November 2019");
  SetEolDateUtc("1 December 2019");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  std::u16string expected_title = u"Updates end December 2019";
  std::u16string expected_message =
      u"You'll still be able to use this Chrome device after that time, but it "
      u"will no longer get automatic software and security updates";
  EXPECT_EQ(notification->title(), expected_title);
  EXPECT_EQ(notification->message(), expected_message);

  SetCurrentTimeToUtc("1 October 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  DismissNotification();

  SetCurrentTimeToUtc("15 November 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestFinalEolNotification) {
  SetEolDateUtc("1 December 2019");
  SetCurrentTimeToUtc("2 December 2019");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  std::u16string expected_title = u"Final software update";
  std::u16string expected_message =
      u"This is the last automatic software and security update for this "
      u"Chrome device. To get future updates, upgrade to a newer model.";
  EXPECT_EQ(notification->title(), expected_title);
  EXPECT_EQ(notification->message(), expected_message);

  DismissNotification();

  SetCurrentTimeToUtc("15 December 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestOnEolDateChangeBeforeFirstWarning) {
  SetCurrentTimeToUtc("1 January 2019");
  SetEolDateUtc("1 December 2019");
  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  SetCurrentTimeToUtc("1 January 2019");
  SetEolDateUtc("1 November 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestOnEolDateChangeBeforeSecondWarning) {
  SetCurrentTimeToUtc("1 August 2019");
  SetEolDateUtc("1 December 2019");
  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  // In practice, such a small change in date should not happen.
  SetEolDateUtc("2 December 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestOnEolDateChangeBeforeFinalWarning) {
  SetCurrentTimeToUtc("1 November 2019");
  SetEolDateUtc("1 December 2019");
  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  // Dismiss first warning notification.
  DismissNotification();
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  // In practice, such a small change in date should not happen.
  SetEolDateUtc("2 December 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);
}

TEST_F(EolNotificationTest, TestOnEolDateChangedAfterFinalWarning) {
  SetEolDateUtc("1 December 2019");
  SetCurrentTimeToUtc("3 December 2019");
  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  // Dismiss first warning notification.
  DismissNotification();
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  // Refuse to show notification as eol date is still in the past.
  SetEolDateUtc("2 December 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  // Show as eol date is in the future and within first warning range.
  SetEolDateUtc("4 December 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);
}

TEST_F(EolNotificationTest, TestNotificationUpdatesProperlyWithoutDismissal) {
  SetCurrentTimeToUtc("1 August 2019");
  SetEolDateUtc("1 December 2019");
  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);

  // EOL date arrives and the user has not dismissed the notification.
  SetCurrentTimeToUtc("1 December 2019");
  CheckEolInfo();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);
  std::u16string expected_title = u"Final software update";
  std::u16string expected_message =
      u"This is the last automatic software and security update for this "
      u"Chrome device. To get future updates, upgrade to a newer model.";
  EXPECT_EQ(notification->title(), expected_title);
  EXPECT_EQ(notification->message(), expected_message);

  DismissNotification();
  notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestBackwardsCompatibilityFinalUpdateAlreadyShown) {
  SetEolDateUtc("1 December 2019");
  SetCurrentTimeToUtc("2 December 2019");

  // User dismissed Final Update notification prior to the addition of
  // first and second warning notifications.
  profile()->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, true);

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolNotificationTest, TestOnEolInfoCallsExtendedUpdatesController) {
  SetCurrentTimeToUtc("10 October 2024");

  base::Time eol_date, extended_date;
  ASSERT_TRUE(base::Time::FromUTCString("25 December 2025", &eol_date));
  ASSERT_TRUE(base::Time::FromUTCString("8 June 2024", &extended_date));
  const UpdateEngineClient::EolInfo eol_info{
      .eol_date = eol_date,
      .extended_date = extended_date,
      .extended_opt_in_required = true,
  };
  fake_update_engine_client_->set_eol_info(eol_info);

  ::testing::StrictMock<MockExtendedUpdatesController> mock_controller;
  EXPECT_CALL(mock_controller, OnEolInfo(profile(), eol_info)).Times(1);

  ScopedExtendedUpdatesController scoped_controller(&mock_controller);

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  EXPECT_FALSE(notification);
}

class EolIncentiveNotificationTest : public EolNotificationTest {
 public:
  void SetUp() override {
    EolNotificationTest::SetUp();

    // Set the profile creation date to be at least 6 months before the current
    // time set in these unittests, to correctly show the incentive.
    base::Time creation_time;
    ASSERT_TRUE(base::Time::FromUTCString("1 February 2023", &creation_time));
    profile()->SetCreationTimeForTesting(creation_time);

    scoped_feature_list_.InitAndEnableFeature(ash::features::kEolIncentive);
  }

  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(EolIncentiveNotificationTest, TestIncentiveFarBeforeEolDate) {
  SetCurrentTimeToUtc("1 January 2023");
  SetEolDateUtc("1 December 2023");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_FALSE(notification);
}

TEST_F(EolIncentiveNotificationTest, TestIncentiveBeforeEolDate) {
  SetCurrentTimeToUtc("1 November 2023");
  SetEolDateUtc("1 December 2023");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  DismissNotification();
  ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
      prefs::kEolApproachingIncentiveNotificationDismissed));
}

TEST_F(EolIncentiveNotificationTest, TestIncentiveOnEolDate) {
  SetCurrentTimeToUtc("1 December 2023");
  SetEolDateUtc("1 December 2023");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  DismissNotification();
  ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
      prefs::kEolPassedFinalIncentiveDismissed));
}

TEST_F(EolIncentiveNotificationTest, TestIncentiveAfterEolDate) {
  SetCurrentTimeToUtc("3 December 2023");
  SetEolDateUtc("1 December 2023");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  ASSERT_TRUE(notification);

  DismissNotification();
  ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
      prefs::kEolPassedFinalIncentiveDismissed));
}

TEST_F(EolIncentiveNotificationTest, TestIncentiveFarAfterEolDate) {
  SetCurrentTimeToUtc("20 December 2023");
  SetEolDateUtc("1 December 2023");

  profile()->GetPrefs()->SetBoolean(prefs::kEolPassedFinalIncentiveDismissed,
                                    true);

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  // Check that no notification is shown far ater EOL date if the final
  // incentive was dismissed.
  ASSERT_FALSE(notification);
}

TEST_F(EolIncentiveNotificationTest,
       TestIncentiveFarAfterEolDateIncentiveNotShown) {
  SetCurrentTimeToUtc("20 December 2023");
  SetEolDateUtc("1 December 2023");

  CheckEolInfo();
  auto notification = tester_->GetNotification("chrome://product_eol");
  // Check that a notification is shown far after EOL date if the final
  // incentive was not dismissed.
  ASSERT_TRUE(notification);

  DismissNotification();
  ASSERT_TRUE(
      profile()->GetPrefs()->GetBoolean(prefs::kEolNotificationDismissed));
}

}  // namespace ash