#include "ash/system/palette/palette_welcome_bubble.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/system/palette/palette_tray.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/status_area_widget_test_helper.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/session_manager_types.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/controls/button/image_button.h"
namespace ash {
namespace {
constexpr char kUser1Email[] = "user1@palettewelcome.com";
constexpr char kUser2Email[] = "user2@palettewelcome.com";
constexpr char kPublicAccountEmail[] = "public@palettewelcome.com";
}
class PaletteWelcomeBubbleTest : public AshTestBase {
public:
PaletteWelcomeBubbleTest() = default;
PaletteWelcomeBubbleTest(const PaletteWelcomeBubbleTest&) = delete;
PaletteWelcomeBubbleTest& operator=(const PaletteWelcomeBubbleTest&) = delete;
~PaletteWelcomeBubbleTest() override = default;
PrefService* user1_pref_service() {
return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
AccountId::FromUserEmail(kUser1Email));
}
PrefService* user2_pref_service() {
return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
AccountId::FromUserEmail(kUser2Email));
}
void ShowBubble() { welcome_bubble_->Show(); }
void HideBubble() { welcome_bubble_->Hide(); }
void SetUp() override {
AshTestBase::SetUp();
welcome_bubble_ = std::make_unique<PaletteWelcomeBubble>(
StatusAreaWidgetTestHelper::GetStatusAreaWidget()->palette_tray());
SimulateUserLogin({kUser1Email});
SimulateUserLogin({kUser2Email});
GetSessionControllerClient()->SwitchActiveUser(
AccountId::FromUserEmail(kUser1Email));
}
void TearDown() override {
welcome_bubble_.reset();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<PaletteWelcomeBubble> welcome_bubble_;
};
TEST_F(PaletteWelcomeBubbleTest, Basic) {
EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
ShowBubble();
EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
HideBubble();
EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
EXPECT_TRUE(
user1_pref_service()->GetBoolean(prefs::kShownPaletteWelcomeBubble));
}
TEST_F(PaletteWelcomeBubbleTest, ShowIfNeeded) {
user1_pref_service()->SetBoolean(prefs::kShownPaletteWelcomeBubble, true);
welcome_bubble_->ShowIfNeeded();
EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
}
TEST_F(PaletteWelcomeBubbleTest, TapOutsideOfBubble) {
ShowBubble();
ASSERT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
auto bounds = welcome_bubble_->GetBubbleViewForTesting()->GetBoundsInScreen();
ASSERT_FALSE(bounds.IsEmpty());
GetEventGenerator()->set_current_screen_location(bounds.CenterPoint());
GetEventGenerator()->ClickLeftButton();
EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
ASSERT_FALSE(bounds.Contains(gfx::Point()));
GetEventGenerator()->set_current_screen_location(gfx::Point());
GetEventGenerator()->ClickLeftButton();
EXPECT_TRUE(
!welcome_bubble_->GetBubbleViewForTesting() ||
welcome_bubble_->GetBubbleViewForTesting()->GetWidget()->IsClosed());
}
TEST_F(PaletteWelcomeBubbleTest, BubbleShownForSecondUser) {
ShowBubble();
EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
HideBubble();
ASSERT_TRUE(
user1_pref_service()->GetBoolean(prefs::kShownPaletteWelcomeBubble));
GetSessionControllerClient()->SwitchActiveUser(
AccountId::FromUserEmail(kUser2Email));
EXPECT_FALSE(
user2_pref_service()->GetBoolean(prefs::kShownPaletteWelcomeBubble));
welcome_bubble_->ShowIfNeeded();
EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
}
TEST_F(PaletteWelcomeBubbleTest, BubbleNotShownInactiveSession) {
GetSessionControllerClient()->SetSessionState(
session_manager::SessionState::LOGGED_IN_NOT_ACTIVE);
welcome_bubble_->ShowIfNeeded();
EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
}
TEST_F(PaletteWelcomeBubbleTest, BubbleNotShownKiosk) {
ClearLogin();
SimulateKioskMode(user_manager::UserType::kKioskWebApp);
SetCanLockScreen(false);
welcome_bubble_->ShowIfNeeded();
EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
}
using PaletteWelcomeBubbleEmphemeralAccountTest = AshTestBase;
TEST_F(PaletteWelcomeBubbleEmphemeralAccountTest, BubbleNotShownForGuest) {
auto welcome_bubble = std::make_unique<PaletteWelcomeBubble>(
StatusAreaWidgetTestHelper::GetStatusAreaWidget()->palette_tray());
SimulateGuestLogin();
welcome_bubble->ShowIfNeeded();
EXPECT_FALSE(welcome_bubble->GetBubbleViewForTesting());
}
TEST_F(PaletteWelcomeBubbleEmphemeralAccountTest,
BubbleNotShownForPublicAccount) {
auto welcome_bubble = std::make_unique<PaletteWelcomeBubble>(
StatusAreaWidgetTestHelper::GetStatusAreaWidget()->palette_tray());
SimulateUserLogin(
{kPublicAccountEmail, user_manager::UserType::kPublicAccount});
welcome_bubble->ShowIfNeeded();
EXPECT_FALSE(welcome_bubble->GetBubbleViewForTesting());
}
}