File: hats_office_trigger_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 (351 lines) | stat: -rw-r--r-- 13,187 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
// 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 "chrome/browser/ui/webui/ash/cloud_upload/hats_office_trigger.h"

#include "ash/constants/ash_switches.h"
#include "ash/constants/web_app_id_constants.h"
#include "base/command_line.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/hats/hats_config.h"
#include "chrome/browser/ash/hats/hats_notification_controller.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/system_notification_helper.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/safe_browsing/url_lookup_service_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "components/services/app_service/public/cpp/instance.h"
#include "components/session_manager/core/session_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::cloud_upload {

namespace {
constexpr char kFakeUserEmail[] = "test-user@example.com";
}  // namespace

class HatsOfficeTriggerTestBase : public testing::Test {
 public:
  HatsOfficeTriggerTestBase() {
    scoped_feature_list_.InitAndEnableFeature(kHatsOfficeSurvey.feature);

    base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
    command_line->AppendSwitchASCII(
        ash::switches::kForceHappinessTrackingSystem,
        ::features::kHappinessTrackingOffice.name);
  }
  ~HatsOfficeTriggerTestBase() override = default;

  // testing::Test:
  void SetUp() override {
    user_manager_ = std::make_unique<FakeChromeUserManager>();
    user_manager_->Initialize();
    // Login user.
    user_manager_->AddUser(AccountId::FromUserEmail(kFakeUserEmail));
  }

  void TearDown() override {
    user_manager_->Destroy();
    user_manager_.reset();
  }

  bool IsDelayTriggerActive() {
    return hats_office_trigger_.IsDelayTriggerActiveForTesting();
  }

  bool IsAppStateTriggerActive() {
    return hats_office_trigger_.IsAppStateTriggerActiveForTesting();
  }

  const HatsNotificationController* GetHatsNotificationController() const {
    return hats_office_trigger_.GetHatsNotificationControllerForTesting();
  }

  base::Time Now() const { return task_environment_.GetMockClock()->Now(); }

  base::test::ScopedFeatureList scoped_feature_list_;
  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  session_manager::SessionManager session_manager_;
  std::unique_ptr<FakeChromeUserManager> user_manager_;

  HatsOfficeTrigger hats_office_trigger_;
};

class HatsOfficeTriggerTest : public HatsOfficeTriggerTestBase {
 public:
  void SetUp() override {
    HatsOfficeTriggerTestBase::SetUp();

    const AccountId account_id = AccountId::FromUserEmail(kFakeUserEmail);

    user_manager_->LoginUser(account_id);
    user_manager_->SwitchActiveUser(account_id);

    ASSERT_TRUE(test_profile_manager_.SetUp());
    // Use sign-in profile to simulate the login screen.
    profile_ = test_profile_manager_.CreateTestingProfile(kFakeUserEmail);
    session_manager_.SetSessionState(session_manager::SessionState::ACTIVE);

    TestingBrowserProcess::GetGlobal()->SetSystemNotificationHelper(
        std::make_unique<SystemNotificationHelper>());
    display_service_ =
        std::make_unique<NotificationDisplayServiceTester>(profile_);

    network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();

    task_environment_.RunUntilIdle();
  }

  void TearDown() override {
    // Remove the hats notification if it is showing. This allows the
    // scopedref-ed HatsNotificationController to get destroyed at the end of
    // the test.
    if (display_service_
            ->GetNotification(HatsNotificationController::kNotificationId)
            .has_value()) {
      display_service_->RemoveNotification(
          NotificationHandler::Type::TRANSIENT,
          HatsNotificationController::kNotificationId, /*by_user=*/true);
    }
    network_handler_test_helper_.reset();
    display_service_.reset();
    profile_ = nullptr;
    test_profile_manager_.DeleteAllTestingProfiles();

    HatsOfficeTriggerTestBase::TearDown();
  }

  bool IsHatsNotificationActive() const {
    return display_service_
        ->GetNotification(HatsNotificationController::kNotificationId)
        .has_value();
  }

  void OnTrackedDocsInstance(apps::InstanceState state) {
    apps::InstanceRegistry& registry =
        apps::AppServiceProxyFactory::GetForProfile(profile_)
            ->InstanceRegistry();
    auto instance = std::make_unique<apps::Instance>(
        ash::kGoogleDocsAppId, tracked_instance_id_, nullptr);
    instance->UpdateState(state, Now());
    registry.OnInstance(std::move(instance));
  }

  void OnIgnoredDocsInstance(apps::InstanceState state) {
    apps::InstanceRegistry& registry =
        apps::AppServiceProxyFactory::GetForProfile(profile_)
            ->InstanceRegistry();
    auto instance = std::make_unique<apps::Instance>(
        ash::kGoogleDocsAppId, ignored_instance_id_, nullptr);
    instance->UpdateState(state, Now());
    registry.OnInstance(std::move(instance));
  }

  TestingProfileManager test_profile_manager_{
      TestingBrowserProcess::GetGlobal()};
  raw_ptr<Profile> profile_ = nullptr;

  base::UnguessableToken tracked_instance_id_ =
      base::UnguessableToken::Create();
  base::UnguessableToken ignored_instance_id_ =
      base::UnguessableToken::Create();

  std::unique_ptr<NetworkHandlerTestHelper> network_handler_test_helper_;
  std::unique_ptr<NotificationDisplayServiceTester> display_service_;
};

class HatsOfficeTriggerLoginScreenTest : public HatsOfficeTriggerTestBase {
 public:
  void SetUp() override {
    HatsOfficeTriggerTestBase::SetUp();
    ash::ProfileHelper::Get();  // Instantiate ProfileHelper.
    session_manager_.SetSessionState(
        session_manager::SessionState::LOGIN_PRIMARY);
  }
};

TEST_F(HatsOfficeTriggerTest, ShowSurveyAfterDelaySuccess) {
  ASSERT_FALSE(IsHatsNotificationActive());

  base::test::TestFuture<void> future;
  display_service_->SetNotificationAddedClosure(future.GetRepeatingCallback());
  hats_office_trigger_.ShowSurveyAfterDelay(
      HatsOfficeLaunchingApp::kQuickOffice);

  ASSERT_TRUE(IsDelayTriggerActive());
  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  ASSERT_TRUE(future.Wait());

  ASSERT_FALSE(IsDelayTriggerActive());
  ASSERT_TRUE(GetHatsNotificationController());
  ASSERT_TRUE(IsHatsNotificationActive());
}

TEST_F(HatsOfficeTriggerTest, ShowSurveyAfterAppInactiveSuccess) {
  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  base::test::TestFuture<void> future;
  display_service_->SetNotificationAddedClosure(future.GetRepeatingCallback());
  hats_office_trigger_.ShowSurveyAfterAppInactive(
      ash::kGoogleDocsAppId, HatsOfficeLaunchingApp::kDrive);

  // Simulate receiving updates from an instance that shouldn't be tracked.
  OnIgnoredDocsInstance(
      apps::InstanceState(apps::kStarted | apps::kRunning | apps::kVisible));
  task_environment_.FastForwardBy(kDebounceDelay);
  // Simulate receiving instance updates. The first few updates, when the app is
  // not "active" yet, should be debounced.
  OnTrackedDocsInstance(apps::InstanceState(apps::kStarted | apps::kRunning));
  task_environment_.FastForwardBy(kDebounceDelay / 2);
  OnTrackedDocsInstance(
      apps::InstanceState(apps::kStarted | apps::kRunning | apps::kVisible));
  task_environment_.FastForwardBy(kDebounceDelay / 2);

  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  // Simulate the app being active. The survey shouldn't be triggered.
  OnTrackedDocsInstance(apps::InstanceState(apps::kStarted | apps::kRunning |
                                            apps::kVisible | apps::kActive));
  task_environment_.FastForwardBy(kDebounceDelay);

  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  // Simulate another update from the app that isn't tracked, it shouldn't
  // trigger the survey either.
  OnIgnoredDocsInstance(
      apps::InstanceState(apps::kStarted | apps::kRunning | apps::kVisible));
  task_environment_.FastForwardBy(kDebounceDelay);

  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  // Simulate the app being no longer active, it will this time trigger the
  // survey.
  OnTrackedDocsInstance(
      apps::InstanceState(apps::kStarted | apps::kRunning | apps::kVisible));

  ASSERT_FALSE(future.IsReady());
  task_environment_.FastForwardBy(kDebounceDelay);
  ASSERT_TRUE(future.IsReady());

  ASSERT_TRUE(GetHatsNotificationController());
  ASSERT_TRUE(IsHatsNotificationActive());
}

TEST_F(HatsOfficeTriggerTest, NoAppUpdateTimeout) {
  ASSERT_FALSE(IsAppStateTriggerActive());
  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  base::test::TestFuture<void> future;
  display_service_->SetNotificationAddedClosure(future.GetRepeatingCallback());

  hats_office_trigger_.ShowSurveyAfterAppInactive(
      ash::kGoogleDocsAppId, HatsOfficeLaunchingApp::kDrive);

  ASSERT_TRUE(IsAppStateTriggerActive());

  // The expected initial app event (started and running) isn't received during
  // the initial delay.
  OnTrackedDocsInstance(apps::kStarted);
  task_environment_.FastForwardBy(kFirstAppStateEventTimeout);

  ASSERT_FALSE(IsAppStateTriggerActive());
  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  // Simulate receiving the expected instance updates. They shouldn't trigger
  // the survey.
  OnTrackedDocsInstance(apps::InstanceState(apps::kStarted | apps::kRunning));
  task_environment_.FastForwardBy(kDebounceDelay / 2);
  // Running and active.
  OnTrackedDocsInstance(apps::InstanceState(apps::kStarted | apps::kRunning |
                                            apps::kVisible | apps::kActive));
  task_environment_.FastForwardBy(kDebounceDelay * 2);
  // Destroyed.
  OnTrackedDocsInstance(apps::kDestroyed);
  task_environment_.FastForwardBy(kDebounceDelay);

  ASSERT_FALSE(future.IsReady());
  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());
}

TEST_F(HatsOfficeTriggerTest, ShowSurveyOnlyOnce) {
  ASSERT_FALSE(IsHatsNotificationActive());

  // Show survey once
  base::test::TestFuture<void> future;
  display_service_->SetNotificationAddedClosure(future.GetRepeatingCallback());

  hats_office_trigger_.ShowSurveyAfterDelay(
      HatsOfficeLaunchingApp::kQuickOfficeClippyOff);

  ASSERT_TRUE(IsDelayTriggerActive());
  ASSERT_FALSE(IsHatsNotificationActive());
  ASSERT_FALSE(GetHatsNotificationController());

  ASSERT_TRUE(future.Wait());

  ASSERT_FALSE(IsDelayTriggerActive());
  const HatsNotificationController* hats_notification_controller =
      GetHatsNotificationController();
  EXPECT_NE(hats_notification_controller, nullptr);
  ASSERT_TRUE(IsHatsNotificationActive());

  // Trigger survey again but the controller shouldn't be a new instance.
  hats_office_trigger_.ShowSurveyAfterDelay(HatsOfficeLaunchingApp::kDrive);

  ASSERT_FALSE(IsDelayTriggerActive());
  EXPECT_EQ(hats_notification_controller, GetHatsNotificationController());
}

TEST_F(HatsOfficeTriggerLoginScreenTest, NoActiveUser) {
  hats_office_trigger_.ShowSurveyAfterDelay(HatsOfficeLaunchingApp::kMS365);
  ASSERT_FALSE(IsDelayTriggerActive());
  ASSERT_FALSE(GetHatsNotificationController());
}

TEST_F(HatsOfficeTriggerTest, NoSurveyForManagedProfile) {
  profile_->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);
  ASSERT_FALSE(IsHatsNotificationActive());

  hats_office_trigger_.ShowSurveyAfterDelay(HatsOfficeLaunchingApp::kMS365);
  ASSERT_FALSE(IsDelayTriggerActive());
  ASSERT_FALSE(GetHatsNotificationController());
}

TEST_F(HatsOfficeTriggerTest, NoSurveyIfSessionNotActive) {
  session_manager::SessionManager::Get()->SetSessionState(
      session_manager::SessionState::LOCKED);
  ASSERT_FALSE(IsHatsNotificationActive());

  hats_office_trigger_.ShowSurveyAfterDelay(HatsOfficeLaunchingApp::kMS365);

  ASSERT_TRUE(IsDelayTriggerActive());
  task_environment_.FastForwardBy(kDelayTriggerTimeout);
  ASSERT_FALSE(IsDelayTriggerActive());
  ASSERT_FALSE(GetHatsNotificationController());
}

}  // namespace ash::cloud_upload