File: hats_notification_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (488 lines) | stat: -rw-r--r-- 19,621 bytes parent folder | download | duplicates (5)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright 2016 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/hats/hats_notification_controller.h"

#include <memory>

#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "chrome/browser/ash/hats/hats_config.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.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/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_state.h"
#include "components/image_fetcher/core/request_metadata.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/sync_preferences/pref_service_mock_factory.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "net/base/url_util.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/public/cpp/notification.h"
#include "url/gurl.h"

using testing::_;
using testing::AtLeast;
using testing::Invoke;
using testing::NiceMock;
using testing::Return;
using testing::SaveArg;

namespace {

const char kLocaleKey[] = "locale";
const char kBrowserKey[] = "browser";
const char kPlatformKey[] = "platform";
const char kFirmwareKey[] = "firmware";
const char kPsdKey1[] = "psd1";
const char kPsdKey2[] = "psd2";
const char kPsdValue1[] = "psdValue1";
const char kPsdValue2[] = "psdValue2 =%^*$#&";
const char kLocaleValue1[] = "locale1";
const char kBrowserValue1[] = "browser1";

const ash::HatsConfig kNonPrioritizedTestConfig{
    ::features::kHappinessTrackingSystem,
    base::Days(7),
    prefs::kHatsDeviceIsSelected,
    prefs::kHatsSurveyCycleEndTimestamp,
};
const char kFeatureNonPrioritizedCommandLine[] =
    "HappinessTrackingSystem:"
    "prob/1/survey_cycle_length/9/"
    "survey_start_date_ms/1600000000000/"
    "trigger_id/random_trigger_id_non_prio";

const ash::HatsConfig kPrioritizedTestConfig{
    ::features::kHappinessTrackingGeneralCameraPrioritized,
    base::Days(7),
    prefs::kHatsGeneralCameraPrioritizedIsSelected,
    prefs::kHatsGeneralCameraPrioritizedSurveyCycleEndTs,
    prefs::kHatsGeneralCameraPrioritizedLastInteractionTimestamp,
    base::Days(120)};
const char kFeaturePrioritizedCommandLine[] =
    "HappinessTrackingGeneralCameraPrioritized:"
    "prob/1/survey_cycle_length/9/"
    "survey_start_date_ms/1600000000000/"
    "trigger_id/random_trigger_id_prio";

bool GetQueryParameter(const std::string& query,
                       const std::string& key,
                       std::string* value) {
  // Name and scheme actually don't matter, but are required to get a valid URL
  // for parsing.
  GURL query_url("http://localhost?" + query);
  return net::GetValueForKeyInQuery(query_url, key, value);
}

}  // namespace

namespace ash {

struct HatsScenario {
  // If true, |kPrioritizedTestConfig| will be used.
  // Otherwise |kNonPrioritizedTestConfig| will be used.
  bool prioritized;

  // How long since the previous prio HaTS was shown.
  base::TimeDelta prev_prio_hats;

  // How long since the previous non prio HaTS was shown.
  base::TimeDelta prev_non_prio_hats;

  // Whether the test (prio) HaTS was shown recently or not.
  // If true, then the |pref.survey_last_interaction_timestamp_pref_name|
  // will be set to |today's timestamp| - | pref.threshold_time| + | 1 day |.
  bool this_hats_recent = false;

  // Expected return value of
  // HatsNotificationController::ShouldShowSurveyToProfile.
  bool should_be_selected;
};

class HatsNotificationControllerTest
    : public BrowserWithTestWindowTest,
      public testing::WithParamInterface<HatsScenario> {
 public:
  HatsNotificationControllerTest() = default;

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

  ~HatsNotificationControllerTest() override = default;

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

    display_service_ =
        std::make_unique<NotificationDisplayServiceTester>(profile());
    helper_ = std::make_unique<NetworkHandlerTestHelper>();

    std::string feature_list = kFeatureNonPrioritizedCommandLine;
    feature_list += ",";
    feature_list += kFeaturePrioritizedCommandLine;

    scoped_feature_list_.InitFromCommandLine(feature_list, "");
  }

  TestingProfile* CreateProfile(const std::string& profile_name) override {
    sync_preferences::PrefServiceMockFactory factory;
    auto registry = base::MakeRefCounted<user_prefs::PrefRegistrySyncable>();
    std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs(
        factory.CreateSyncable(registry.get()));
    RegisterUserProfilePrefs(registry.get());
    auto* profile = profile_manager()->CreateTestingProfile(
        profile_name, std::move(prefs), std::u16string(), 0,
        TestingProfile::TestingFactories());
    return profile;
  }

  void OnUserProfileCreated(const std::string& profile_name,
                            Profile* profile) override {
    BrowserWithTestWindowTest::OnUserProfileCreated(profile_name, profile);
    user_manager()->SetOwnerId(
        ProfileHelper::Get()->GetUserByProfile(profile)->GetAccountId());
  }

  void TearDown() override {
    helper_.reset();
    display_service_.reset();
    // Notifications may be deleted async.
    base::RunLoop().RunUntilIdle();

    BrowserWithTestWindowTest::TearDown();
  }

  scoped_refptr<HatsNotificationController> InstantiateHatsController() {
    // The initialization will fail since the function IsNewDevice() will return
    // true.
    auto hats_notification_controller =
        base::MakeRefCounted<HatsNotificationController>(profile(),
                                                         kHatsGeneralSurvey);

    // HatsController::IsNewDevice() is run on a blocking thread.
    content::RunAllTasksUntilIdle();

    return hats_notification_controller;
  }

  scoped_refptr<HatsNotificationController> InstantiateHatsControllerWithConfig(
      const HatsConfig* config) {
    // The initialization will fail since the function IsNewDevice() will return
    // true.
    auto hats_notification_controller =
        base::MakeRefCounted<HatsNotificationController>(profile(), *config);

    // HatsController::IsNewDevice() is run on a blocking thread.
    content::RunAllTasksUntilIdle();

    return hats_notification_controller;
  }

  void SendPortalState(scoped_refptr<HatsNotificationController>& controller,
                       NetworkState::PortalState portal_state) {
    NetworkState network_state("");
    controller->PortalStateChanged(&network_state, portal_state);
    base::RunLoop().RunUntilIdle();
  }

  const raw_ref<const HatsConfig> GetHatsConfig() const {
    return GetParam().prioritized
               ? raw_ref<const HatsConfig>(kPrioritizedTestConfig)
               : raw_ref<const HatsConfig>(kNonPrioritizedTestConfig);
  }

  std::unique_ptr<NotificationDisplayServiceTester> display_service_;
  std::unique_ptr<NetworkHandlerTestHelper> helper_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(HatsNotificationControllerTest, GetFormattedSiteContext) {
  base::flat_map<std::string, std::string> product_specific_data = {
      {kPsdKey1, kPsdValue1},
      {kPsdKey2, kPsdValue2},
      {kBrowserKey, kBrowserValue1}};

  std::string context = HatsNotificationController::GetFormattedSiteContext(
      kLocaleValue1, product_specific_data);

  std::string value;
  EXPECT_TRUE(GetQueryParameter(context, kLocaleKey, &value));
  EXPECT_EQ(kLocaleValue1, value);
  EXPECT_TRUE(GetQueryParameter(context, kBrowserKey, &value));
  EXPECT_NE(kBrowserValue1, value);
  EXPECT_TRUE(GetQueryParameter(context, kPlatformKey, &value));
  EXPECT_TRUE(GetQueryParameter(context, kFirmwareKey, &value));

  EXPECT_TRUE(GetQueryParameter(context, kPsdKey1, &value));
  EXPECT_EQ(kPsdValue1, value);
  EXPECT_TRUE(GetQueryParameter(context, kPsdKey2, &value));
  EXPECT_EQ(kPsdValue2, value);

  // Confirm that the values are properly url escaped.
  EXPECT_NE(std::string::npos, context.find("psdValue2%20%3D%25%5E*%24%23%26"));
}

TEST_F(HatsNotificationControllerTest, NewDevice_ShouldNotShowNotification) {
  int64_t initial_timestamp = base::Time::Now().ToInternalValue();
  PrefService* pref_service = profile()->GetPrefs();
  pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp,
                         initial_timestamp);

  auto hats_notification_controller = InstantiateHatsController();
  hats_notification_controller->Initialize(true);

  int64_t current_timestamp =
      pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);

  // When the device is new, the controller does not begin initialization and
  // simply updates the timestamp to Time::Now().
  ASSERT_TRUE(base::Time::FromInternalValue(current_timestamp) >
              base::Time::FromInternalValue(initial_timestamp));

  EXPECT_FALSE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));
}

TEST_F(HatsNotificationControllerTest, OldDevice_ShouldShowNotification) {
  auto hats_notification_controller = InstantiateHatsController();
  hats_notification_controller->Initialize(false);

  // Ensure notification was launched to confirm initialization.
  EXPECT_TRUE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));

  // Simulate dismissing notification by the user to clean up the notification.
  display_service_->RemoveNotification(
      NotificationHandler::Type::TRANSIENT,
      HatsNotificationController::kNotificationId, /*by_user=*/true);
}

TEST_F(HatsNotificationControllerTest, NoInternet_DoNotShowNotification) {
  auto hats_notification_controller = InstantiateHatsController();

  SendPortalState(hats_notification_controller,
                  NetworkState::PortalState::kUnknown);
  EXPECT_FALSE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));

  SendPortalState(hats_notification_controller,
                  NetworkState::PortalState::kNoInternet);
  EXPECT_FALSE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));

  SendPortalState(hats_notification_controller,
                  NetworkState::PortalState::kPortal);
  EXPECT_FALSE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));
}

TEST_F(HatsNotificationControllerTest, DismissNotification_ShouldUpdatePref) {
  int64_t now_timestamp = base::Time::Now().ToInternalValue();
  PrefService* pref_service = profile()->GetPrefs();
  pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp, now_timestamp);

  auto hats_notification_controller = InstantiateHatsController();

  // Simulate closing notification via user interaction.
  hats_notification_controller->Close(true);

  int64_t new_timestamp =
      pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
  // The flag should be updated to a new timestamp.
  EXPECT_TRUE(base::Time::FromInternalValue(new_timestamp) >
              base::Time::FromInternalValue(now_timestamp));
}

TEST_F(HatsNotificationControllerTest,
       DismissNotification_PrioritizedShouldUpdatePref) {
  base::Time now_timestamp = base::Time::Now();
  PrefService* pref_service = profile()->GetPrefs();
  pref_service->SetInt64(prefs::kHatsLastInteractionTimestamp,
                         now_timestamp.ToInternalValue());

  // Make sure time has actually advanced
  base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());

  auto hats_notification_controller =
      InstantiateHatsControllerWithConfig(&kPrioritizedTestConfig);

  // Simulate closing notification via user interaction.
  hats_notification_controller->Close(true);

  base::Time survey_timestamp = pref_service->GetTime(
      kPrioritizedTestConfig.survey_last_interaction_timestamp_pref_name);
  // The flag should be updated to a new timestamp.
  EXPECT_GT(survey_timestamp, now_timestamp);

  base::Time global_timestamp =
      pref_service->GetTime(prefs::kHatsPrioritizedLastInteractionTimestamp);
  // All prioritized HaTS has its own cooldown timestamp, should be updated.
  EXPECT_GT(global_timestamp, now_timestamp);

  // The general HaTS timestamp should not be changed.
  int64_t hats_timestamp =
      pref_service->GetInt64(prefs::kHatsLastInteractionTimestamp);
  EXPECT_EQ(base::Time::FromInternalValue(hats_timestamp), now_timestamp);
}

TEST_F(HatsNotificationControllerTest,
       Disconnected_RemoveNotification_Connected_AddNotification) {
  auto hats_notification_controller = InstantiateHatsController();

  hats_notification_controller->Initialize(false);

  // Notification is launched.
  EXPECT_TRUE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));

  // Notification is removed when Internet connection is lost.
  SendPortalState(hats_notification_controller,
                  NetworkState::PortalState::kNoInternet);
  EXPECT_FALSE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));

  // Notification is launched again when Internet connection is regained.
  SendPortalState(hats_notification_controller,
                  NetworkState::PortalState::kOnline);
  EXPECT_TRUE(display_service_->GetNotification(
      HatsNotificationController::kNotificationId));

  // Simulate dismissing notification by the user to clean up the notification.
  display_service_->RemoveNotification(
      NotificationHandler::Type::TRANSIENT,
      HatsNotificationController::kNotificationId, /*by_user=*/true);
}

TEST_P(HatsNotificationControllerTest, ShouldShowSurveyToProfile) {
  // Insert the time values into prefs
  PrefService* pref_service = profile()->GetPrefs();
  pref_service->SetTime(prefs::kHatsPrioritizedLastInteractionTimestamp,
                        base::Time::Now() - GetParam().prev_prio_hats);
  pref_service->SetTime(prefs::kHatsLastInteractionTimestamp,
                        base::Time::Now() - GetParam().prev_non_prio_hats);

  // Explanation by example:
  // Given HaTS config -> threshold_time: 90 days
  // If param |this_hats_recent| is true, then the timestamp of
  // |survey_last_interaction_timestamp_pref_name| will be 89 days ago,
  // otherwise the timestamp will be 91 days ago.
  if (GetParam().prioritized) {
    pref_service->SetTime(
        GetHatsConfig()->survey_last_interaction_timestamp_pref_name,
        base::Time::Now() - GetHatsConfig()->threshold_time +
            (GetParam().this_hats_recent ? base::Days(1) : -base::Days(1)));
  }

  EXPECT_EQ(HatsNotificationController::ShouldShowSurveyToProfile(
                profile(), *GetHatsConfig()),
            GetParam().should_be_selected);
}

const std::vector<HatsScenario> kHatsScenario{
    // Prio HaTS, any other HaTS was shown very long time ago.
    {.prioritized = true,
     .prev_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .should_be_selected = true},
    // Prio HaTS, but another prio HaTS was shown quite recently: within the
    // non prio HaTS cooldown period, but already outside the prio HaTS
    // cooldown.
    {.prioritized = true,
     .prev_prio_hats =
         HatsNotificationController::kPrioritizedHatsThreshold + base::Days(1),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(1),
     .should_be_selected = true},
    // Prio HaTS, but a non prio HaTS was shown recently, though should not be
    // affected.
    {.prioritized = true,
     .prev_prio_hats = HatsNotificationController::kPrioritizedHatsThreshold +
                       base::Days(100),
     .prev_non_prio_hats =
         HatsNotificationController::kMinimumHatsThreshold + base::Days(1),
     .should_be_selected = true},
    // Prio HaTS, any other HaTS was shown very long time ago, but this prio
    // HaTS itself is still within its own threshold.
    {.prioritized = true,
     .prev_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .this_hats_recent = true,
     .should_be_selected = false},
    // Prio HaTS, but another prio HaTS was shown recently.
    {.prioritized = true,
     .prev_prio_hats =
         HatsNotificationController::kPrioritizedHatsThreshold - base::Days(1),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .should_be_selected = false},
    // Prio HaTS, but a non prio HaTS was shown just few hours ago.
    {.prioritized = true,
     .prev_prio_hats = HatsNotificationController::kPrioritizedHatsThreshold +
                       base::Days(100),
     .prev_non_prio_hats =
         HatsNotificationController::kMinimumHatsThreshold - base::Hours(3),
     .should_be_selected = false},
    // Non prio HaTS, any other HaTS was shown very long time ago.
    {.prioritized = false,
     .prev_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .should_be_selected = true},
    // Non prio HaTS, but a prio HaTS was shown quite recently.
    {.prioritized = false,
     .prev_prio_hats =
         HatsNotificationController::kMinimumHatsThreshold + base::Days(1),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(1),
     .should_be_selected = true},
    // Non prio HaTS, but another non prio HaTS was shown recently.
    {.prioritized = true,
     .prev_prio_hats = HatsNotificationController::kPrioritizedHatsThreshold +
                       base::Days(100),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold - base::Days(1),
     .should_be_selected = true},
    // Non prio HaTS, but a prio HaTS was shown just few hours ago.
    {.prioritized = true,
     .prev_prio_hats =
         HatsNotificationController::kMinimumHatsThreshold - base::Hours(1),
     .prev_non_prio_hats =
         HatsNotificationController::kHatsThreshold + base::Days(100),
     .should_be_selected = false},
};

INSTANTIATE_TEST_SUITE_P(HatsAllScenario,
                         HatsNotificationControllerTest,
                         testing::ValuesIn(kHatsScenario));

}  // namespace ash