File: magic_boost_state_ash_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 (406 lines) | stat: -rw-r--r-- 16,184 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
// 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/ash/magic_boost/magic_boost_state_ash.h"

#include <memory>

#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/types/cxx23_to_underlying.h"
#include "base/values.h"
#include "chrome/browser/ash/magic_boost/mock_editor_panel_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/components/magic_boost/public/cpp/magic_boost_state.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using chromeos::HMRConsentStatus;
using chromeos::MagicBoostState;

namespace ash {

namespace {

class TestMagicBoostStateObserver : public MagicBoostState::Observer {
 public:
  TestMagicBoostStateObserver() = default;

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

  ~TestMagicBoostStateObserver() override = default;

  // MagicBoostStateObserver:
  void OnHMREnabledUpdated(bool enabled) override { hmr_enabled_ = enabled; }

  void OnHMRConsentStatusUpdated(HMRConsentStatus status) override {
    hmr_consent_status_ = status;
  }

  void OnIsDeleting() override {
    // Do nothing as a user of `TestMagicBoostStateObserver` is responsible for
    // managing life cycle, i.e., stop observing before `MagicBoostState`
    // instance gets destructed.
  }

  bool hmr_enabled() const { return hmr_enabled_; }
  HMRConsentStatus hmr_consent_status() const { return hmr_consent_status_; }

 private:
  bool hmr_enabled_ = false;
  HMRConsentStatus hmr_consent_status_ = HMRConsentStatus::kUnset;
};

}  // namespace

class MagicBoostStateAshTest : public AshTestBase {
 protected:
  MagicBoostStateAshTest() = default;
  MagicBoostStateAshTest(const MagicBoostStateAshTest&) = delete;
  MagicBoostStateAshTest& operator=(const MagicBoostStateAshTest&) = delete;
  ~MagicBoostStateAshTest() override = default;

  // ChromeAshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();

    prefs_ = static_cast<TestingPrefServiceSimple*>(
        ash::Shell::Get()->session_controller()->GetPrimaryUserPrefService());

    magic_boost_state_ = std::make_unique<MagicBoostStateAsh>(
        base::BindRepeating([]() { return static_cast<Profile*>(nullptr); }));
    magic_boost_state_->set_editor_panel_manager_for_test(
        &mock_editor_manager_);

    observer_ = std::make_unique<TestMagicBoostStateObserver>();
  }

  void TearDown() override {
    observer_.reset();
    magic_boost_state_.reset();
    prefs_ = nullptr;
    AshTestBase::TearDown();
  }

  TestingPrefServiceSimple* prefs() { return prefs_; }

  TestMagicBoostStateObserver* observer() { return observer_.get(); }

  MagicBoostStateAsh* magic_boost_state() { return magic_boost_state_.get(); }

  MockEditorPanelManager& mock_editor_manager() { return mock_editor_manager_; }

 private:
  raw_ptr<TestingPrefServiceSimple> prefs_;
  std::unique_ptr<TestMagicBoostStateObserver> observer_;
  std::unique_ptr<MagicBoostStateAsh> magic_boost_state_;
  testing::NiceMock<MockEditorPanelManager> mock_editor_manager_;
};

TEST_F(MagicBoostStateAshTest, UpdateMagicBoostEnabledState) {
  MagicBoostState::Get()->AddObserver(observer());

  prefs()->SetBoolean(ash::prefs::kMagicBoostEnabled, false);

  // Both HMR and Orca should be disabled when `kMagicBoostEnabled` is false.
  EXPECT_FALSE(MagicBoostState::Get()->hmr_enabled().value());
  EXPECT_FALSE(prefs()->GetBoolean(ash::prefs::kOrcaEnabled));
  EXPECT_FALSE(prefs()->GetBoolean(ash::prefs::kLobsterEnabled));

  // The observer class should get a notification when the pref value
  // changes.
  MagicBoostState::Get()->AsyncWriteHMREnabled(false);
  EXPECT_FALSE(observer()->hmr_enabled());

  prefs()->SetBoolean(ash::prefs::kMagicBoostEnabled, true);

  // Both HMR and Orca should be enabled when `kMagicBoostEnabled` is true.
  EXPECT_TRUE(MagicBoostState::Get()->hmr_enabled().value());
  EXPECT_TRUE(prefs()->GetBoolean(ash::prefs::kOrcaEnabled));
  EXPECT_TRUE(prefs()->GetBoolean(ash::prefs::kLobsterEnabled));

  // The observer class should get a notification when the pref value
  // changes.
  EXPECT_TRUE(MagicBoostState::Get()->hmr_enabled().value());
  EXPECT_TRUE(observer()->hmr_enabled());

  MagicBoostState::Get()->RemoveObserver(observer());
}

TEST_F(MagicBoostStateAshTest, UpdateHMREnabledState) {
  MagicBoostState::Get()->AddObserver(observer());

  // The observer class should get an notification when the pref value
  // changes.
  MagicBoostState::Get()->AsyncWriteHMREnabled(false);
  EXPECT_FALSE(MagicBoostState::Get()->hmr_enabled().value());
  EXPECT_FALSE(observer()->hmr_enabled());

  // The observer class should get an notification when the pref value
  // changes.
  MagicBoostState::Get()->AsyncWriteHMREnabled(true);
  EXPECT_TRUE(MagicBoostState::Get()->hmr_enabled().value());
  EXPECT_TRUE(observer()->hmr_enabled());

  MagicBoostState::Get()->RemoveObserver(observer());
}

TEST_F(MagicBoostStateAshTest, UpdateHMRConsentStatus) {
  MagicBoostState::Get()->AddObserver(observer());

  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kUnset);
  EXPECT_EQ(observer()->hmr_consent_status(), HMRConsentStatus::kUnset);

  // The observer class should get an notification when the pref value
  // changes.
  prefs()->SetInteger(ash::prefs::kHMRConsentStatus,
                      base::to_underlying(HMRConsentStatus::kDeclined));

  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kDeclined);
  EXPECT_EQ(observer()->hmr_consent_status(), HMRConsentStatus::kDeclined);

  prefs()->SetInteger(ash::prefs::kHMRConsentStatus,
                      base::to_underlying(HMRConsentStatus::kApproved));
  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kApproved);
  EXPECT_EQ(observer()->hmr_consent_status(), HMRConsentStatus::kApproved);

  MagicBoostState::Get()->RemoveObserver(observer());
}

TEST_F(MagicBoostStateAshTest, UpdateHMRConsentStatusWhenEnableStateChanged) {
  MagicBoostState::Get()->AsyncWriteHMREnabled(false);
  MagicBoostState::Get()->AsyncWriteConsentStatus(HMRConsentStatus::kDeclined);

  // When consent status is `kDeclined` and enable state flip from false to
  // true (this can happen when flipping the toggle in Settings app), consent
  // status should be flip to `kPending` so that disclaimer UI can be shown when
  // accessing the feature.
  MagicBoostState::Get()->AsyncWriteHMREnabled(true);

  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kPendingDisclaimer);

  // Flipping back enable state from true to false should retain the consent
  // status value.
  MagicBoostState::Get()->AsyncWriteHMREnabled(false);

  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kPendingDisclaimer);

  MagicBoostState::Get()->AsyncWriteConsentStatus(HMRConsentStatus::kUnset);

  // When consent status is `kUnset` and enable state flip from false to
  // true (this can happen when flipping the toggle in Settings app), consent
  // status should be flip to `kPendingDisclaimer` so that disclaimer UI can be
  // shown when accessing the feature.
  MagicBoostState::Get()->AsyncWriteHMREnabled(true);

  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kPendingDisclaimer);

  // When consent status is `kApproved`, flipping enable state should not change
  // consent status state.
  MagicBoostState::Get()->AsyncWriteConsentStatus(HMRConsentStatus::kApproved);

  MagicBoostState::Get()->AsyncWriteHMREnabled(false);
  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kApproved);

  MagicBoostState::Get()->AsyncWriteHMREnabled(true);
  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_status(),
            HMRConsentStatus::kApproved);
}

TEST_F(MagicBoostStateAshTest, UpdateHMRConsentWindowDismissCount) {
  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_window_dismiss_count(), 0);

  prefs()->SetInteger(ash::prefs::kHMRConsentWindowDismissCount, 1);
  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_window_dismiss_count(), 1);

  prefs()->SetInteger(ash::prefs::kHMRConsentWindowDismissCount, 2);
  EXPECT_EQ(MagicBoostState::Get()->hmr_consent_window_dismiss_count(), 2);
}

TEST_F(MagicBoostStateAshTest, ShouldIncludeOrcaInOptInFunctionCall) {
  // `ShouldIncludeOrcaInOptIn` should fetch panel context from
  // `EditorPanelManagerImpl` to see if opt-in is needed for Orca.
  EXPECT_CALL(mock_editor_manager(), GetEditorPanelContext);
  magic_boost_state()->ShouldIncludeOrcaInOptIn(
      base::BindOnce([](bool result) {}));
  testing::Mock::VerifyAndClearExpectations(&mock_editor_manager());
}

TEST_F(MagicBoostStateAshTest, ShouldIncludeOrcaInOptInBlocked) {
  ON_CALL(mock_editor_manager(), GetEditorPanelContext)
      .WillByDefault(
          [](base::OnceCallback<void(
                 const chromeos::editor_menu::EditorContext&)> callback) {
            std::move(callback).Run(chromeos::editor_menu::EditorContext(
                chromeos::editor_menu::EditorMode::kHardBlocked,
                chromeos::editor_menu::EditorTextSelectionMode::kNoSelection,
                /*consent_status_settled=*/false, {}));
          });

  magic_boost_state()->ShouldIncludeOrcaInOptIn(base::BindOnce([](bool result) {
    // If `EditorPanelMode` is `kHardBlocked`, Orca should not be included in
    // opt-in flow.
    EXPECT_FALSE(result);
  }));
  testing::Mock::VerifyAndClearExpectations(&mock_editor_manager());
}

TEST_F(MagicBoostStateAshTest, ShouldIncludeOrcaInOptInConsentStatusSettled) {
  ON_CALL(mock_editor_manager(), GetEditorPanelContext)
      .WillByDefault(
          [](base::OnceCallback<void(
                 const chromeos::editor_menu::EditorContext&)> callback) {
            std::move(callback).Run(chromeos::editor_menu::EditorContext(
                chromeos::editor_menu::EditorMode::kWrite,
                chromeos::editor_menu::EditorTextSelectionMode::kNoSelection,
                /*consent_status_settled=*/true, {}));
          });

  magic_boost_state()->ShouldIncludeOrcaInOptIn(base::BindOnce([](bool result) {
    // If `consent_status_settled`, Orca should not be included in opt-in flow.
    EXPECT_FALSE(result);
  }));
  testing::Mock::VerifyAndClearExpectations(&mock_editor_manager());
}

TEST_F(MagicBoostStateAshTest,
       ShouldIncludeOrcaInOptInConsentStatusNotSettled) {
  ON_CALL(mock_editor_manager(), GetEditorPanelContext)
      .WillByDefault(
          [](base::OnceCallback<void(
                 const chromeos::editor_menu::EditorContext&)> callback) {
            std::move(callback).Run(chromeos::editor_menu::EditorContext(
                chromeos::editor_menu::EditorMode::kWrite,
                chromeos::editor_menu::EditorTextSelectionMode::kNoSelection,
                /*consent_status_settled=*/false, {}));
          });

  magic_boost_state()->ShouldIncludeOrcaInOptIn(base::BindOnce([](bool result) {
    // If `consent_status_settled` is false, Orca should be included in opt-in
    // flow.
    EXPECT_TRUE(result);
  }));
  testing::Mock::VerifyAndClearExpectations(&mock_editor_manager());
}

TEST_F(MagicBoostStateAshTest, DisableOrcaFeature) {
  // `DisableOrcaFeature` should trigger the correct functions from
  // `EditorPanelManagerImpl`.
  EXPECT_CALL(mock_editor_manager(), OnMagicBoostPromoCardDeclined);

  magic_boost_state()->DisableOrcaFeature();
  testing::Mock::VerifyAndClearExpectations(&mock_editor_manager());
}

TEST_F(MagicBoostStateAshTest, EnableOrcaFeature) {
  // `EnableOrcaFeature` should trigger the correct functions from
  // `EditorPanelManagerImpl`.
  EXPECT_CALL(mock_editor_manager(), OnConsentApproved);

  magic_boost_state()->EnableOrcaFeature();
  testing::Mock::VerifyAndClearExpectations(&mock_editor_manager());
}

TEST_F(MagicBoostStateAshTest, DisableLobsterSettings) {
  ASSERT_TRUE(prefs()->GetBoolean(ash::prefs::kLobsterEnabled));

  magic_boost_state()->DisableLobsterSettings();

  EXPECT_FALSE(prefs()->GetBoolean(ash::prefs::kLobsterEnabled));
}

struct MagicBoostHmrCardShowConditionTestCase {
  std::string test_name;
  bool magic_boost_revamp_enabled;
  HMRConsentStatus hmr_consent_status;
  bool expected_hmr_card_shown;
};

class MagicBoostHmrCardShowConditionTest
    : public MagicBoostStateAshTest,
      public testing::WithParamInterface<
          MagicBoostHmrCardShowConditionTestCase> {};

TEST_P(MagicBoostHmrCardShowConditionTest, ShouldShowHmrCard) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatureState(chromeos::features::kMagicBoostRevamp,
                                    GetParam().magic_boost_revamp_enabled);

  MagicBoostState::Get()->AsyncWriteConsentStatus(
      GetParam().hmr_consent_status);

  EXPECT_EQ(MagicBoostState::Get()->ShouldShowHmrCard(),
            GetParam().expected_hmr_card_shown);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    MagicBoostHmrCardShowConditionTest,
    testing::ValuesIn<MagicBoostHmrCardShowConditionTestCase>(
        {// magic_boost_revamp_enabled = false
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"NoMagicBoostRevamp_ConsentStatusUnset",
             /*magic_boost_revamp_enabled=*/false,
             /*hmr_consent_status=*/HMRConsentStatus::kUnset,
             /*expected_hmr_card_shown=*/false},
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"NoMagicBoostRevamp_ConsentStatusDeclined",
             /*magic_boost_revamp_enabled=*/false,
             /*hmr_consent_status=*/HMRConsentStatus::kDeclined,
             /*expected_hmr_card_shown=*/false},
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"NoMagicBoostRevamp_ConsentStatusApproved",
             /*magic_boost_revamp_enabled=*/false,
             /*hmr_consent_status=*/HMRConsentStatus::kApproved,
             /*expected_hmr_card_shown=*/true},
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"NoMagicBoostRevamp_"
                           "ConsentStatusPendingDisclaimer",
             /*magic_boost_revamp_enabled=*/false,
             /*hmr_consent_status=*/HMRConsentStatus::kPendingDisclaimer,
             /*expected_hmr_card_shown=*/true},

         // magic_boost_revamp_enabled = true
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"MagicBoostRevamp_ConsentStatusUnset",
             /*magic_boost_revamp_enabled=*/true,
             /*hmr_consent_status=*/HMRConsentStatus::kUnset,
             /*expected_hmr_card_shown=*/true},
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"MagicBoostRevamp_ConsentStatusDeclined",
             /*magic_boost_revamp_enabled=*/true,
             /*hmr_consent_status=*/HMRConsentStatus::kDeclined,
             /*expected_hmr_card_shown=*/false},
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"MagicBoostRevamp_ConsentStatusApproved",
             /*magic_boost_revamp_enabled=*/true,
             /*hmr_consent_status=*/HMRConsentStatus::kApproved,
             /*expected_hmr_card_shown=*/true},
         MagicBoostHmrCardShowConditionTestCase{
             /*test_name=*/"MagicBoostRevamp_"
                           "ConsentStatusPendingDisclaimer",
             /*magic_boost_revamp_enabled=*/true,
             /*hmr_consent_status=*/HMRConsentStatus::kPendingDisclaimer,
             /*expected_hmr_card_shown=*/true}}),
    [](const testing::TestParamInfo<
        MagicBoostHmrCardShowConditionTest::ParamType>& info) {
      return info.param.test_name;
    });

}  // namespace ash