Commit 88fb7f25 authored by Jacob Dufault's avatar Jacob Dufault Committed by Commit Bot

cros: Make ScreenLockerTester have a higher-level synchronous API that handles waiting

- Simplify helper code used with ScreenLockerTest.TestFullscreenExit
- Simplify BrailleDisplayPrivateAPIUnitTest.KeyEventOnLockScreen, remove MSAN
  restriction since views-lock screen is significantly faster to load

Bug: 899777
Change-Id: I0cf3c50c562289c42513b8509a86f901caf93035
Reviewed-on: https://chromium-review.googlesource.com/c/1327543
Commit-Queue: Jacob Dufault <jdufault@chromium.org>
Reviewed-by: default avatarDavid Tseng <dtseng@chromium.org>
Reviewed-by: default avatarAlexander Alekseev <alemate@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613321}
parent 2be72dc0
......@@ -10,7 +10,6 @@
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/extensions/users_private/users_private_delegate.h"
#include "chrome/browser/chromeos/extensions/users_private/users_private_delegate_factory.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.h"
......@@ -25,9 +24,6 @@
#include "components/keyed_service/core/keyed_service.h"
#include "components/ownership/mock_owner_key_util.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/core/session_manager.h"
#include "components/session_manager/session_manager_types.h"
#include "content/public/browser/notification_service.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/test_utils.h"
#include "crypto/rsa_private_key.h"
......@@ -239,16 +235,7 @@ IN_PROC_BROWSER_TEST_F(UsersPrivateApiLoginStatusTest, User) {
// Screenlock - logged in, screen locked.
IN_PROC_BROWSER_TEST_F(UsersPrivateApiLockStatusTest, ScreenLock) {
chromeos::ScreenLocker::Show();
std::unique_ptr<chromeos::ScreenLockerTester> tester =
chromeos::ScreenLockerTester::Create();
content::WindowedNotificationObserver lock_state_observer(
chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
if (!tester->IsLocked())
lock_state_observer.Wait();
EXPECT_EQ(session_manager::SessionState::LOCKED,
session_manager::SessionManager::Get()->session_state());
chromeos::ScreenLockerTester::Create()->Lock();
EXPECT_TRUE(RunExtensionSubtest("users_private", "main.html?getLoginStatus",
kFlagLoadAsComponent))
<< message_;
......
......@@ -14,11 +14,17 @@
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.h"
#include "chrome/browser/chromeos/login/lock/webui_screen_locker.h"
#include "chromeos/login/auth/auth_status_consumer.h"
#include "chromeos/login/auth/fake_extended_authenticator.h"
#include "chromeos/login/auth/key.h"
#include "chromeos/login/auth/stub_authenticator.h"
#include "chromeos/login/auth/user_context.h"
#include "components/session_manager/core/session_manager.h"
#include "components/session_manager/session_manager_types.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
......@@ -83,8 +89,8 @@ class WebUIScreenLockerTester : public ScreenLockerTester {
// ScreenLockerTester:
bool IsLocked() override { return IsScreenLockerLocked(); }
void EnterPassword(const AccountId& account_id,
const std::string& password) override {
void UnlockWithPassword(const AccountId& account_id,
const std::string& password) override {
bool result;
SetPassword(password);
......@@ -165,10 +171,11 @@ class MojoScreenLockerTester : public ScreenLockerTester {
login_screen.IsLockShown(&is_ui_shown);
return IsScreenLockerLocked() && is_ui_shown;
}
void EnterPassword(const AccountId& account_id,
const std::string& password) override {
void UnlockWithPassword(const AccountId& account_id,
const std::string& password) override {
ash::mojom::LoginScreenTestApiAsyncWaiter login_screen(test_api_.get());
login_screen.SubmitPassword(account_id, password);
base::RunLoop().RunUntilIdle();
}
private:
......@@ -192,8 +199,25 @@ ScreenLockerTester::ScreenLockerTester() = default;
ScreenLockerTester::~ScreenLockerTester() = default;
void ScreenLockerTester::InjectStubUserContext(
const UserContext& user_context) {
void ScreenLockerTester::Lock() {
content::WindowedNotificationObserver lock_state_observer(
chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
ScreenLocker::Show();
if (!IsLocked())
lock_state_observer.Wait();
ASSERT_TRUE(IsLocked());
ASSERT_EQ(session_manager::SessionState::LOCKED,
session_manager::SessionManager::Get()->session_state());
base::RunLoop().RunUntilIdle();
}
void ScreenLockerTester::SetUnlockPassword(const AccountId& account_id,
const std::string& password) {
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR,
account_id);
user_context.SetKey(Key(password));
auto* locker = ScreenLocker::default_screen_locker();
locker->SetAuthenticatorsForTesting(
base::MakeRefCounted<StubAuthenticator>(locker, user_context),
......
......@@ -12,8 +12,6 @@ class AccountId;
namespace chromeos {
class UserContext;
// ScreenLockerTester provides a high-level API to test the lock screen. This
// API is meant to be representation independent.
class ScreenLockerTester {
......@@ -24,15 +22,19 @@ class ScreenLockerTester {
ScreenLockerTester();
virtual ~ScreenLockerTester();
// Synchronously lock the device.
void Lock();
// Injects authenticators that only authenticate with the given password.
void SetUnlockPassword(const AccountId& account_id,
const std::string& password);
// Returns true if the screen is locked.
virtual bool IsLocked() = 0;
// Injects StubAuthenticator that uses the credentials in |user_context|.
virtual void InjectStubUserContext(const UserContext& user_context);
// Enters and submits the given password.
virtual void EnterPassword(const AccountId& account_id,
const std::string& password) = 0;
// Enters and submits the given password for the given account.
virtual void UnlockWithPassword(const AccountId& account_id,
const std::string& password) = 0;
};
} // namespace chromeos
......
......@@ -237,14 +237,9 @@ class ShutdownPolicyLockerTest : public ShutdownPolicyBaseTest {
ShutdownPolicyBaseTest::SetUpOnMainThread();
// Bring up the locker screen.
ScreenLocker::Show();
std::unique_ptr<chromeos::ScreenLockerTester> tester =
chromeos::ScreenLockerTester::Create();
content::WindowedNotificationObserver lock_state_observer(
chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
if (!tester->IsLocked())
lock_state_observer.Wait();
tester->Lock();
ScreenLocker* screen_locker = ScreenLocker::default_screen_locker();
WebUIScreenLocker* web_ui_screen_locker =
screen_locker->web_ui_for_testing();
......
......@@ -9,7 +9,6 @@
#include "base/task/post_task.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.h"
#include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
......@@ -23,17 +22,12 @@
#include "chrome/test/base/testing_profile.h"
#include "chromeos/chromeos_switches.h"
#include "components/session_manager/core/session_manager.h"
#include "components/user_manager/user_names.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
using chromeos::ProfileHelper;
using chromeos::ScreenLocker;
using chromeos::ScreenLockerTester;
using user_manager::UserManager;
using content::BrowserThread;
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
namespace extensions {
namespace api {
......@@ -41,8 +35,7 @@ namespace braille_display_private {
namespace {
constexpr char kTestUserName[] = "owner@invalid.domain";
constexpr char kTestUserGaiaId[] = "0123456789";
constexpr char kTestUserEmail[] = "testuser@gmail.com";
// Used to make ReadKeys return an error.
brlapi_keyCode_t kErrorKeyCode = BRLAPI_KEY_MAX;
......@@ -76,7 +69,7 @@ class MockBrlapiConnection : public BrlapiConnection {
on_data_ready_ = on_data_ready;
if (!data_->pending_keys.empty()) {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&MockBrlapiConnection::NotifyDataReady,
base::Unretained(this)));
}
......@@ -88,7 +81,7 @@ class MockBrlapiConnection : public BrlapiConnection {
if (data_->reappear_on_disconnect) {
data_->display_columns *= 2;
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(
&BrailleControllerImpl::PokeSocketDirForTesting,
base::Unretained(BrailleControllerImpl::GetInstance())));
......@@ -133,12 +126,11 @@ class MockBrlapiConnection : public BrlapiConnection {
}
private:
void NotifyDataReady() {
on_data_ready_.Run();
if (!data_->pending_keys.empty()) {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&MockBrlapiConnection::NotifyDataReady,
base::Unretained(this)));
}
......@@ -276,27 +268,28 @@ IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateApiTest, DisplayStateChanges) {
class BrailleDisplayPrivateAPIUserTest : public BrailleDisplayPrivateApiTest {
public:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(chromeos::switches::kLoginManager);
command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile,
TestingProfile::kTestUserProfileDir);
}
class MockEventDelegate : public BrailleDisplayPrivateAPI::EventDelegate {
public:
MockEventDelegate() : event_count_(0) {}
MockEventDelegate() = default;
~MockEventDelegate() override = default;
int GetEventCount() { return event_count_; }
// BrailleDisplayPrivateAPI::EventDelegate:
void BroadcastEvent(std::unique_ptr<Event> event) override {
++event_count_;
}
bool HasListener() override { return true; }
private:
int event_count_;
int event_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(MockEventDelegate);
};
BrailleDisplayPrivateAPIUserTest() = default;
~BrailleDisplayPrivateAPIUserTest() override = default;
MockEventDelegate* SetMockEventDelegate(BrailleDisplayPrivateAPI* api) {
MockEventDelegate* delegate = new MockEventDelegate();
api->SetEventDelegateForTest(
......@@ -304,63 +297,42 @@ class BrailleDisplayPrivateAPIUserTest : public BrailleDisplayPrivateApiTest {
return delegate;
}
void LockScreen(ScreenLockerTester* tester) {
ScreenLocker::Show();
content::WindowedNotificationObserver lock_state_observer(
chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
if (!tester->IsLocked())
lock_state_observer.Wait();
ASSERT_TRUE(tester->IsLocked());
}
void DismissLockScreen(ScreenLockerTester* tester) {
ScreenLocker::Hide();
content::WindowedNotificationObserver lock_state_observer(
chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
if (tester->IsLocked())
lock_state_observer.Wait();
ASSERT_FALSE(tester->IsLocked());
// BrailleDisplayPrivateApiTest:
void SetUpInProcessBrowserTestFixture() override {
BrailleDisplayPrivateApiTest::SetUpInProcessBrowserTestFixture();
zero_duration_mode_ =
std::make_unique<ui::ScopedAnimationDurationScaleMode>(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
}
protected:
// BrailleDisplayPrivateApiTest:
void DisableAccessibilityManagerBraille() override {
// Let the accessibility manager behave as usual for these tests.
}
protected:
std::unique_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
DISALLOW_COPY_AND_ASSIGN(BrailleDisplayPrivateAPIUserTest);
};
// Flakily times out on ChromeOS MSAN bots. See https://crbug.com/592893.
#if defined(MEMORY_SANITIZER)
#define MAYBE_KeyEventOnLockScreen DISABLED_KeyEventOnLockScreen
#else
#define MAYBE_KeyEventOnLockScreen KeyEventOnLockScreen
#endif
IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateAPIUserTest,
MAYBE_KeyEventOnLockScreen) {
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
// Log in.
session_manager::SessionManager::Get()->CreateSession(
AccountId::FromUserEmailGaiaId(kTestUserName, kTestUserGaiaId),
kTestUserName, false);
{
base::ScopedAllowBlockingForTesting allow_io;
g_browser_process->profile_manager()->GetProfile(
ProfileHelper::Get()->GetProfilePathByUserIdHash(kTestUserName));
}
session_manager::SessionManager::Get()->SessionStarted();
Profile* profile = ProfileManager::GetActiveUserProfile();
ASSERT_FALSE(
ProfileHelper::GetSigninProfile()->IsSameProfile(profile))
<< ProfileHelper::GetSigninProfile()->GetDebugName() << " vs. "
<< profile->GetDebugName();
// Create API and event delegate for sign in profile.
BrailleDisplayPrivateAPI signin_api(ProfileHelper::GetSigninProfile());
IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateAPIUserTest, KeyEventOnLockScreen) {
std::unique_ptr<chromeos::ScreenLockerTester> tester =
chromeos::ScreenLockerTester::Create();
// Make sure the signin profile and active profile are different.
Profile* signin_profile = chromeos::ProfileHelper::GetSigninProfile();
Profile* user_profile = ProfileManager::GetActiveUserProfile();
ASSERT_FALSE(signin_profile->IsSameProfile(user_profile))
<< signin_profile->GetDebugName() << " vs "
<< user_profile->GetDebugName();
// Create API and event delegate for the signin profile.
BrailleDisplayPrivateAPI signin_api(signin_profile);
MockEventDelegate* signin_delegate = SetMockEventDelegate(&signin_api);
EXPECT_EQ(0, signin_delegate->GetEventCount());
// Create api and delegate for the logged in user.
BrailleDisplayPrivateAPI user_api(profile);
// Create API and delegate for the user profile.
BrailleDisplayPrivateAPI user_api(user_profile);
MockEventDelegate* user_delegate = SetMockEventDelegate(&user_api);
// Send key event to both profiles.
......@@ -371,16 +343,16 @@ IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateAPIUserTest,
EXPECT_EQ(0, signin_delegate->GetEventCount());
EXPECT_EQ(1, user_delegate->GetEventCount());
// Lock screen, and make sure that the key event goes to the
// signin profile.
LockScreen(tester.get());
// Lock screen, and make sure that the key event goes to the signin profile.
tester->Lock();
signin_api.OnBrailleKeyEvent(key_event);
user_api.OnBrailleKeyEvent(key_event);
EXPECT_EQ(0, signin_delegate->GetEventCount());
EXPECT_EQ(2, user_delegate->GetEventCount());
// Unlock screen, making sur ekey events go to the user profile again.
DismissLockScreen(tester.get());
// Unlock screen, making sure key events go to the user profile again.
tester->SetUnlockPassword(AccountId::FromUserEmail(kTestUserEmail), "pass");
tester->UnlockWithPassword(AccountId::FromUserEmail(kTestUserEmail), "pass");
signin_api.OnBrailleKeyEvent(key_event);
user_api.OnBrailleKeyEvent(key_event);
EXPECT_EQ(0, signin_delegate->GetEventCount());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment