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 @@ ...@@ -10,7 +10,6 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/values.h" #include "base/values.h"
#include "build/build_config.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.h"
#include "chrome/browser/chromeos/extensions/users_private/users_private_delegate_factory.h" #include "chrome/browser/chromeos/extensions/users_private/users_private_delegate_factory.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.h" #include "chrome/browser/chromeos/login/lock/screen_locker.h"
...@@ -25,9 +24,6 @@ ...@@ -25,9 +24,6 @@
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/ownership/mock_owner_key_util.h" #include "components/ownership/mock_owner_key_util.h"
#include "components/prefs/pref_service.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/common/content_switches.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "crypto/rsa_private_key.h" #include "crypto/rsa_private_key.h"
...@@ -239,16 +235,7 @@ IN_PROC_BROWSER_TEST_F(UsersPrivateApiLoginStatusTest, User) { ...@@ -239,16 +235,7 @@ IN_PROC_BROWSER_TEST_F(UsersPrivateApiLoginStatusTest, User) {
// Screenlock - logged in, screen locked. // Screenlock - logged in, screen locked.
IN_PROC_BROWSER_TEST_F(UsersPrivateApiLockStatusTest, ScreenLock) { IN_PROC_BROWSER_TEST_F(UsersPrivateApiLockStatusTest, ScreenLock) {
chromeos::ScreenLocker::Show(); chromeos::ScreenLockerTester::Create()->Lock();
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());
EXPECT_TRUE(RunExtensionSubtest("users_private", "main.html?getLoginStatus", EXPECT_TRUE(RunExtensionSubtest("users_private", "main.html?getLoginStatus",
kFlagLoadAsComponent)) kFlagLoadAsComponent))
<< message_; << message_;
......
...@@ -7,10 +7,8 @@ ...@@ -7,10 +7,8 @@
#include <memory> #include <memory>
#include "ash/wm/window_state.h" #include "ash/wm/window_state.h"
#include "base/command_line.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/run_loop.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.h"
#include "chrome/browser/chromeos/login/lock/screen_locker_tester.h" #include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
#include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h" #include "chrome/browser/chromeos/login/quick_unlock/quick_unlock_utils.h"
...@@ -19,18 +17,15 @@ ...@@ -19,18 +17,15 @@
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.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.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller_test.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.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/biod/fake_biod_client.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_session_manager_client.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/prefs/pref_service.h"
#include "components/session_manager/core/session_manager.h" #include "components/session_manager/core/session_manager.h"
#include "components/user_manager/user_names.h" #include "components/user_manager/user_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ui_base_features.h" #include "ui/base/ui_base_features.h"
...@@ -41,84 +36,41 @@ namespace { ...@@ -41,84 +36,41 @@ namespace {
constexpr char kFingerprint[] = "pinky"; constexpr char kFingerprint[] = "pinky";
// An object that wait for lock state and fullscreen state. class FullscreenWaiter {
class Waiter : public content::NotificationObserver {
public: public:
explicit Waiter(Browser* browser) : browser_(browser) { explicit FullscreenWaiter(Browser* browser) : browser_(browser) {}
registrar_.Add(this, chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, ~FullscreenWaiter() = default;
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();
}
// Wait until the two conditions are met. void WaitForState(bool fullscreen) {
void Wait(bool locker_state, bool fullscreen) { if (browser_->window()->IsFullscreen() != fullscreen)
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create(); observer_.Wait();
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();
} }
private: private:
FullscreenNotificationObserver observer_;
Browser* browser_; Browser* browser_;
content::NotificationRegistrar registrar_;
base::OnceClosure quit_loop_;
DISALLOW_COPY_AND_ASSIGN(Waiter); DISALLOW_COPY_AND_ASSIGN(FullscreenWaiter);
}; };
} // namespace
class ScreenLockerTest : public InProcessBrowserTest { class ScreenLockerTest : public InProcessBrowserTest {
public: public:
ScreenLockerTest() = default; ScreenLockerTest() = default;
~ScreenLockerTest() override = 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() { FakeSessionManagerClient* session_manager_client() {
return fake_session_manager_client_; return fake_session_manager_client_;
} }
// InProcessBrowserTest: // InProcessBrowserTest:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
}
void SetUpInProcessBrowserTestFixture() override { void SetUpInProcessBrowserTestFixture() override {
fake_session_manager_client_ = new FakeSessionManagerClient; fake_session_manager_client_ = new FakeSessionManagerClient;
DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
std::unique_ptr<SessionManagerClient>(fake_session_manager_client_)); std::unique_ptr<SessionManagerClient>(fake_session_manager_client_));
zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( zero_duration_mode_ =
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); std::make_unique<ui::ScopedAnimationDurationScaleMode>(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
fake_biod_client_ = new FakeBiodClient(); fake_biod_client_ = new FakeBiodClient();
DBusThreadManager::GetSetterForTesting()->SetBiodClient( DBusThreadManager::GetSetterForTesting()->SetBiodClient(
...@@ -129,7 +81,7 @@ class ScreenLockerTest : public InProcessBrowserTest { ...@@ -129,7 +81,7 @@ class ScreenLockerTest : public InProcessBrowserTest {
quick_unlock::EnableForTesting(); quick_unlock::EnableForTesting();
fake_biod_client_->StartEnrollSession( fake_biod_client_->StartEnrollSession(
"user", std::string(), "test-user", std::string(),
base::BindRepeating(&ScreenLockerTest::OnStartSession, base::BindRepeating(&ScreenLockerTest::OnStartSession,
base::Unretained(this))); base::Unretained(this)));
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
...@@ -161,27 +113,21 @@ class ScreenLockerTest : public InProcessBrowserTest { ...@@ -161,27 +113,21 @@ class ScreenLockerTest : public InProcessBrowserTest {
DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest); DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
}; };
} // namespace
IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBadThenGoodPassword) { IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBadThenGoodPassword) {
// Show lock screen and wait until it is shown.
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create(); std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get()); tester->Lock();
// Inject fake authentication credentials. tester->SetUnlockPassword(user_manager::StubAccountId(), "pass");
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());
// Submit a bad password. // Submit a bad password.
tester->EnterPassword(user_manager::StubAccountId(), "fail"); tester->UnlockWithPassword(user_manager::StubAccountId(), "fail");
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(tester->IsLocked()); EXPECT_TRUE(tester->IsLocked());
// Submit the correct password. Successful authentication clears the lock // Submit the correct password. Successful authentication clears the lock
// screen and tells the SessionManager to announce this over DBus. // screen and tells the SessionManager to announce this over DBus.
tester->EnterPassword(user_manager::StubAccountId(), "pass"); tester->UnlockWithPassword(user_manager::StubAccountId(), "pass");
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(tester->IsLocked()); EXPECT_FALSE(tester->IsLocked());
EXPECT_EQ(1, session_manager_client()->notify_lock_screen_shown_call_count()); EXPECT_EQ(1, session_manager_client()->notify_lock_screen_shown_call_count());
EXPECT_EQ(session_manager::SessionState::ACTIVE, EXPECT_EQ(session_manager::SessionState::ACTIVE,
...@@ -214,38 +160,34 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) { ...@@ -214,38 +160,34 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
ash::wm::WindowState* window_state = ash::wm::WindowState* window_state =
ash::wm::GetWindowState(browser_window->GetNativeWindow()); ash::wm::GetWindowState(browser_window->GetNativeWindow());
{ {
Waiter waiter(browser()); FullscreenWaiter fullscreen_waiter(browser());
browser() browser()
->exclusive_access_manager() ->exclusive_access_manager()
->fullscreen_controller() ->fullscreen_controller()
->ToggleBrowserFullscreenMode(); ->ToggleBrowserFullscreenMode();
waiter.Wait(false /* not locked */, true /* full screen */); fullscreen_waiter.WaitForState(true);
EXPECT_TRUE(browser_window->IsFullscreen()); EXPECT_TRUE(browser_window->IsFullscreen());
EXPECT_FALSE(window_state->GetHideShelfWhenFullscreen()); EXPECT_FALSE(window_state->GetHideShelfWhenFullscreen());
EXPECT_FALSE(tester->IsLocked()); EXPECT_FALSE(tester->IsLocked());
} }
{ {
Waiter waiter(browser()); FullscreenWaiter fullscreen_waiter(browser());
ScreenLocker::Show(); tester->Lock();
waiter.Wait(true /* locked */, true /* full screen */); fullscreen_waiter.WaitForState(true);
EXPECT_TRUE(browser_window->IsFullscreen()); EXPECT_TRUE(browser_window->IsFullscreen());
EXPECT_FALSE(window_state->GetHideShelfWhenFullscreen()); EXPECT_FALSE(window_state->GetHideShelfWhenFullscreen());
EXPECT_TRUE(tester->IsLocked()); EXPECT_TRUE(tester->IsLocked());
} }
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR, tester->SetUnlockPassword(user_manager::StubAccountId(), "pass");
user_manager::StubAccountId()); tester->UnlockWithPassword(user_manager::StubAccountId(), "pass");
user_context.SetKey(Key("pass"));
tester->InjectStubUserContext(user_context);
tester->EnterPassword(user_manager::StubAccountId(), "pass");
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(tester->IsLocked()); EXPECT_FALSE(tester->IsLocked());
{ {
Waiter waiter(browser()); FullscreenWaiter fullscreen_waiter(browser());
browser() browser()
->exclusive_access_manager() ->exclusive_access_manager()
->fullscreen_controller() ->fullscreen_controller()
->ToggleBrowserFullscreenMode(); ->ToggleBrowserFullscreenMode();
waiter.Wait(false /* not locked */, false /* fullscreen */); fullscreen_waiter.WaitForState(false);
EXPECT_FALSE(browser_window->IsFullscreen()); EXPECT_FALSE(browser_window->IsFullscreen());
} }
...@@ -257,28 +199,28 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) { ...@@ -257,28 +199,28 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
// has all of the pixels, locking the screen should exit fullscreen. The // has all of the pixels, locking the screen should exit fullscreen. The
// fullscreen window has all of the pixels when in tab fullscreen. // fullscreen window has all of the pixels when in tab fullscreen.
{ {
Waiter waiter(browser()); FullscreenWaiter fullscreen_waiter(browser());
content::WebContents* web_contents = content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents(); browser()->tab_strip_model()->GetActiveWebContents();
browser() browser()
->exclusive_access_manager() ->exclusive_access_manager()
->fullscreen_controller() ->fullscreen_controller()
->EnterFullscreenModeForTab(web_contents, GURL()); ->EnterFullscreenModeForTab(web_contents, GURL());
waiter.Wait(false /* not locked */, true /* fullscreen */); fullscreen_waiter.WaitForState(true);
EXPECT_TRUE(browser_window->IsFullscreen()); EXPECT_TRUE(browser_window->IsFullscreen());
EXPECT_TRUE(window_state->GetHideShelfWhenFullscreen()); EXPECT_TRUE(window_state->GetHideShelfWhenFullscreen());
EXPECT_FALSE(tester->IsLocked()); EXPECT_FALSE(tester->IsLocked());
} }
{ {
Waiter waiter(browser()); FullscreenWaiter fullscreen_waiter(browser());
ScreenLocker::Show(); tester->Lock();
waiter.Wait(true /* locked */, false /* full screen */); fullscreen_waiter.WaitForState(false);
EXPECT_FALSE(browser_window->IsFullscreen()); EXPECT_FALSE(browser_window->IsFullscreen());
EXPECT_TRUE(tester->IsLocked()); EXPECT_TRUE(tester->IsLocked());
} }
tester->EnterPassword(user_manager::StubAccountId(), "pass"); tester->SetUnlockPassword(user_manager::StubAccountId(), "pass");
base::RunLoop().RunUntilIdle(); tester->UnlockWithPassword(user_manager::StubAccountId(), "pass");
EXPECT_FALSE(tester->IsLocked()); EXPECT_FALSE(tester->IsLocked());
EXPECT_EQ(2, session_manager_client()->notify_lock_screen_shown_call_count()); EXPECT_EQ(2, session_manager_client()->notify_lock_screen_shown_call_count());
...@@ -288,7 +230,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) { ...@@ -288,7 +230,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) { IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create(); std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get()); tester->Lock();
// Calling Show again simply send LockCompleted signal. // Calling Show again simply send LockCompleted signal.
ScreenLocker::Show(); ScreenLocker::Show();
...@@ -306,14 +248,11 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) { ...@@ -306,14 +248,11 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) { IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) {
// Show lock screen and wait until it is shown. // Show lock screen and wait until it is shown.
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create(); std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get()); tester->Lock();
// Inject fake authentication credentials. // Inject fake authentication credentials.
const std::string kPassword = "pass"; const std::string kPassword = "pass";
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR, tester->SetUnlockPassword(user_manager::StubAccountId(), kPassword);
user_manager::StubAccountId());
user_context.SetKey(Key(kPassword));
tester->InjectStubUserContext(user_context);
EXPECT_TRUE(tester->IsLocked()); EXPECT_TRUE(tester->IsLocked());
// Disable authentication for user. // Disable authentication for user.
...@@ -322,7 +261,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) { ...@@ -322,7 +261,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) {
base::Time::Now() + base::TimeDelta::FromHours(1)); base::Time::Now() + base::TimeDelta::FromHours(1));
// Try to authenticate with password. // Try to authenticate with password.
tester->EnterPassword(user_manager::StubAccountId(), kPassword); tester->UnlockWithPassword(user_manager::StubAccountId(), kPassword);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(tester->IsLocked()); EXPECT_TRUE(tester->IsLocked());
...@@ -331,7 +270,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) { ...@@ -331,7 +270,7 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, PasswordAuthWhenAuthDisabled) {
user_manager::StubAccountId(), true /*is_enabled*/, base::nullopt); user_manager::StubAccountId(), true /*is_enabled*/, base::nullopt);
// Try to authenticate with password. // Try to authenticate with password.
tester->EnterPassword(user_manager::StubAccountId(), kPassword); tester->UnlockWithPassword(user_manager::StubAccountId(), kPassword);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_FALSE(tester->IsLocked()); EXPECT_FALSE(tester->IsLocked());
EXPECT_EQ(1, session_manager_client()->notify_lock_screen_shown_call_count()); EXPECT_EQ(1, session_manager_client()->notify_lock_screen_shown_call_count());
...@@ -346,14 +285,10 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, FingerprintAuthWhenAuthDisabled) { ...@@ -346,14 +285,10 @@ IN_PROC_BROWSER_TEST_F(ScreenLockerTest, FingerprintAuthWhenAuthDisabled) {
// Show lock screen and wait until it is shown. // Show lock screen and wait until it is shown.
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create(); std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create();
LockScreen(tester.get()); tester->Lock();
// Inject fake authentication credentials.
const std::string kPassword = "pass"; const std::string kPassword = "pass";
UserContext user_context(user_manager::UserType::USER_TYPE_REGULAR, tester->SetUnlockPassword(user_manager::StubAccountId(), kPassword);
user_manager::StubAccountId());
user_context.SetKey(Key(kPassword));
tester->InjectStubUserContext(user_context);
EXPECT_TRUE(tester->IsLocked()); EXPECT_TRUE(tester->IsLocked());
// Disable authentication for user. // Disable authentication for user.
......
...@@ -14,11 +14,17 @@ ...@@ -14,11 +14,17 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.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/screen_locker.h"
#include "chrome/browser/chromeos/login/lock/webui_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/auth_status_consumer.h"
#include "chromeos/login/auth/fake_extended_authenticator.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/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/render_frame_host.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui.h"
...@@ -83,8 +89,8 @@ class WebUIScreenLockerTester : public ScreenLockerTester { ...@@ -83,8 +89,8 @@ class WebUIScreenLockerTester : public ScreenLockerTester {
// ScreenLockerTester: // ScreenLockerTester:
bool IsLocked() override { return IsScreenLockerLocked(); } bool IsLocked() override { return IsScreenLockerLocked(); }
void EnterPassword(const AccountId& account_id, void UnlockWithPassword(const AccountId& account_id,
const std::string& password) override { const std::string& password) override {
bool result; bool result;
SetPassword(password); SetPassword(password);
...@@ -165,10 +171,11 @@ class MojoScreenLockerTester : public ScreenLockerTester { ...@@ -165,10 +171,11 @@ class MojoScreenLockerTester : public ScreenLockerTester {
login_screen.IsLockShown(&is_ui_shown); login_screen.IsLockShown(&is_ui_shown);
return IsScreenLockerLocked() && 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 { const std::string& password) override {
ash::mojom::LoginScreenTestApiAsyncWaiter login_screen(test_api_.get()); ash::mojom::LoginScreenTestApiAsyncWaiter login_screen(test_api_.get());
login_screen.SubmitPassword(account_id, password); login_screen.SubmitPassword(account_id, password);
base::RunLoop().RunUntilIdle();
} }
private: private:
...@@ -192,8 +199,25 @@ ScreenLockerTester::ScreenLockerTester() = default; ...@@ -192,8 +199,25 @@ ScreenLockerTester::ScreenLockerTester() = default;
ScreenLockerTester::~ScreenLockerTester() = default; ScreenLockerTester::~ScreenLockerTester() = default;
void ScreenLockerTester::InjectStubUserContext( void ScreenLockerTester::Lock() {
const UserContext& user_context) { 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(); auto* locker = ScreenLocker::default_screen_locker();
locker->SetAuthenticatorsForTesting( locker->SetAuthenticatorsForTesting(
base::MakeRefCounted<StubAuthenticator>(locker, user_context), base::MakeRefCounted<StubAuthenticator>(locker, user_context),
......
...@@ -12,8 +12,6 @@ class AccountId; ...@@ -12,8 +12,6 @@ class AccountId;
namespace chromeos { namespace chromeos {
class UserContext;
// ScreenLockerTester provides a high-level API to test the lock screen. This // ScreenLockerTester provides a high-level API to test the lock screen. This
// API is meant to be representation independent. // API is meant to be representation independent.
class ScreenLockerTester { class ScreenLockerTester {
...@@ -24,15 +22,19 @@ class ScreenLockerTester { ...@@ -24,15 +22,19 @@ class ScreenLockerTester {
ScreenLockerTester(); ScreenLockerTester();
virtual ~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. // Returns true if the screen is locked.
virtual bool IsLocked() = 0; virtual bool IsLocked() = 0;
// Injects StubAuthenticator that uses the credentials in |user_context|. // Enters and submits the given password for the given account.
virtual void InjectStubUserContext(const UserContext& user_context); virtual void UnlockWithPassword(const AccountId& account_id,
const std::string& password) = 0;
// Enters and submits the given password.
virtual void EnterPassword(const AccountId& account_id,
const std::string& password) = 0;
}; };
} // namespace chromeos } // namespace chromeos
......
...@@ -237,14 +237,9 @@ class ShutdownPolicyLockerTest : public ShutdownPolicyBaseTest { ...@@ -237,14 +237,9 @@ class ShutdownPolicyLockerTest : public ShutdownPolicyBaseTest {
ShutdownPolicyBaseTest::SetUpOnMainThread(); ShutdownPolicyBaseTest::SetUpOnMainThread();
// Bring up the locker screen. // Bring up the locker screen.
ScreenLocker::Show();
std::unique_ptr<chromeos::ScreenLockerTester> tester = std::unique_ptr<chromeos::ScreenLockerTester> tester =
chromeos::ScreenLockerTester::Create(); chromeos::ScreenLockerTester::Create();
content::WindowedNotificationObserver lock_state_observer( tester->Lock();
chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
content::NotificationService::AllSources());
if (!tester->IsLocked())
lock_state_observer.Wait();
ScreenLocker* screen_locker = ScreenLocker::default_screen_locker(); ScreenLocker* screen_locker = ScreenLocker::default_screen_locker();
WebUIScreenLocker* web_ui_screen_locker = WebUIScreenLocker* web_ui_screen_locker =
screen_locker->web_ui_for_testing(); screen_locker->web_ui_for_testing();
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "chrome/browser/browser_process.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/accessibility/accessibility_manager.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.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/lock/screen_locker_tester.h"
...@@ -23,17 +22,12 @@ ...@@ -23,17 +22,12 @@
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "chromeos/chromeos_switches.h" #include "chromeos/chromeos_switches.h"
#include "components/session_manager/core/session_manager.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_task_traits.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
using chromeos::ProfileHelper;
using chromeos::ScreenLocker;
using chromeos::ScreenLockerTester;
using user_manager::UserManager;
using content::BrowserThread;
namespace extensions { namespace extensions {
namespace api { namespace api {
...@@ -41,8 +35,7 @@ namespace braille_display_private { ...@@ -41,8 +35,7 @@ namespace braille_display_private {
namespace { namespace {
constexpr char kTestUserName[] = "owner@invalid.domain"; constexpr char kTestUserEmail[] = "testuser@gmail.com";
constexpr char kTestUserGaiaId[] = "0123456789";
// Used to make ReadKeys return an error. // Used to make ReadKeys return an error.
brlapi_keyCode_t kErrorKeyCode = BRLAPI_KEY_MAX; brlapi_keyCode_t kErrorKeyCode = BRLAPI_KEY_MAX;
...@@ -76,7 +69,7 @@ class MockBrlapiConnection : public BrlapiConnection { ...@@ -76,7 +69,7 @@ class MockBrlapiConnection : public BrlapiConnection {
on_data_ready_ = on_data_ready; on_data_ready_ = on_data_ready;
if (!data_->pending_keys.empty()) { if (!data_->pending_keys.empty()) {
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&MockBrlapiConnection::NotifyDataReady, base::BindOnce(&MockBrlapiConnection::NotifyDataReady,
base::Unretained(this))); base::Unretained(this)));
} }
...@@ -88,7 +81,7 @@ class MockBrlapiConnection : public BrlapiConnection { ...@@ -88,7 +81,7 @@ class MockBrlapiConnection : public BrlapiConnection {
if (data_->reappear_on_disconnect) { if (data_->reappear_on_disconnect) {
data_->display_columns *= 2; data_->display_columns *= 2;
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {content::BrowserThread::IO},
base::BindOnce( base::BindOnce(
&BrailleControllerImpl::PokeSocketDirForTesting, &BrailleControllerImpl::PokeSocketDirForTesting,
base::Unretained(BrailleControllerImpl::GetInstance()))); base::Unretained(BrailleControllerImpl::GetInstance())));
...@@ -133,12 +126,11 @@ class MockBrlapiConnection : public BrlapiConnection { ...@@ -133,12 +126,11 @@ class MockBrlapiConnection : public BrlapiConnection {
} }
private: private:
void NotifyDataReady() { void NotifyDataReady() {
on_data_ready_.Run(); on_data_ready_.Run();
if (!data_->pending_keys.empty()) { if (!data_->pending_keys.empty()) {
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&MockBrlapiConnection::NotifyDataReady, base::BindOnce(&MockBrlapiConnection::NotifyDataReady,
base::Unretained(this))); base::Unretained(this)));
} }
...@@ -276,27 +268,28 @@ IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateApiTest, DisplayStateChanges) { ...@@ -276,27 +268,28 @@ IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateApiTest, DisplayStateChanges) {
class BrailleDisplayPrivateAPIUserTest : public BrailleDisplayPrivateApiTest { class BrailleDisplayPrivateAPIUserTest : public BrailleDisplayPrivateApiTest {
public: 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 { class MockEventDelegate : public BrailleDisplayPrivateAPI::EventDelegate {
public: public:
MockEventDelegate() : event_count_(0) {} MockEventDelegate() = default;
~MockEventDelegate() override = default;
int GetEventCount() { return event_count_; } int GetEventCount() { return event_count_; }
// BrailleDisplayPrivateAPI::EventDelegate:
void BroadcastEvent(std::unique_ptr<Event> event) override { void BroadcastEvent(std::unique_ptr<Event> event) override {
++event_count_; ++event_count_;
} }
bool HasListener() override { return true; } bool HasListener() override { return true; }
private: private:
int event_count_; int event_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(MockEventDelegate);
}; };
BrailleDisplayPrivateAPIUserTest() = default;
~BrailleDisplayPrivateAPIUserTest() override = default;
MockEventDelegate* SetMockEventDelegate(BrailleDisplayPrivateAPI* api) { MockEventDelegate* SetMockEventDelegate(BrailleDisplayPrivateAPI* api) {
MockEventDelegate* delegate = new MockEventDelegate(); MockEventDelegate* delegate = new MockEventDelegate();
api->SetEventDelegateForTest( api->SetEventDelegateForTest(
...@@ -304,63 +297,42 @@ class BrailleDisplayPrivateAPIUserTest : public BrailleDisplayPrivateApiTest { ...@@ -304,63 +297,42 @@ class BrailleDisplayPrivateAPIUserTest : public BrailleDisplayPrivateApiTest {
return delegate; return delegate;
} }
void LockScreen(ScreenLockerTester* tester) { // BrailleDisplayPrivateApiTest:
ScreenLocker::Show(); void SetUpInProcessBrowserTestFixture() override {
content::WindowedNotificationObserver lock_state_observer( BrailleDisplayPrivateApiTest::SetUpInProcessBrowserTestFixture();
chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, zero_duration_mode_ =
content::NotificationService::AllSources()); std::make_unique<ui::ScopedAnimationDurationScaleMode>(
if (!tester->IsLocked()) ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
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());
} }
protected: // BrailleDisplayPrivateApiTest:
void DisableAccessibilityManagerBraille() override { void DisableAccessibilityManagerBraille() override {
// Let the accessibility manager behave as usual for these tests. // 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. IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateAPIUserTest, KeyEventOnLockScreen) {
#if defined(MEMORY_SANITIZER) std::unique_ptr<chromeos::ScreenLockerTester> tester =
#define MAYBE_KeyEventOnLockScreen DISABLED_KeyEventOnLockScreen chromeos::ScreenLockerTester::Create();
#else
#define MAYBE_KeyEventOnLockScreen KeyEventOnLockScreen // Make sure the signin profile and active profile are different.
#endif Profile* signin_profile = chromeos::ProfileHelper::GetSigninProfile();
IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateAPIUserTest, Profile* user_profile = ProfileManager::GetActiveUserProfile();
MAYBE_KeyEventOnLockScreen) { ASSERT_FALSE(signin_profile->IsSameProfile(user_profile))
std::unique_ptr<ScreenLockerTester> tester = ScreenLockerTester::Create(); << signin_profile->GetDebugName() << " vs "
// Log in. << user_profile->GetDebugName();
session_manager::SessionManager::Get()->CreateSession(
AccountId::FromUserEmailGaiaId(kTestUserName, kTestUserGaiaId), // Create API and event delegate for the signin profile.
kTestUserName, false); BrailleDisplayPrivateAPI signin_api(signin_profile);
{
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());
MockEventDelegate* signin_delegate = SetMockEventDelegate(&signin_api); MockEventDelegate* signin_delegate = SetMockEventDelegate(&signin_api);
EXPECT_EQ(0, signin_delegate->GetEventCount()); EXPECT_EQ(0, signin_delegate->GetEventCount());
// Create api and delegate for the logged in user. // Create API and delegate for the user profile.
BrailleDisplayPrivateAPI user_api(profile); BrailleDisplayPrivateAPI user_api(user_profile);
MockEventDelegate* user_delegate = SetMockEventDelegate(&user_api); MockEventDelegate* user_delegate = SetMockEventDelegate(&user_api);
// Send key event to both profiles. // Send key event to both profiles.
...@@ -371,16 +343,16 @@ IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateAPIUserTest, ...@@ -371,16 +343,16 @@ IN_PROC_BROWSER_TEST_F(BrailleDisplayPrivateAPIUserTest,
EXPECT_EQ(0, signin_delegate->GetEventCount()); EXPECT_EQ(0, signin_delegate->GetEventCount());
EXPECT_EQ(1, user_delegate->GetEventCount()); EXPECT_EQ(1, user_delegate->GetEventCount());
// Lock screen, and make sure that the key event goes to the // Lock screen, and make sure that the key event goes to the signin profile.
// signin profile. tester->Lock();
LockScreen(tester.get());
signin_api.OnBrailleKeyEvent(key_event); signin_api.OnBrailleKeyEvent(key_event);
user_api.OnBrailleKeyEvent(key_event); user_api.OnBrailleKeyEvent(key_event);
EXPECT_EQ(0, signin_delegate->GetEventCount()); EXPECT_EQ(0, signin_delegate->GetEventCount());
EXPECT_EQ(2, user_delegate->GetEventCount()); EXPECT_EQ(2, user_delegate->GetEventCount());
// Unlock screen, making sur ekey events go to the user profile again. // Unlock screen, making sure key events go to the user profile again.
DismissLockScreen(tester.get()); tester->SetUnlockPassword(AccountId::FromUserEmail(kTestUserEmail), "pass");
tester->UnlockWithPassword(AccountId::FromUserEmail(kTestUserEmail), "pass");
signin_api.OnBrailleKeyEvent(key_event); signin_api.OnBrailleKeyEvent(key_event);
user_api.OnBrailleKeyEvent(key_event); user_api.OnBrailleKeyEvent(key_event);
EXPECT_EQ(0, signin_delegate->GetEventCount()); 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