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_;
......
...@@ -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