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
|
// Copyright 2019 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/assistant/assistant_state_controller.h"
#include <memory>
#include <string>
#include "ash/assistant/assistant_controller_impl.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/ash/services/assistant/public/cpp/assistant_prefs.h"
#include "components/prefs/pref_service.h"
namespace ash {
namespace {
using assistant::prefs::AssistantOnboardingMode;
using assistant::prefs::ConsentStatus;
using assistant::prefs::kAssistantConsentStatus;
using assistant::prefs::kAssistantContextEnabled;
using assistant::prefs::kAssistantEnabled;
using assistant::prefs::kAssistantHotwordAlwaysOn;
using assistant::prefs::kAssistantHotwordEnabled;
using assistant::prefs::kAssistantLaunchWithMicOpen;
using assistant::prefs::kAssistantNotificationEnabled;
using assistant::prefs::kAssistantOnboardingMode;
using assistant::prefs::kAssistantOnboardingModeDefault;
using assistant::prefs::kAssistantOnboardingModeEducation;
class TestAssistantStateObserver : public AssistantStateObserver {
public:
TestAssistantStateObserver() = default;
TestAssistantStateObserver(const TestAssistantStateObserver&) = delete;
TestAssistantStateObserver& operator=(const TestAssistantStateObserver&) =
delete;
~TestAssistantStateObserver() override = default;
// AssistantStateObserver:
void OnAssistantConsentStatusChanged(int consent_status) override {
consent_status_ = consent_status;
}
void OnAssistantContextEnabled(bool context_enabled) override {
context_enabled_ = context_enabled;
}
void OnAssistantSettingsEnabled(bool settings_enabled) override {
settings_enabled_ = settings_enabled;
}
void OnAssistantHotwordAlwaysOn(bool hotword_always_on) override {
hotword_always_on_ = hotword_always_on;
}
void OnAssistantHotwordEnabled(bool hotword_enabled) override {
hotword_enabled_ = hotword_enabled;
}
void OnAssistantLaunchWithMicOpen(bool launch_with_mic_open) override {
launch_with_mic_open_ = launch_with_mic_open;
}
void OnAssistantNotificationEnabled(bool notification_enabled) override {
notification_enabled_ = notification_enabled;
}
void OnAssistantOnboardingModeChanged(
AssistantOnboardingMode onboarding_mode) override {
onboarding_mode_ = onboarding_mode;
}
int consent_status() const { return consent_status_; }
bool context_enabled() const { return context_enabled_; }
bool settings_enabled() const { return settings_enabled_; }
bool hotword_always_on() const { return hotword_always_on_; }
bool hotword_enabled() const { return hotword_enabled_; }
bool launch_with_mic_open() const { return launch_with_mic_open_; }
bool notification_enabled() const { return notification_enabled_; }
AssistantOnboardingMode onboarding_mode() const { return onboarding_mode_; }
private:
int consent_status_ = ConsentStatus::kUnknown;
bool context_enabled_ = false;
bool settings_enabled_ = false;
bool hotword_always_on_ = false;
bool hotword_enabled_ = false;
bool launch_with_mic_open_ = false;
bool notification_enabled_ = false;
AssistantOnboardingMode onboarding_mode_ = AssistantOnboardingMode::kDefault;
};
class AssistantStateControllerTest : public AshTestBase {
public:
AssistantStateControllerTest(const AssistantStateControllerTest&) = delete;
AssistantStateControllerTest& operator=(const AssistantStateControllerTest&) =
delete;
protected:
AssistantStateControllerTest() = default;
~AssistantStateControllerTest() override = default;
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
prefs_ = Shell::Get()->session_controller()->GetPrimaryUserPrefService();
DCHECK(prefs_);
observer_ = std::make_unique<TestAssistantStateObserver>();
}
PrefService* prefs() { return prefs_; }
TestAssistantStateObserver* observer() { return observer_.get(); }
private:
raw_ptr<PrefService, DanglingUntriaged> prefs_ = nullptr;
std::unique_ptr<TestAssistantStateObserver> observer_;
};
} // namespace
TEST_F(AssistantStateControllerTest, InitObserver) {
prefs()->SetInteger(kAssistantConsentStatus,
ConsentStatus::kActivityControlAccepted);
prefs()->SetBoolean(kAssistantContextEnabled, true);
prefs()->SetBoolean(kAssistantEnabled, true);
prefs()->SetBoolean(kAssistantHotwordAlwaysOn, true);
prefs()->SetBoolean(kAssistantHotwordEnabled, true);
prefs()->SetBoolean(kAssistantLaunchWithMicOpen, true);
prefs()->SetBoolean(kAssistantNotificationEnabled, true);
prefs()->SetString(kAssistantOnboardingMode, kAssistantOnboardingModeDefault);
// The observer class should get an instant notification about the current
// pref value.
AssistantState::Get()->AddObserver(observer());
EXPECT_EQ(observer()->consent_status(),
ConsentStatus::kActivityControlAccepted);
EXPECT_EQ(observer()->context_enabled(), true);
EXPECT_EQ(observer()->settings_enabled(), true);
EXPECT_EQ(observer()->hotword_always_on(), true);
EXPECT_EQ(observer()->hotword_enabled(), true);
EXPECT_EQ(observer()->launch_with_mic_open(), true);
EXPECT_EQ(observer()->notification_enabled(), true);
EXPECT_EQ(observer()->onboarding_mode(), AssistantOnboardingMode::kDefault);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifyConsentStatus) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetInteger(kAssistantConsentStatus, ConsentStatus::kUnauthorized);
EXPECT_EQ(observer()->consent_status(), ConsentStatus::kUnauthorized);
prefs()->SetInteger(kAssistantConsentStatus,
ConsentStatus::kActivityControlAccepted);
EXPECT_EQ(observer()->consent_status(),
ConsentStatus::kActivityControlAccepted);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifyContextEnabled) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetBoolean(kAssistantContextEnabled, false);
EXPECT_EQ(observer()->context_enabled(), false);
prefs()->SetBoolean(kAssistantContextEnabled, true);
EXPECT_EQ(observer()->context_enabled(), true);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifySettingsEnabled) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetBoolean(kAssistantEnabled, false);
EXPECT_EQ(observer()->settings_enabled(), false);
prefs()->SetBoolean(kAssistantEnabled, true);
EXPECT_EQ(observer()->settings_enabled(), true);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifyHotwordAlwaysOn) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetBoolean(kAssistantHotwordAlwaysOn, false);
EXPECT_EQ(observer()->hotword_always_on(), false);
prefs()->SetBoolean(kAssistantHotwordAlwaysOn, true);
EXPECT_EQ(observer()->hotword_always_on(), true);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifyHotwordEnabled) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetBoolean(kAssistantHotwordEnabled, false);
EXPECT_EQ(observer()->hotword_enabled(), false);
prefs()->SetBoolean(kAssistantHotwordEnabled, true);
EXPECT_EQ(observer()->hotword_enabled(), true);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifyLaunchWithMicOpen) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetBoolean(kAssistantLaunchWithMicOpen, false);
EXPECT_EQ(observer()->launch_with_mic_open(), false);
prefs()->SetBoolean(kAssistantLaunchWithMicOpen, true);
EXPECT_EQ(observer()->launch_with_mic_open(), true);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifyNotificationEnabled) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetBoolean(kAssistantNotificationEnabled, false);
EXPECT_EQ(observer()->notification_enabled(), false);
prefs()->SetBoolean(kAssistantNotificationEnabled, true);
EXPECT_EQ(observer()->notification_enabled(), true);
AssistantState::Get()->RemoveObserver(observer());
}
TEST_F(AssistantStateControllerTest, NotifyOnboardingModeChanged) {
AssistantState::Get()->AddObserver(observer());
prefs()->SetString(kAssistantOnboardingMode, kAssistantOnboardingModeDefault);
EXPECT_EQ(observer()->onboarding_mode(), AssistantOnboardingMode::kDefault);
prefs()->SetString(kAssistantOnboardingMode,
kAssistantOnboardingModeEducation);
EXPECT_EQ(observer()->onboarding_mode(), AssistantOnboardingMode::kEducation);
AssistantState::Get()->RemoveObserver(observer());
}
} // namespace ash
|