#include "ash/detachable_base/detachable_base_notification_controller.h"
#include <string>
#include "ash/detachable_base/detachable_base_handler.h"
#include "ash/public/cpp/session/user_info.h"
#include "ash/session/session_controller_impl.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/time/time.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user_type.h"
#include "ui/message_center/message_center.h"
namespace ash {
UserInfo CreateTestUserInfo(const std::string& user_email) {
UserInfo user_info;
user_info.type = user_manager::UserType::kRegular;
user_info.account_id = AccountId::FromUserEmail(user_email);
user_info.display_name = "Test user";
user_info.display_email = user_email;
user_info.is_ephemeral = false;
user_info.is_new_profile = false;
return user_info;
}
class DetachableBaseNotificationControllerTest : public NoSessionAshTestBase {
public:
DetachableBaseNotificationControllerTest() = default;
DetachableBaseNotificationControllerTest(
const DetachableBaseNotificationControllerTest&) = delete;
DetachableBaseNotificationControllerTest& operator=(
const DetachableBaseNotificationControllerTest&) = delete;
~DetachableBaseNotificationControllerTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
chromeos::FakePowerManagerClient::Get()->SetTabletMode(
chromeos::PowerManagerClient::TabletMode::OFF, base::TimeTicks());
}
bool IsBaseChangedNotificationVisible() {
return message_center::MessageCenter::Get()->FindVisibleNotificationById(
DetachableBaseNotificationController::kBaseChangedNotificationId);
}
bool IsBaseRequiresUpdateNotificationVisible() {
return message_center::MessageCenter::Get()->FindVisibleNotificationById(
DetachableBaseNotificationController::
kBaseRequiresUpdateNotificationId);
}
void CloseBaseChangedNotification() {
message_center::MessageCenter::Get()->RemoveNotification(
DetachableBaseNotificationController::kBaseChangedNotificationId,
true );
}
DetachableBaseHandler* detachable_base_handler() {
return Shell::Get()->detachable_base_handler();
}
SessionControllerImpl* session_controller() {
return Shell::Get()->session_controller();
}
};
TEST_F(DetachableBaseNotificationControllerTest,
ShowPairingNotificationIfSessionNotBlocked) {
SimulateUserLogin(kRegularUserLoginInfo);
detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
EXPECT_TRUE(IsBaseChangedNotificationVisible());
CloseBaseChangedNotification();
EXPECT_FALSE(IsBaseChangedNotificationVisible());
detachable_base_handler()->PairChallengeSucceeded({0x03, 0x03});
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
ShowNotificationOnNonAuthenticatedBases) {
SimulateUserLogin(kRegularUserLoginInfo);
detachable_base_handler()->PairChallengeFailed();
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
UpdateNotificationOnUserSwitch) {
SimulateUserLogin(kRegularUserLoginInfo);
detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
SimulateUserLogin({"secondary_user@test.com"});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
EXPECT_TRUE(IsBaseChangedNotificationVisible());
CloseBaseChangedNotification();
GetSessionControllerClient()->SwitchActiveUser(
session_controller()->GetUserSession(1)->user_info.account_id);
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NonAuthenticatedBaseNotificationOnUserSwitch) {
SimulateUserLogin(kRegularUserLoginInfo);
detachable_base_handler()->PairChallengeFailed();
EXPECT_TRUE(IsBaseChangedNotificationVisible());
CloseBaseChangedNotification();
SimulateUserLogin({"secondary_user@test.com"});
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NoNotificationIfSessionNotStarted) {
const char kTestUser[] = "user_1@test.com";
UserInfo test_user_info = CreateTestUserInfo(kTestUser);
detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
EXPECT_TRUE(
detachable_base_handler()->SetPairedBaseAsLastUsedByUser(test_user_info));
detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
SimulateUserLogin({kTestUser});
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NoNotificationOnSessionStartIfBaseMarkedAsLastUsed) {
const char kTestUser[] = "user_1@test.com";
UserInfo test_user_info = CreateTestUserInfo(kTestUser);
detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
EXPECT_TRUE(
detachable_base_handler()->SetPairedBaseAsLastUsedByUser(test_user_info));
detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
EXPECT_TRUE(
detachable_base_handler()->SetPairedBaseAsLastUsedByUser(test_user_info));
SimulateUserLogin({kTestUser});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NonAuthenticatedBaseNotificationNotShownBeforeLogin) {
detachable_base_handler()->PairChallengeFailed();
EXPECT_FALSE(IsBaseChangedNotificationVisible());
SimulateUserLogin(kRegularUserLoginInfo);
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest, NoNotificationOnLockScreen) {
SimulateUserLogin(kRegularUserLoginInfo);
detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
UnblockUserSession();
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NoNotificationAfterLockScreenIfSetAsUsed) {
SimulateUserLogin(kRegularUserLoginInfo);
detachable_base_handler()->PairChallengeSucceeded({0x01, 0x01});
BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
detachable_base_handler()->PairChallengeSucceeded({0x02, 0x02});
EXPECT_FALSE(IsBaseChangedNotificationVisible());
EXPECT_TRUE(detachable_base_handler()->SetPairedBaseAsLastUsedByUser(
session_controller()->GetUserSession(0)->user_info));
UnblockUserSession();
EXPECT_FALSE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NonAuthenticatedBaseNotificationNotShownOnLock) {
ASSERT_TRUE(IsInSessionState(session_manager::SessionState::LOGIN_PRIMARY));
detachable_base_handler()->PairChallengeFailed();
EXPECT_FALSE(IsBaseChangedNotificationVisible());
SimulateUserLogin(kRegularUserLoginInfo);
EXPECT_TRUE(IsBaseChangedNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest, NotificationOnUpdateRequired) {
SimulateUserLogin(kRegularUserLoginInfo);
detachable_base_handler()->BaseFirmwareUpdateNeeded();
EXPECT_TRUE(IsBaseRequiresUpdateNotificationVisible());
chromeos::FakePowerManagerClient::Get()->SetTabletMode(
chromeos::PowerManagerClient::TabletMode::ON, base::TimeTicks());
EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NotificationOnUpdateRequiredBeforeLogin) {
detachable_base_handler()->BaseFirmwareUpdateNeeded();
EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
SimulateUserLogin(kRegularUserLoginInfo);
EXPECT_TRUE(IsBaseRequiresUpdateNotificationVisible());
chromeos::FakePowerManagerClient::Get()->SetTabletMode(
chromeos::PowerManagerClient::TabletMode::ON, base::TimeTicks());
EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
}
TEST_F(DetachableBaseNotificationControllerTest,
NotificationOnUpdateRequiredOnLockScreen) {
BlockUserSession(UserSessionBlockReason::BLOCKED_BY_LOCK_SCREEN);
detachable_base_handler()->BaseFirmwareUpdateNeeded();
EXPECT_TRUE(IsBaseRequiresUpdateNotificationVisible());
chromeos::FakePowerManagerClient::Get()->SetTabletMode(
chromeos::PowerManagerClient::TabletMode::ON, base::TimeTicks());
EXPECT_FALSE(IsBaseRequiresUpdateNotificationVisible());
}
}