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_;
......
......@@ -7,10 +7,8 @@
#include <memory>
#include "ash/wm/window_state.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/run_loop.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/quick_unlock/quick_unlock_utils.h"
......@@ -19,18 +17,15 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller_test.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/biod/fake_biod_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_session_manager_client.h"
#include "chromeos/login/auth/key.h"
#include "chromeos/login/auth/user_context.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/core/session_manager.h"
#include "components/user_manager/user_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h"
......@@ -41,84 +36,41 @@ namespace {
constexpr char kFingerprint[] = "pinky";
// An object that wait for lock state and fullscreen state.
class Waiter : public content::NotificationObserver {
class FullscreenWaiter {
public:
explicit Waiter(Browser* browser) : browser_(browser) {
registrar_.Add(this, chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED,
content::NotificationService::AllSources());
}
~Waiter() override {}
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override {
DCHECK(type == chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED ||
type == chrome::NOTIFICATION_FULLSCREEN_CHANGED);
if (quit_loop_)
std::move(quit_loop_).Run();
}
explicit FullscreenWaiter(Browser* browser) : browser_(browser) {}
~FullscreenWaiter() = default;
// Wait until the two conditions are met.
void Wait(bool locker_state, bool fullscreen) {
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
while (tester->IsLocked() != locker_state ||
browser_->window()->IsFullscreen() != fullscreen) {
base::RunLoop run_loop;
quit_loop_ = run_loop.QuitClosure();
run_loop.Run();
}
// Make sure all pending tasks are executed.
base::RunLoop().RunUntilIdle();
void WaitForState(bool fullscreen) {
if (browser_->window()->IsFullscreen() != fullscreen)
observer_.Wait();
}
private:
FullscreenNotificationObserver observer_;
Browser* browser_;
content::NotificationRegistrar registrar_;
base::OnceClosure quit_loop_;
DISALLOW_COPY_AND_ASSIGN(Waiter);
DISALLOW_COPY_AND_ASSIGN(FullscreenWaiter);
};
} // namespace
class ScreenLockerTest : public InProcessBrowserTest {
public:
ScreenLockerTest() = default;
~ScreenLockerTest() override = default;
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();
EXPECT_TRUE(tester->IsLocked());
EXPECT_EQ(session_manager::SessionState::LOCKED,
session_manager::SessionManager::Get()->session_state());
}
FakeSessionManagerClient* session_manager_client() {
return fake_session_manager_client_;
}
// InProcessBrowserTest:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
}
void SetUpInProcessBrowserTestFixture() override {
fake_session_manager_client_ = new FakeSessionManagerClient;
DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
std::unique_ptr<SessionManagerClient>(fake_session_manager_client_));
zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
zero_duration_mode_ =
std::make_unique<ui::ScopedAnimationDurationScaleMode>(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
fake_biod_client_ = new FakeBiodClient();
DBusThreadManager::GetSetterForTesting()->SetBiodClient(
......@@ -129,7 +81,7 @@ class ScreenLockerTest : public InProcessBrowserTest {
quick_unlock::EnableForTesting();
fake_biod_client_->StartEnrollSession(
"user", std::string(),
"test-user", std::string(),
base::BindRepeating(&ScreenLockerTest::OnStartSession,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
......@@ -161,27 +113,21 @@ class ScreenLockerTest : public InProcessBrowserTest {
DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
};
} // namespace
IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBadThenGoodPassword) {
// Show lock screen and wait until it is shown.
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get());
tester->Lock();
// Inject fake authentication credentials.
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR,
user_manager::StubAccountId());
user_context.SetKey(Key("pass"));
tester->InjectStubUserContext(user_context);
EXPECT_TRUE(tester->IsLocked());
tester->SetUnlockPassword(user_manager::StubAccountId(), "pass");
// Submit a bad password.
tester->EnterPassword(user_manager::StubAccountId(), "fail");
base::RunLoop().RunUntilIdle();
tester->UnlockWithPassword(user_manager::StubAccountId(), "fail");
EXPECT_TRUE(tester->IsLocked());
// Submit the correct password. Successful authentication clears the lock
// screen and tells the SessionManager to announce this over DBus.
tester->EnterPassword(user_manager::StubAccountId(), "pass");
base::RunLoop().RunUntilIdle();
tester->UnlockWithPassword(user_manager::StubAccountId(), "pass");
EXPECT_FALSE(tester->IsLocked());
EXPECT_EQ(1, session_manager_client()->notify_lock_screen_shown_call_count());
EXPECT_EQ(session_manager::SessionState::ACTIVE,
......@@ -214,38 +160,34 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
ash::wm::WindowState* window_state =
ash::wm::GetWindowState(browser_window->GetNativeWindow());
{
Waiter waiter(browser());
FullscreenWaiter fullscreen_waiter(browser());
browser()
->exclusive_access_manager()
->fullscreen_controller()
->ToggleBrowserFullscreenMode();
waiter.Wait(false /* not locked */, true /* full screen */);
fullscreen_waiter.WaitForState(true);
EXPECT_TRUE(browser_window->IsFullscreen());
EXPECT_FALSE(window_state->GetHideShelfWhenFullscreen());
EXPECT_FALSE(tester->IsLocked());
}
{
Waiter waiter(browser());
ScreenLocker::Show();
waiter.Wait(true /* locked */, true /* full screen */);
FullscreenWaiter fullscreen_waiter(browser());
tester->Lock();
fullscreen_waiter.WaitForState(true);
EXPECT_TRUE(browser_window->IsFullscreen());
EXPECT_FALSE(window_state->GetHideShelfWhenFullscreen());
EXPECT_TRUE(tester->IsLocked());
}
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR,
user_manager::StubAccountId());
user_context.SetKey(Key("pass"));
tester->InjectStubUserContext(user_context);
tester->EnterPassword(user_manager::StubAccountId(), "pass");
base::RunLoop().RunUntilIdle();
tester->SetUnlockPassword(user_manager::StubAccountId(), "pass");
tester->UnlockWithPassword(user_manager::StubAccountId(), "pass");
EXPECT_FALSE(tester->IsLocked());
{
Waiter waiter(browser());
FullscreenWaiter fullscreen_waiter(browser());
browser()
->exclusive_access_manager()
->fullscreen_controller()
->ToggleBrowserFullscreenMode();
waiter.Wait(false /* not locked */, false /* fullscreen */);
fullscreen_waiter.WaitForState(false);
EXPECT_FALSE(browser_window->IsFullscreen());
}
......@@ -257,28 +199,28 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
// has all of the pixels, locking the screen should exit fullscreen. The
// fullscreen window has all of the pixels when in tab fullscreen.
{
Waiter waiter(browser());
FullscreenWaiter fullscreen_waiter(browser());
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
browser()
->exclusive_access_manager()
->fullscreen_controller()
->EnterFullscreenModeForTab(web_contents, GURL());
waiter.Wait(false /* not locked */, true /* fullscreen */);
fullscreen_waiter.WaitForState(true);
EXPECT_TRUE(browser_window->IsFullscreen());
EXPECT_TRUE(window_state->GetHideShelfWhenFullscreen());
EXPECT_FALSE(tester->IsLocked());
}
{
Waiter waiter(browser());
ScreenLocker::Show();
waiter.Wait(true /* locked */, false /* full screen */);
FullscreenWaiter fullscreen_waiter(browser());
tester->Lock();
fullscreen_waiter.WaitForState(false);
EXPECT_FALSE(browser_window->IsFullscreen());
EXPECT_TRUE(tester->IsLocked());
}
tester->EnterPassword(user_manager::StubAccountId(), "pass");
base::RunLoop().RunUntilIdle();
tester->SetUnlockPassword(user_manager::StubAccountId(), "pass");
tester->UnlockWithPassword(user_manager::StubAccountId(), "pass");
EXPECT_FALSE(tester->IsLocked());
EXPECT_EQ(2, session_manager_client()->notify_lock_screen_shown_call_count());
......@@ -288,7 +230,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get());
tester->Lock();
// Calling Show again simply send LockCompleted signal.
ScreenLocker::Show();
......@@ -306,14 +248,11 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) {
// Show lock screen and wait until it is shown.
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get());
tester->Lock();
// Inject fake authentication credentials.
const std::string kPassword = "pass";
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR,
user_manager::StubAccountId());
user_context.SetKey(Key(kPassword));
tester->InjectStubUserContext(user_context);
tester->SetUnlockPassword(user_manager::StubAccountId(), kPassword);
EXPECT_TRUE(tester->IsLocked());
// Disable authentication for user.
......@@ -322,7 +261,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) {
base::Time::Now() + base::TimeDelta::FromHours(1));
// Try to authenticate with password.
tester->EnterPassword(user_manager::StubAccountId(), kPassword);
tester->UnlockWithPassword(user_manager::StubAccountId(), kPassword);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(tester->IsLocked());
......@@ -331,7 +270,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) {
user_manager::StubAccountId(), true /*is_enabled*/, base::nullopt);
// Try to authenticate with password.
tester->EnterPassword(user_manager::StubAccountId(), kPassword);
tester->UnlockWithPassword(user_manager::StubAccountId(), kPassword);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(tester->IsLocked());
EXPECT_EQ(1, session_manager_client()->notify_lock_screen_shown_call_count());
......@@ -346,14 +285,10 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, FingerprintAuthWhenAuthDisabled) {
// Show lock screen and wait until it is shown.
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get());
tester->Lock();
// Inject fake authentication credentials.
const std::string kPassword = "pass";
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR,
user_manager::StubAccountId());
user_context.SetKey(Key(kPassword));
tester->InjectStubUserContext(user_context);
tester->SetUnlockPassword(user_manager::StubAccountId(), kPassword);
EXPECT_TRUE(tester->IsLocked());
// Disable authentication for user.
......
......@@ -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,7 +89,7 @@ class WebUIScreenLockerTester : public ScreenLockerTester {
// ScreenLockerTester:
bool IsLocked() override { return IsScreenLockerLocked(); }
void EnterPassword(const AccountId& account_id,
void UnlockWithPassword(const AccountId& account_id,
const std::string& password) override {
bool result;
......@@ -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,
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,14 +22,18 @@ 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,
// Enters and submits the given password for the given account.
virtual void UnlockWithPassword(const AccountId& account_id,
const std::string& password) = 0;
};
......
......@@ -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