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
|
// Copyright 2025 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/lobster/lobster_service.h"
#include <memory>
#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/lobster/lobster_system_state.h"
#include "base/test/task_environment.h"
#include "base/types/cxx23_to_underlying.h"
#include "chrome/browser/ash/lobster/mock/mock_snapper_provider.h"
#include "chrome/browser/ash/lobster/mock_lobster_system_state_provider.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/test/base/chrome_ash_test_base.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/browser_context_helper/annotated_account_id.h"
#include "chromeos/ash/components/editor_menu/public/cpp/editor_consent_status.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_names.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class LobsterServiceTest : public ChromeAshTestBase {
public:
LobsterServiceTest() : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
~LobsterServiceTest() override = default;
void SetUp() override {
ChromeAshTestBase::SetUp();
ASSERT_TRUE(profile_manager_.SetUp());
auto* user = fake_user_manager_->AddUser(user_manager::StubAccountId());
fake_user_manager_->LoginUser(user->GetAccountId());
testing_profile_ = profile_manager_.CreateTestingProfile("profile");
ash::AnnotatedAccountId::Set(testing_profile_.get(), user->GetAccountId());
lobster_service_ = std::make_unique<LobsterService>(
std::make_unique<MockSnapperProvider>(), testing_profile_.get());
}
void TearDown() override {
ChromeAshTestBase::TearDown();
lobster_service_.reset();
testing_profile_ = nullptr;
profile_manager_.DeleteAllTestingProfiles();
fake_user_manager_.Reset();
}
TestingProfile* profile() { return testing_profile_; }
LobsterService* lobster_service() { return lobster_service_.get(); }
void ToggleLobsterSettings(bool value) {
profile()->GetPrefs()->SetBoolean(ash::prefs::kLobsterEnabled, value);
}
chromeos::editor_menu::EditorConsentStatus GetLobsterConsentStatus() {
return chromeos::editor_menu::GetConsentStatusFromInteger(
profile()->GetPrefs()->GetInteger(ash::prefs::kOrcaConsentStatus));
}
private:
raw_ptr<TestingProfile> testing_profile_ = nullptr;
TestingProfileManager profile_manager_;
std::unique_ptr<LobsterService> lobster_service_;
user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
fake_user_manager_{std::make_unique<ash::FakeChromeUserManager>()};
};
TEST_F(LobsterServiceTest,
SwitchingOnSettingToggleWillResetConsentWhichWasPreviouslyDeclined) {
ToggleLobsterSettings(false);
profile()->GetPrefs()->SetInteger(
ash::prefs::kOrcaConsentStatus,
base::to_underlying(
chromeos::editor_menu::EditorConsentStatus::kDeclined));
// Turn on the Lobster Settings
ToggleLobsterSettings(true);
EXPECT_EQ(GetLobsterConsentStatus(),
chromeos::editor_menu::EditorConsentStatus::kUnset);
}
TEST_F(LobsterServiceTest,
SwitchingOnSettingToggleWillNotResetConsentWhichWasPreviouslyApproved) {
ToggleLobsterSettings(true);
profile()->GetPrefs()->SetInteger(
ash::prefs::kOrcaConsentStatus,
base::to_underlying(
chromeos::editor_menu::EditorConsentStatus::kApproved));
// Turn off the Lobster Settings
ToggleLobsterSettings(false);
EXPECT_EQ(GetLobsterConsentStatus(),
chromeos::editor_menu::EditorConsentStatus::kApproved);
// Turn on the Lobster Settings
ToggleLobsterSettings(true);
EXPECT_EQ(GetLobsterConsentStatus(),
chromeos::editor_menu::EditorConsentStatus::kApproved);
}
TEST_F(LobsterServiceTest, CanNotShowLobsterFeatureSettingsToggle) {
std::unique_ptr<MockLobsterSystemStateProvider> mock_system_state_provider =
std::make_unique<MockLobsterSystemStateProvider>();
ON_CALL(*mock_system_state_provider, GetSystemState)
.WillByDefault(testing::Return(ash::LobsterSystemState(
ash::LobsterStatus::kBlocked, /*failed_checks=*/{
ash::LobsterSystemCheck::kInvalidAccountCapabilities,
ash::LobsterSystemCheck::kInvalidRegion})));
lobster_service()->set_lobster_system_state_provider_for_testing(
std::move(mock_system_state_provider));
EXPECT_FALSE(lobster_service()->CanShowFeatureSettingsToggle());
}
TEST_F(LobsterServiceTest, CanShowLobsterFeatureSettingsToggle) {
std::unique_ptr<MockLobsterSystemStateProvider> mock_system_state_provider =
std::make_unique<MockLobsterSystemStateProvider>();
ON_CALL(*mock_system_state_provider, GetSystemState)
.WillByDefault(testing::Return(ash::LobsterSystemState(
ash::LobsterStatus::kBlocked,
/*failed_checks=*/{ash::LobsterSystemCheck::kInvalidInputField,
ash::LobsterSystemCheck::kInvalidInputMethod,
ash::LobsterSystemCheck::kNoInternetConnection})));
lobster_service()->set_lobster_system_state_provider_for_testing(
std::move(mock_system_state_provider));
EXPECT_TRUE(lobster_service()->CanShowFeatureSettingsToggle());
}
} // namespace
|