File: arc_vm_data_migration_notifier_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 (326 lines) | stat: -rw-r--r-- 14,298 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
// Copyright 2022 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/arc/notification/arc_vm_data_migration_notifier.h"

#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ash/arc/session/arc_session_manager.h"
#include "chrome/browser/ash/arc/test/test_arc_session_manager.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/policy/profile_policy_connector.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/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/session/arc_session_runner.h"
#include "chromeos/ash/experiences/arc/session/arc_vm_data_migration_status.h"
#include "chromeos/ash/experiences/arc/test/fake_arc_session.h"
#include "components/user_manager/scoped_user_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace arc {

namespace {

constexpr char kProfileName[] = "user@gmail.com";
constexpr GaiaId::Literal kGaiaId("1234567890");

constexpr char kNotificationId[] = "arc_vm_data_migration_notification";

class ArcVmDataMigrationNotifierTest : public ash::AshTestBase {
 public:
  ArcVmDataMigrationNotifierTest()
      : ash::AshTestBase(std::unique_ptr<base::test::TaskEnvironment>(
            std::make_unique<content::BrowserTaskEnvironment>())) {
    base::CommandLine::ForCurrentProcess()->InitFromArgv(
        {"", "--arc-availability=officially-supported", "--enable-arcvm"});
  }

  ~ArcVmDataMigrationNotifierTest() override = default;

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

  void SetUp() override {
    ash::AshTestBase::SetUp();
    ash::ConciergeClient::InitializeFake();
    ArcSessionManager::SetUiEnabledForTesting(false);
    arc_session_manager_ =
        CreateTestArcSessionManager(std::make_unique<ArcSessionRunner>(
            base::BindRepeating(FakeArcSession::Create)));

    fake_user_manager_.Reset(std::make_unique<ash::FakeChromeUserManager>());
    profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    ASSERT_TRUE(profile_manager_->SetUp());
    testing_profile_ = profile_manager_->CreateTestingProfile(kProfileName);
    const AccountId account_id = AccountId::FromUserEmailGaiaId(
        testing_profile_->GetProfileUserName(), kGaiaId);
    fake_user_manager_->AddUser(account_id);
    fake_user_manager_->LoginUser(account_id);
    DCHECK(ash::ProfileHelper::IsPrimaryProfile(testing_profile_));

    notification_tester_ =
        std::make_unique<NotificationDisplayServiceTester>(testing_profile_);
    arc_vm_data_migration_notifier_ =
        std::make_unique<ArcVmDataMigrationNotifier>(testing_profile_);

    arc_session_manager_->SetProfile(testing_profile_);
    arc_session_manager_->Initialize();
  }

  void TearDown() override {
    arc_session_manager_->Shutdown();
    notification_tester_.reset();
    profile_manager_->DeleteTestingProfile(kProfileName);
    testing_profile_ = nullptr;
    profile_manager_.reset();
    fake_user_manager_.Reset();
    arc_vm_data_migration_notifier_.reset();
    arc_session_manager_.reset();
    ash::ConciergeClient::Shutdown();
    ash::AshTestBase::TearDown();
  }

  ArcSessionManager* arc_session_manager() {
    return arc_session_manager_.get();
  }

  NotificationDisplayServiceTester* notification_tester() {
    return notification_tester_.get();
  }

  TestingProfile* profile() { return testing_profile_; }

 private:
  std::unique_ptr<ArcSessionManager> arc_session_manager_;
  std::unique_ptr<ArcVmDataMigrationNotifier> arc_vm_data_migration_notifier_;
  user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
      fake_user_manager_;
  std::unique_ptr<TestingProfileManager> profile_manager_;
  raw_ptr<TestingProfile, DanglingUntriaged> testing_profile_ =
      nullptr;  // Owned by |profile_manager_|.
  std::unique_ptr<NotificationDisplayServiceTester> notification_tester_;
};

// Tests that no notification is shown when the migration is disabled.
TEST_F(ArcVmDataMigrationNotifierTest, MigrationDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(kEnableArcVmDataMigration);

  arc_session_manager()->StartArcForTesting();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
  // TODO(b/258278176): Use GetArcVmDataMigrationStatus() and stop using
  // Yoda-style comparisons. The same goes for other test cases.
  EXPECT_EQ(
      ArcVmDataMigrationStatus::kUnnotified,
      static_cast<ArcVmDataMigrationStatus>(
          profile()->GetPrefs()->GetInteger(prefs::kArcVmDataMigrationStatus)));
}

// Tests that no notification is shown for managed users even when the migration
// is enabled via the feature and when the policy is not set.
TEST_F(ArcVmDataMigrationNotifierTest, AccountManagedDefault) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);

  arc_session_manager()->StartArcForTesting();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(GetArcVmDataMigrationStatus(profile()->GetPrefs()),
            ArcVmDataMigrationStatus::kUnnotified);
}

// Tests that no notification is shown for managed users when the migration is
// enabled via the featuer and when the policy is set to not prompt.
TEST_F(ArcVmDataMigrationNotifierTest, AccountManagedDoNotPrompt) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);
  profile()->GetPrefs()->SetInteger(
      prefs::kArcVmDataMigrationStrategy,
      static_cast<int>(ArcVmDataMigrationStrategy::kDoNotPrompt));

  arc_session_manager()->StartArcForTesting();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(GetArcVmDataMigrationStatus(profile()->GetPrefs()),
            ArcVmDataMigrationStatus::kUnnotified);
}

// Tests that no notification is shown for managed users when the migration is
// enabled via the featuer and when the policy is set to not prompt and already
// started.
TEST_F(ArcVmDataMigrationNotifierTest, AccountManagedPromptAndStarted) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);
  auto* prefs = profile()->GetPrefs();
  SetArcVmDataMigrationStatus(prefs, ArcVmDataMigrationStatus::kStarted);
  profile()->GetPrefs()->SetInteger(
      prefs::kArcVmDataMigrationStrategy,
      static_cast<int>(ArcVmDataMigrationStrategy::kPrompt));

  arc_session_manager()->StartArcForTesting();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(GetArcVmDataMigrationStatus(profile()->GetPrefs()),
            ArcVmDataMigrationStatus::kStarted);
}

// Tests that no notification is shown for managed users when the migration is
// enabled via the featuer and when the policy is set to not prompti and
// already finished.
TEST_F(ArcVmDataMigrationNotifierTest, AccountManagedPromptAndFinished) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);
  auto* prefs = profile()->GetPrefs();
  SetArcVmDataMigrationStatus(prefs, ArcVmDataMigrationStatus::kFinished);
  profile()->GetPrefs()->SetInteger(
      prefs::kArcVmDataMigrationStrategy,
      static_cast<int>(ArcVmDataMigrationStrategy::kPrompt));

  arc_session_manager()->StartArcForTesting();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(GetArcVmDataMigrationStatus(profile()->GetPrefs()),
            ArcVmDataMigrationStatus::kFinished);
}

// Tests that notification is shown for managed users when the migration is
// enabled via the feature and when the policy is set to prompt.
TEST_F(ArcVmDataMigrationNotifierTest, AccountManagedPrompt) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);
  profile()->GetPrefs()->SetInteger(
      prefs::kArcVmDataMigrationStrategy,
      static_cast<int>(ArcVmDataMigrationStrategy::kPrompt));

  arc_session_manager()->StartArcForTesting();
  EXPECT_TRUE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(GetArcVmDataMigrationStatus(profile()->GetPrefs()),
            ArcVmDataMigrationStatus::kNotified);
}

// Tests that a notification is shown when the migration is enabled but not
// started nor finished yet.
TEST_F(ArcVmDataMigrationNotifierTest, MigrationEnabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);

  arc_session_manager()->StartArcForTesting();
  EXPECT_TRUE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(
      ArcVmDataMigrationStatus::kNotified,
      static_cast<ArcVmDataMigrationStatus>(
          profile()->GetPrefs()->GetInteger(prefs::kArcVmDataMigrationStatus)));
}

// Tests that a notification is shown when the user has been notified of the
// migration but not started the migration yet.
TEST_F(ArcVmDataMigrationNotifierTest, MigrationNotified) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetPrefs()->SetInteger(
      prefs::kArcVmDataMigrationStatus,
      static_cast<int>(ArcVmDataMigrationStatus::kNotified));

  arc_session_manager()->StartArcForTesting();
  EXPECT_TRUE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(
      ArcVmDataMigrationStatus::kNotified,
      static_cast<ArcVmDataMigrationStatus>(
          profile()->GetPrefs()->GetInteger(prefs::kArcVmDataMigrationStatus)));
}

// Tests that a notification is shown when the user has confirmed the migration
// but ARC session is started without entering the migration screen.
TEST_F(ArcVmDataMigrationNotifierTest, MigrationConfirmed) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetPrefs()->SetInteger(
      prefs::kArcVmDataMigrationStatus,
      static_cast<int>(ArcVmDataMigrationStatus::kConfirmed));

  arc_session_manager()->StartArcForTesting();
  EXPECT_TRUE(notification_tester()->GetNotification(kNotificationId));
  // The migration status is set back to kNotified.
  EXPECT_EQ(
      ArcVmDataMigrationStatus::kNotified,
      static_cast<ArcVmDataMigrationStatus>(
          profile()->GetPrefs()->GetInteger(prefs::kArcVmDataMigrationStatus)));
}

// Tests that no notification is shown once the migration has started but the
// maximum number of auto-resumes has not been reached yet.
TEST_F(ArcVmDataMigrationNotifierTest, MigrationStarted) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  auto* prefs = profile()->GetPrefs();
  SetArcVmDataMigrationStatus(prefs, ArcVmDataMigrationStatus::kStarted);
  prefs->SetInteger(prefs::kArcVmDataMigrationAutoResumeCount, 0);

  arc_session_manager()->RequestEnable();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(GetArcVmDataMigrationStatus(prefs),
            ArcVmDataMigrationStatus::kStarted);
}

// Tests that a notification is shown when the migration has started and the
// maximum number of auto-resumes has been reached.
TEST_F(ArcVmDataMigrationNotifierTest, MaxNumberOfAutoResumesReached) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  auto* prefs = profile()->GetPrefs();
  SetArcVmDataMigrationStatus(prefs, ArcVmDataMigrationStatus::kStarted);
  prefs->SetInteger(prefs::kArcVmDataMigrationAutoResumeCount,
                    kArcVmDataMigrationMaxAutoResumeCount + 1);

  arc_session_manager()->RequestEnable();
  EXPECT_TRUE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(GetArcVmDataMigrationStatus(prefs),
            ArcVmDataMigrationStatus::kStarted);
}

// Tests that no notification is shown once the migration has finished.
TEST_F(ArcVmDataMigrationNotifierTest, MigrationFinished) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(kEnableArcVmDataMigration);
  profile()->GetPrefs()->SetInteger(
      prefs::kArcVmDataMigrationStatus,
      static_cast<int>(ArcVmDataMigrationStatus::kFinished));

  arc_session_manager()->StartArcForTesting();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
  EXPECT_EQ(
      ArcVmDataMigrationStatus::kFinished,
      static_cast<ArcVmDataMigrationStatus>(
          profile()->GetPrefs()->GetInteger(prefs::kArcVmDataMigrationStatus)));
}

// Tests that no notification is shown even when the migration is enabled if
// virtio-blk /data is forcibly enabled via kEnableVirtioBlkForData.
TEST_F(ArcVmDataMigrationNotifierTest, VirtioBlkDataForced) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(
      {kEnableArcVmDataMigration, kEnableVirtioBlkForData}, {});

  arc_session_manager()->StartArcForTesting();
  EXPECT_FALSE(notification_tester()->GetNotification(kNotificationId));
}

}  // namespace

}  // namespace arc