Skip to content

Commit

Permalink
oobe: Align UserAddingScreen tests to the user flow
Browse files Browse the repository at this point in the history
Interact with html elements, native buttons.

Fixed: 944173
Change-Id: Ic3fce574de9b230917be2ddb29c71e7647961f38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2113496
Commit-Queue: Roman Sorokin [CET] <rsorokin@chromium.org>
Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
Reviewed-by: Denis Kuznetsov [CET] <antrim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#752897}
  • Loading branch information
Roman Sorokin authored and Commit Bot committed Mar 24, 2020
1 parent 2e5fcf6 commit d2c5628
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 49 deletions.
17 changes: 15 additions & 2 deletions ash/login/login_screen_test_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ bool LoginScreenTestApi::IsAddUserButtonShown() {
return IsLoginShelfViewButtonShown(LoginShelfView::kAddUser);
}

// static
bool LoginScreenTestApi::IsCancelButtonShown() {
return IsLoginShelfViewButtonShown(LoginShelfView::kCancel);
}

// static
bool LoginScreenTestApi::IsParentAccessButtonShown() {
return IsLoginShelfViewButtonShown(LoginShelfView::kParentAccess);
Expand Down Expand Up @@ -204,13 +209,21 @@ bool LoginScreenTestApi::LaunchApp(const std::string& app_id) {
// static
bool LoginScreenTestApi::ClickAddUserButton() {
LoginShelfView* view = GetLoginShelfView();
return view && view->SimulateAddUserButtonForTesting();
return view &&
view->SimulateButtonPressedForTesting(LoginShelfView::kAddUser);
}

// static
bool LoginScreenTestApi::ClickCancelButton() {
LoginShelfView* view = GetLoginShelfView();
return view && view->SimulateButtonPressedForTesting(LoginShelfView::kCancel);
}

// static
bool LoginScreenTestApi::ClickGuestButton() {
LoginShelfView* view = GetLoginShelfView();
return view && view->SimulateGuestButtonForTesting();
return view &&
view->SimulateButtonPressedForTesting(LoginShelfView::kBrowseAsGuest);
}

// static
Expand Down
2 changes: 2 additions & 0 deletions ash/public/cpp/login_screen_test_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ class ASH_PUBLIC_EXPORT LoginScreenTestApi {
static bool IsAuthErrorBubbleShown();
static bool IsGuestButtonShown();
static bool IsAddUserButtonShown();
static bool IsCancelButtonShown();
static bool IsParentAccessButtonShown();
static void SubmitPassword(const AccountId& account_id,
const std::string& password);
static int64_t GetUiUpdateCount();
static bool LaunchApp(const std::string& app_id);
static bool ClickAddUserButton();
static bool ClickCancelButton();
static bool ClickGuestButton();
static bool WaitForUiUpdate(int64_t previous_update_count);
static int GetUsersCount();
Expand Down
21 changes: 8 additions & 13 deletions ash/shelf/login_shelf_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "ui/base/models/menu_model.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/rect.h"
Expand Down Expand Up @@ -573,21 +574,15 @@ bool LoginShelfView::LaunchAppForTesting(const std::string& app_id) {
kiosk_apps_button_->LaunchAppForTesting(app_id);
}

bool LoginShelfView::SimulateAddUserButtonForTesting() {
views::View* add_user_button = GetViewByID(kAddUser);
if (!add_user_button->GetEnabled())
bool LoginShelfView::SimulateButtonPressedForTesting(
LoginShelfView::ButtonId button_id) {
views::View* button = GetViewByID(button_id);
if (!button->GetEnabled())
return false;

StartAddUser();
return true;
}

bool LoginShelfView::SimulateGuestButtonForTesting() {
views::View* guest_login_button = GetViewByID(kBrowseAsGuest);
if (!guest_login_button->GetEnabled())
return false;

Shell::Get()->login_screen_controller()->LoginAsGuest();
ButtonPressed(static_cast<LoginShelfButton*>(button),
ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::PointF(),
gfx::PointF(), base::TimeTicks(), 0, 0));
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions ash/shelf/login_shelf_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ class ASH_EXPORT LoginShelfView : public views::View,
// Test API. Returns true if request was successful (i.e. button was
// clickable).
bool LaunchAppForTesting(const std::string& app_id);
bool SimulateAddUserButtonForTesting();
bool SimulateGuestButtonForTesting();
bool SimulateButtonPressedForTesting(ButtonId button);

// Adds test delegate. Delegate will become owned by LoginShelfView.
void InstallTestUiUpdateDelegate(
Expand Down
40 changes: 21 additions & 19 deletions chrome/browser/chromeos/login/login_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,6 @@

namespace chromeos {

namespace {

UserContext CreateUserContext(const AccountId& account_id) {
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR,
account_id);
user_context.SetKey(Key("password"));
if (account_id.GetUserEmail() == FakeGaiaMixin::kEnterpriseUser1) {
user_context.SetRefreshToken(FakeGaiaMixin::kTestRefreshToken1);
} else if (account_id.GetUserEmail() == FakeGaiaMixin::kEnterpriseUser2) {
user_context.SetRefreshToken(FakeGaiaMixin::kTestRefreshToken2);
}
return user_context;
}

} // namespace

LoginManagerTest::LoginManagerTest(bool should_launch_browser,
bool should_initialize_webui)
: should_launch_browser_(should_launch_browser),
Expand Down Expand Up @@ -93,8 +77,26 @@ void LoginManagerTest::RegisterUser(const AccountId& account_id) {
ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers");
users_pref->AppendIfNotPresent(
std::make_unique<base::Value>(account_id.GetUserEmail()));
if (user_manager::UserManager::IsInitialized())
if (user_manager::UserManager::IsInitialized()) {
user_manager::known_user::SaveKnownUser(account_id);
user_manager::UserManager::Get()->SaveUserOAuthStatus(
account_id, user_manager::User::OAUTH2_TOKEN_STATUS_VALID);
}
}

constexpr char LoginManagerTest::kPassword[] = "password";

UserContext LoginManagerTest::CreateUserContext(const AccountId& account_id,
const std::string& password) {
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR,
account_id);
user_context.SetKey(Key(password));
if (account_id.GetUserEmail() == FakeGaiaMixin::kEnterpriseUser1) {
user_context.SetRefreshToken(FakeGaiaMixin::kTestRefreshToken1);
} else if (account_id.GetUserEmail() == FakeGaiaMixin::kEnterpriseUser2) {
user_context.SetRefreshToken(FakeGaiaMixin::kTestRefreshToken2);
}
return user_context;
}

void LoginManagerTest::SetExpectedCredentials(const UserContext& user_context) {
Expand Down Expand Up @@ -133,13 +135,13 @@ bool LoginManagerTest::AddUserToSession(const UserContext& user_context) {
}

void LoginManagerTest::LoginUser(const AccountId& account_id) {
const UserContext user_context = CreateUserContext(account_id);
const UserContext user_context = CreateUserContext(account_id, kPassword);
SetExpectedCredentials(user_context);
EXPECT_TRUE(TryToLogin(user_context));
}

void LoginManagerTest::AddUser(const AccountId& account_id) {
const UserContext user_context = CreateUserContext(account_id);
const UserContext user_context = CreateUserContext(account_id, kPassword);
SetExpectedCredentials(user_context);
EXPECT_TRUE(AddUserToSession(user_context));
}
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/chromeos/login/login_manager_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class LoginManagerTest : public MixinBasedInProcessBrowserTest {
// TODO(dzhioev): Add the ability to register users without a PRE_* test.
void RegisterUser(const AccountId& account_id);

static const char kPassword[];
UserContext CreateUserContext(const AccountId& account_id,
const std::string& password);

// Set expected credentials for next login attempt.
void SetExpectedCredentials(const UserContext& user_context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace {

constexpr char kTestUser[] = "test-user1@gmail.com";
constexpr char kTestUserGaiaId[] = "test-user1@gmail.com";
constexpr char kPassword[] = "test user password";


// Used to wait for session manager to become active.
Expand Down
67 changes: 55 additions & 12 deletions chrome/browser/chromeos/login/ui/user_adding_screen_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
// found in the LICENSE file.

#include "ash/public/cpp/ash_pref_names.h"
#include "ash/public/cpp/login_screen_test_api.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.h"
#include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
#include "chrome/browser/chromeos/login/login_manager_test.h"
#include "chrome/browser/chromeos/login/startup_utils.h"
#include "chrome/browser/chromeos/login/test/js_checker.h"
#include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
#include "chrome/browser/chromeos/login/test/session_manager_state_waiter.h"
#include "chrome/browser/chromeos/login/ui/login_display_host.h"
#include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
#include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h"
Expand All @@ -27,6 +30,19 @@

namespace chromeos {

namespace {

constexpr char kLoginFormatJS[] = R"({
var pod = $('pod-row').getPodWithUsername_('%s');
$('pod-row').focusPod(pod, true);
pod.passwordElement.value = '%s';
pod.passwordElement.dispatchEvent(
new Event('input', { bubbles: true, cancelable: true }));
pod.submitButton.click();
})";

} // namespace

class UserAddingScreenTest : public LoginManagerTest,
public UserAddingScreen::Observer {
public:
Expand Down Expand Up @@ -72,11 +88,26 @@ class UserAddingScreenTest : public LoginManagerTest,
user->set_can_lock(can_lock);
}

void UILoginUser(const AccountId& account_id, const std::string& password) {
// Wait for pods to load.
test::OobeJS().CreateWaiter("$(\'pod-row\').pods.length > 0")->Wait();

SetExpectedCredentials(CreateUserContext(account_id, password));
SessionStateWaiter waiter;

const std::string login_js = base::StringPrintf(
kLoginFormatJS, account_id.Serialize().c_str(), password.c_str());
test::ExecuteOobeJS(login_js);

waiter.Wait();
}

int user_adding_started() { return user_adding_started_; }

int user_adding_finished() { return user_adding_finished_; }

std::vector<AccountId> test_users_;
std::vector<AccountId> users_in_session_order_;

private:
int user_adding_started_ = 0;
Expand Down Expand Up @@ -112,7 +143,9 @@ IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, CancelAdding) {
EXPECT_EQ(session_manager::SessionState::LOGIN_SECONDARY,
session_manager::SessionManager::Get()->session_state());

UserAddingScreen::Get()->Cancel();
EXPECT_TRUE(ash::LoginScreenTestApi::IsCancelButtonShown());
EXPECT_TRUE(ash::LoginScreenTestApi::ClickCancelButton());

WaitUntilUserAddingFinishedOrCancelled();
content::RunAllPendingInMessageLoop();
EXPECT_EQ(1, user_adding_finished());
Expand All @@ -137,20 +170,24 @@ IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, AddingSeveralUsers) {
session_manager::SessionManager::Get()->session_state());

LoginUser(test_users_[0]);
users_in_session_order_.push_back(test_users_[0]);
EXPECT_EQ(session_manager::SessionState::ACTIVE,
session_manager::SessionManager::Get()->session_state());

user_manager::UserManager* user_manager = user_manager::UserManager::Get();

for (int i = 1; i < 3; ++i) {
const int n = test_users_.size();
for (int i = 1; i < n; ++i) {
UserAddingScreen::Get()->Start();
content::RunAllPendingInMessageLoop();
OobeScreenWaiter(OobeScreen::SCREEN_ACCOUNT_PICKER).Wait();
EXPECT_EQ(i, user_adding_started());
EXPECT_EQ(session_manager::SessionState::LOGIN_SECONDARY,
session_manager::SessionManager::Get()->session_state());
AddUser(test_users_[i]);
WaitUntilUserAddingFinishedOrCancelled();
content::RunAllPendingInMessageLoop();
EXPECT_TRUE(ash::LoginScreenTestApi::IsCancelButtonShown());

UILoginUser(test_users_[n - i], kPassword);
users_in_session_order_.push_back(test_users_[n - i]);

EXPECT_EQ(i, user_adding_finished());
EXPECT_EQ(session_manager::SessionState::ACTIVE,
session_manager::SessionManager::Get()->session_state());
Expand Down Expand Up @@ -205,21 +242,21 @@ IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, AddingSeveralUsers) {
unlock_users = user_manager->GetUnlockUsers();
ASSERT_EQ(3UL, unlock_users.size());
for (int i = 0; i < 3; ++i)
EXPECT_EQ(test_users_[i], unlock_users[i]->GetAccountId());
EXPECT_EQ(users_in_session_order_[i], unlock_users[i]->GetAccountId());

// This preference doesn't affect list of unlock users.
prefs2->SetBoolean(ash::prefs::kEnableAutoScreenLock, true);
unlock_users = user_manager->GetUnlockUsers();
ASSERT_EQ(3UL, unlock_users.size());
for (int i = 0; i < 3; ++i)
EXPECT_EQ(test_users_[i], unlock_users[i]->GetAccountId());
EXPECT_EQ(users_in_session_order_[i], unlock_users[i]->GetAccountId());

// Now one of the users is unable to unlock.
SetUserCanLock(user_manager->GetLoggedInUsers()[2], false);
unlock_users = user_manager->GetUnlockUsers();
ASSERT_EQ(2UL, unlock_users.size());
for (int i = 0; i < 2; ++i)
EXPECT_EQ(test_users_[i], unlock_users[i]->GetAccountId());
EXPECT_EQ(users_in_session_order_[i], unlock_users[i]->GetAccountId());
SetUserCanLock(user_manager->GetLoggedInUsers()[2], true);

// Now one of the users has not-allowed policy.
Expand All @@ -231,7 +268,7 @@ IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, AddingSeveralUsers) {
unlock_users = user_manager->GetUnlockUsers();
ASSERT_EQ(2UL, unlock_users.size());
for (int i = 0; i < 2; ++i)
EXPECT_EQ(test_users_[i], unlock_users[i]->GetAccountId());
EXPECT_EQ(users_in_session_order_[i], unlock_users[i]->GetAccountId());
}

IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, PRE_ScreenVisibility) {
Expand All @@ -245,7 +282,10 @@ IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, ScreenVisibility) {

UserAddingScreen::Get()->Start();
OobeScreenWaiter(OobeScreen::SCREEN_ACCOUNT_PICKER).Wait();
UserAddingScreen::Get()->Cancel();

EXPECT_TRUE(ash::LoginScreenTestApi::IsCancelButtonShown());
EXPECT_TRUE(ash::LoginScreenTestApi::ClickCancelButton());

WaitUntilUserAddingFinishedOrCancelled();
content::RunAllPendingInMessageLoop();

Expand All @@ -267,7 +307,10 @@ IN_PROC_BROWSER_TEST_F(UserAddingScreenTest, ScreenVisibility) {

UserAddingScreen::Get()->Start();
OobeScreenWaiter(OobeScreen::SCREEN_ACCOUNT_PICKER).Wait();
UserAddingScreen::Get()->Cancel();

EXPECT_TRUE(ash::LoginScreenTestApi::IsCancelButtonShown());
EXPECT_TRUE(ash::LoginScreenTestApi::ClickCancelButton());

WaitUntilUserAddingFinishedOrCancelled();
content::RunAllPendingInMessageLoop();
}
Expand Down

0 comments on commit d2c5628

Please sign in to comment.