Skip to content

Commit

Permalink
Move cros settings test fixtures into common base
Browse files Browse the repository at this point in the history
This commit refactors duplicated code of browser test fixtures for tests
using the settings test api into a common base class.


Bug: b:243696986
Change-Id: I04a669a90b4e2a75e2003d7369050b745580ea82
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4320927
Reviewed-by: Wes Okuhara <wesokuhara@google.com>
Commit-Queue: Martin Bidlingmaier <mbid@google.com>
Cr-Commit-Position: refs/heads/main@{#1115644}
  • Loading branch information
mbid authored and Chromium LUCI CQ committed Mar 10, 2023
1 parent 694744a commit b0c0d15
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 179 deletions.
3 changes: 3 additions & 0 deletions chrome/browser/ui/webui/settings/ash/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ static_library("test_support") {
sources = [
"os_settings_browser_test_mixin.cc",
"os_settings_browser_test_mixin.h",
"os_settings_lock_screen_browser_test_base.cc",
"os_settings_lock_screen_browser_test_base.h",
]
deps = [
"//ash/constants",
"//chrome:browser_tests_pak",
"//chrome/browser/ash:test_support",
"//chrome/browser/ui",
"//chrome/test:test_support",
"//chrome/test/data/webui:resources_grit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/login/test/cryptohome_mixin.h"
#include "chrome/browser/ui/webui/settings/ash/os_settings_browser_test_mixin.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/ash/components/dbus/userdataauth/fake_userdataauth_client.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user_names.h"
#include "chrome/browser/ui/webui/settings/ash/os_settings_lock_screen_browser_test_base.h"
#include "content/public/test/browser_test.h"

namespace {

const char kCorrectPassword[] = "correct-password";
const char kIncorrectPassword[] = "incorrect-password";

} // namespace

namespace ash::settings {

// Test of the authentication dialog in the lock screen page in os-settings.
class OSSettingsLockScreenAuthenticationTest
: public MixinBasedInProcessBrowserTest {
: public OSSettingsLockScreenBrowserTestBase {
public:
OSSettingsLockScreenAuthenticationTest() = default;

void SetUpOnMainThread() override {
MixinBasedInProcessBrowserTest::SetUpOnMainThread();
FakeUserDataAuthClient::TestApi::Get()->set_enable_auth_check(true);

auto account = AccountId::FromUserEmail(user_manager::kStubUserEmail);
cryptohome_.MarkUserAsExisting(account);
cryptohome_.AddGaiaPassword(account, kCorrectPassword);
}

// Opens the ChromeOS settings app and goes to the "lock screen" section.
// Does not enter a password.
mojom::LockScreenSettingsAsyncWaiter OpenLockScreenSettings() {
os_settings_driver_remote_ =
mojo::Remote{os_settings_mixin_.OpenOSSettings()};
lock_screen_settings_remote_ = mojo::Remote{
mojom::OSSettingsDriverAsyncWaiter{os_settings_driver_remote_.get()}
.GoToLockScreenSettings()};
return mojom::LockScreenSettingsAsyncWaiter{
lock_screen_settings_remote_.get()};
}

private:
ash::CryptohomeMixin cryptohome_{&mixin_host_};
OSSettingsBrowserTestMixin os_settings_mixin_{&mixin_host_};

mojo::Remote<mojom::OSSettingsDriver> os_settings_driver_remote_;
mojo::Remote<mojom::LockScreenSettings> lock_screen_settings_remote_;
static constexpr const char* kCorrectPassword =
OSSettingsLockScreenBrowserTestBase::kPassword;
static constexpr char kIncorrectPassword[] = "incorrect-password";
};

IN_PROC_BROWSER_TEST_F(OSSettingsLockScreenAuthenticationTest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/settings/ash/os_settings_lock_screen_browser_test_base.h"

namespace ash::settings {

OSSettingsLockScreenBrowserTestBase::OSSettingsLockScreenBrowserTestBase() {
// We configure FakeUserDataAuthClient (via `cryptohome_`) here and not
// later because the global PinBackend object reads whether or not
// cryptohome PINs are supported on startup. If we set up
// FakeUserDataAuthClient in SetUpOnMainThread, then PinBackend would
// determine whether PINs are supported before we have configured
// FakeUserDataAuthClient.
cryptohome_.set_enable_auth_check(true);
cryptohome_.set_supports_low_entropy_credentials(true);
cryptohome_.MarkUserAsExisting(GetAccountId());
cryptohome_.AddGaiaPassword(GetAccountId(), kPassword);
}

OSSettingsLockScreenBrowserTestBase::~OSSettingsLockScreenBrowserTestBase() =
default;

void OSSettingsLockScreenBrowserTestBase::SetUpOnMainThread() {
MixinBasedInProcessBrowserTest::SetUpOnMainThread();
logged_in_user_mixin_.LogInUser();
}

mojom::LockScreenSettingsAsyncWaiter
OSSettingsLockScreenBrowserTestBase::OpenLockScreenSettings() {
os_settings_driver_remote_ =
mojo::Remote{os_settings_mixin_.OpenOSSettings()};

lock_screen_settings_remote_ = mojo::Remote{
mojom::OSSettingsDriverAsyncWaiter{os_settings_driver_remote_.get()}
.GoToLockScreenSettings()};

return mojom::LockScreenSettingsAsyncWaiter{
lock_screen_settings_remote_.get()};
}

mojom::LockScreenSettingsAsyncWaiter
OSSettingsLockScreenBrowserTestBase::OpenLockScreenSettingsAndAuthenticate() {
OpenLockScreenSettings().Authenticate(kPassword);
// The mojom AsyncWaiter classes have deleted copy constructors even though
// they only hold a non-owning pointer to a mojo remote. This restriction
// should probably be dropped, so that we can just return the async waiter
// created by the call to `OpenLockScreenSettings` here. As a workaround, we
// simply create a new waiter.
return mojom::LockScreenSettingsAsyncWaiter{
lock_screen_settings_remote_.get()};
}

const AccountId& OSSettingsLockScreenBrowserTestBase::GetAccountId() {
return logged_in_user_mixin_.GetAccountId();
}

} // namespace ash::settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_ASH_OS_SETTINGS_LOCK_SCREEN_BROWSER_TEST_BASE_H_
#define CHROME_BROWSER_UI_WEBUI_SETTINGS_ASH_OS_SETTINGS_LOCK_SCREEN_BROWSER_TEST_BASE_H_

#include "ash/constants/ash_features.h"
#include "chrome/browser/ash/login/test/cryptohome_mixin.h"
#include "chrome/browser/ash/login/test/logged_in_user_mixin.h"
#include "chrome/browser/ui/webui/settings/ash/os_settings_browser_test_mixin.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chrome/test/data/webui/settings/chromeos/test_api.test-mojom-test-utils.h"
#include "components/account_id/account_id.h"

namespace ash::settings {

// Fixture for browser tests of the "lock screen" section in
// chrome://os-settings. The fixture creates a user with a gaia password and
// logs in. It provides methods to open the os settings page and navigate to
// the "lock screen" section. Tests can then operate this section via its test
// api.
class OSSettingsLockScreenBrowserTestBase
: public MixinBasedInProcessBrowserTest {
public:
// The password of the user that is set up by this fixture.
static constexpr char kPassword[] = "the-password";

OSSettingsLockScreenBrowserTestBase();
~OSSettingsLockScreenBrowserTestBase() override;

void SetUpOnMainThread() override;

// Opens the ChromeOS settings app and navigates to the "lock screen"
// section. The return value is an AsyncWaiter that can be used to interact
// with the WebUI. The returned AsyncWaiter is valid until this function is
// called again.
mojom::LockScreenSettingsAsyncWaiter OpenLockScreenSettings();

// Calls `OpenLockScreenSettings` and authenticates.
mojom::LockScreenSettingsAsyncWaiter OpenLockScreenSettingsAndAuthenticate();

// The account ID of the user set up by this fixture.
const AccountId& GetAccountId();

protected:
CryptohomeMixin cryptohome_{&mixin_host_};
LoggedInUserMixin logged_in_user_mixin_{
&mixin_host_, LoggedInUserMixin::LogInType::kRegular,
embedded_test_server(), this};
OSSettingsBrowserTestMixin os_settings_mixin_{&mixin_host_};

private:
mojo::Remote<mojom::OSSettingsDriver> os_settings_driver_remote_;
mojo::Remote<mojom::LockScreenSettings> lock_screen_settings_remote_;
};

} // namespace ash::settings

#endif // CHROME_BROWSER_UI_WEBUI_SETTINGS_ASH_OS_SETTINGS_LOCK_SCREEN_BROWSER_TEST_BASE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,42 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/constants/ash_features.h"
#include "chrome/browser/ash/login/test/cryptohome_mixin.h"
#include "chrome/browser/ash/login/test/logged_in_user_mixin.h"
#include "ash/constants/ash_pref_names.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/settings/ash/os_settings_browser_test_mixin.h"
#include "chrome/browser/ui/webui/settings/ash/os_settings_lock_screen_browser_test_base.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/ash/components/dbus/userdataauth/fake_userdataauth_client.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user_names.h"
#include "content/public/test/browser_test.h"

namespace ash::settings {

namespace {

const char kPassword[] = "the-password";
const char kFirstPin[] = "111111";
const char kSecondPin[] = "22222222";
const char kIncorrectPin[] = "333333333";

} // namespace

// Tests PIN-related settings in the ChromeOS settings page.
class OSSettingsPinSetupTest : public MixinBasedInProcessBrowserTest {
class OSSettingsPinSetupTest : public OSSettingsLockScreenBrowserTestBase {
public:
OSSettingsPinSetupTest() {
// We configure FakeUserDataAuthClient here and not later because the
// global PinBackend object reads whether or not cryptohome PINs are
// supported on startup. If we set up FakeUserDataAuthClient in
// SetUpOnMainThread, then PinBackend would determine whether PINs are
// supported before we have configured FakeUserDataAuthClient.
UserDataAuthClient::InitializeFake();
cryptohome_.set_enable_auth_check(true);
cryptohome_.set_supports_low_entropy_credentials(true);
cryptohome_.MarkUserAsExisting(GetAccountId());
cryptohome_.AddGaiaPassword(GetAccountId(), kPassword);
}

void SetUpOnMainThread() override {
MixinBasedInProcessBrowserTest::SetUpOnMainThread();
logged_in_user_mixin_.LogInUser();
}

// Opens the ChromeOS settings app, goes to the "lock screen" section and
// enters the password.
mojom::LockScreenSettingsAsyncWaiter OpenLockScreenSettings() {
CHECK(!os_settings_driver_remote_.is_bound());
os_settings_driver_remote_ =
mojo::Remote{os_settings_mixin_.OpenOSSettings()};

CHECK(!lock_screen_settings_remote_.is_bound());
lock_screen_settings_remote_ = mojo::Remote{
mojom::OSSettingsDriverAsyncWaiter{os_settings_driver_remote_.get()}
.GoToLockScreenSettings()};

mojom::LockScreenSettingsAsyncWaiter{lock_screen_settings_remote_.get()}
.Authenticate(kPassword);
return mojom::LockScreenSettingsAsyncWaiter{
lock_screen_settings_remote_.get()};
}

bool GetPinAutoSubmitState() {
PrefService& Prefs() {
PrefService* service =
ProfileHelper::Get()->GetProfileByAccountId(GetAccountId())->GetPrefs();
CHECK(service);
return service->GetBoolean(::prefs::kPinUnlockAutosubmitEnabled);
return *service;
}

const AccountId& GetAccountId() {
return logged_in_user_mixin_.GetAccountId();
bool GetPinAutoSubmitState() {
return Prefs().GetBoolean(::prefs::kPinUnlockAutosubmitEnabled);
}

protected:
base::test::ScopedFeatureList feature_list_;

CryptohomeMixin cryptohome_{&mixin_host_};
LoggedInUserMixin logged_in_user_mixin_{
&mixin_host_, LoggedInUserMixin::LogInType::kRegular,
embedded_test_server(), this};
OSSettingsBrowserTestMixin os_settings_mixin_{&mixin_host_};

mojo::Remote<mojom::OSSettingsDriver> os_settings_driver_remote_;
mojo::Remote<mojom::LockScreenSettings> lock_screen_settings_remote_;
};

// Tests that the happy path of setting and removing PINs works.
IN_PROC_BROWSER_TEST_F(OSSettingsPinSetupTest, SetRemove) {
auto lock_screen_settings = OpenLockScreenSettings();
auto lock_screen_settings = OpenLockScreenSettingsAndAuthenticate();
lock_screen_settings.AssertIsUsingPin(false);

// Remove the pin. Nothing should happen.
Expand Down Expand Up @@ -131,7 +75,7 @@ IN_PROC_BROWSER_TEST_F(OSSettingsPinSetupTest, SetRemove) {

// Tests enabling and disabling autosubmit.
IN_PROC_BROWSER_TEST_F(OSSettingsPinSetupTest, Autosubmit) {
auto lock_screen_settings = OpenLockScreenSettings();
auto lock_screen_settings = OpenLockScreenSettingsAndAuthenticate();

// Set a pin. Autosubmit should be enabled.
lock_screen_settings.SetPin(kFirstPin);
Expand Down

0 comments on commit b0c0d15

Please sign in to comment.