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
|
// 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 "ash/system/mahi/mahi_nudge_controller.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/mahi/mahi_constants.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "ash/test/ash_test_base.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
#include "chromeos/components/magic_boost/public/cpp/magic_boost_state.h"
#include "components/prefs/pref_service.h"
namespace ash {
namespace {
bool IsMahiNudgeShown() {
return Shell::Get()->anchored_nudge_manager()->IsNudgeShown(
mahi_constants::kMahiNudgeId);
}
// A class that mocks `MagicBoostState` to use in tests.
class TestMagicBoostState : public chromeos::MagicBoostState {
public:
TestMagicBoostState() { UpdateMagicBoostAvailable(true); }
TestMagicBoostState(const TestMagicBoostState&) = delete;
TestMagicBoostState& operator=(const TestMagicBoostState&) = delete;
~TestMagicBoostState() override = default;
// chromeos::MagicBoostState:
void AsyncWriteConsentStatus(
chromeos::HMRConsentStatus consent_status) override {
UpdateHMRConsentStatus(consent_status);
}
void AsyncWriteHMREnabled(bool enabled) override {
UpdateHMREnabled(enabled);
}
bool ShouldIncludeOrcaInOptInSync() override { return false; }
bool CanShowNoticeBannerForHMR() override { return false; }
int32_t AsyncIncrementHMRConsentWindowDismissCount() override { return 0; }
void DisableOrcaFeature() override {}
void DisableLobsterSettings() override {}
protected:
base::expected<bool, chromeos::MagicBoostState::Error>
IsMagicBoostAvailableExpected() const override {
return true;
}
};
} // namespace
class MahiNudgeControllerTest : public AshTestBase {
public:
MahiNudgeControllerTest() {
mahi_nudge_controller_ = std::make_unique<MahiNudgeController>();
test_magic_boost_state_.AsyncWriteConsentStatus(
chromeos::HMRConsentStatus::kUnset);
}
MahiNudgeController* nudge_controller() {
return mahi_nudge_controller_.get();
}
AnchoredNudgeManagerImpl* nudge_manager() {
return Shell::Get()->anchored_nudge_manager();
}
static base::Time GetTestTime() { return test_time_; }
static void SetTestTime(base::Time test_time) { test_time_ = test_time; }
protected:
TestMagicBoostState test_magic_boost_state_;
private:
std::unique_ptr<MahiNudgeController> mahi_nudge_controller_;
static inline base::Time test_time_;
};
// Tests that the nudge can show when the user is not opted into Mahi.
TEST_F(MahiNudgeControllerTest, NudgeShows_WhenUserPrefNotEnabled) {
test_magic_boost_state_.AsyncWriteHMREnabled(false);
EXPECT_FALSE(IsMahiNudgeShown());
nudge_controller()->MaybeShowNudge();
EXPECT_TRUE(IsMahiNudgeShown());
}
// Tests that the nudge won't show when the user has opted into Mahi.
TEST_F(MahiNudgeControllerTest, NudgeDoesNotShow_WhenUserPrefEnabled) {
test_magic_boost_state_.AsyncWriteHMREnabled(true);
EXPECT_FALSE(IsMahiNudgeShown());
nudge_controller()->MaybeShowNudge();
EXPECT_FALSE(IsMahiNudgeShown());
}
// Tests that the nudge can show when the consent status is unset.
TEST_F(MahiNudgeControllerTest, NudgeShows_WhenConsentStatusIsUnset) {
test_magic_boost_state_.AsyncWriteHMREnabled(false);
test_magic_boost_state_.AsyncWriteConsentStatus(
chromeos::HMRConsentStatus::kUnset);
EXPECT_FALSE(IsMahiNudgeShown());
nudge_controller()->MaybeShowNudge();
EXPECT_TRUE(IsMahiNudgeShown());
}
// Tests that the nudge won't show when the consent status is set.
TEST_F(MahiNudgeControllerTest, NudgeDoesNotShow_WhenConsentStatusSet) {
test_magic_boost_state_.AsyncWriteHMREnabled(false);
test_magic_boost_state_.AsyncWriteConsentStatus(
chromeos::HMRConsentStatus::kPendingDisclaimer);
EXPECT_FALSE(IsMahiNudgeShown());
nudge_controller()->MaybeShowNudge();
EXPECT_FALSE(IsMahiNudgeShown());
test_magic_boost_state_.AsyncWriteConsentStatus(
chromeos::HMRConsentStatus::kApproved);
EXPECT_FALSE(IsMahiNudgeShown());
nudge_controller()->MaybeShowNudge();
EXPECT_FALSE(IsMahiNudgeShown());
test_magic_boost_state_.AsyncWriteConsentStatus(
chromeos::HMRConsentStatus::kDeclined);
EXPECT_FALSE(IsMahiNudgeShown());
nudge_controller()->MaybeShowNudge();
EXPECT_FALSE(IsMahiNudgeShown());
}
// Tests that the nudge won't show if the time between shown threshold hasn't
// passed since it was last shown.
TEST_F(MahiNudgeControllerTest, NudgeDoesNotShow_IfRecentlyShown) {
test_magic_boost_state_.AsyncWriteHMREnabled(false);
SetTestTime(base::Time::Now());
base::subtle::ScopedTimeClockOverrides clock_override(
/*time_override=*/&MahiNudgeControllerTest::GetTestTime,
/*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);
// Show the nudge once and close it.
EXPECT_FALSE(IsMahiNudgeShown());
nudge_controller()->MaybeShowNudge();
EXPECT_TRUE(IsMahiNudgeShown());
nudge_manager()->Cancel(mahi_constants::kMahiNudgeId);
// Attempt showing the nudge again immediately. It should not show.
nudge_controller()->MaybeShowNudge();
EXPECT_FALSE(IsMahiNudgeShown());
// Attempt showing the nudge after some time but before its threshold time has
// fully passed. It should not show.
SetTestTime(GetTestTime() + mahi_constants::kNudgeTimeBetweenShown -
base::Hours(1));
nudge_controller()->MaybeShowNudge();
EXPECT_FALSE(IsMahiNudgeShown());
// Attempt showing the nudge after its "time between shown" threshold has
// passed. It should show.
SetTestTime(GetTestTime() + mahi_constants::kNudgeTimeBetweenShown +
base::Hours(1));
nudge_controller()->MaybeShowNudge();
EXPECT_TRUE(IsMahiNudgeShown());
}
// Tests that the nudge won't show if it has been shown its max number of times.
TEST_F(MahiNudgeControllerTest, NudgeDoesNotShow_IfMaxTimesShown) {
test_magic_boost_state_.AsyncWriteHMREnabled(false);
SetTestTime(base::Time::Now());
base::subtle::ScopedTimeClockOverrides clock_override(
/*time_override=*/&MahiNudgeControllerTest::GetTestTime,
/*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);
// Show the nudge its max number of times.
for (int i = 0; i < mahi_constants::kNudgeMaxShownCount; i++) {
nudge_controller()->MaybeShowNudge();
EXPECT_TRUE(IsMahiNudgeShown());
nudge_manager()->Cancel(mahi_constants::kMahiNudgeId);
SetTestTime(GetTestTime() + mahi_constants::kNudgeTimeBetweenShown +
base::Hours(1));
}
// Attempt showing the nudge once more. It should not show.
nudge_controller()->MaybeShowNudge();
EXPECT_FALSE(IsMahiNudgeShown());
}
} // namespace ash
|