Commit 0ecd5a6e authored by Toby Huang's avatar Toby Huang Committed by Chromium LUCI CQ

Tests: Make LoggedInUserMixin more user-friendly

LoggedInUserMixin is not as easy to use as it could be. This CL
maximizes the chances that LoggedInUserMixin just works out of the
box, while minimizing the chances of mysterious time outs. The test
developer can optimize for efficiency later by tweaking the options in
LoggedInUserMixin. This CL improves the browser test login tools.

Bug: None
Change-Id: Ic5e597e68ba32e7f29daa4d5331d47298ef3fbcf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2598044
Commit-Queue: Toby Huang <tobyhuang@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarAga Wronska <agawronska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840919}
parent adb18674
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/supervised_user/logged_in_user_mixin.h" #include "chrome/browser/supervised_user/logged_in_user_mixin.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h" #include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/constants/chromeos_switches.h"
#include "components/account_id/account_id.h" #include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
...@@ -89,18 +88,11 @@ class ParentAccessServiceTest : public MixinBasedInProcessBrowserTest { ...@@ -89,18 +88,11 @@ class ParentAccessServiceTest : public MixinBasedInProcessBrowserTest {
logged_in_user_mixin_.GetAccountId())) {} logged_in_user_mixin_.GetAccountId())) {}
~ParentAccessServiceTest() override = default; ~ParentAccessServiceTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
MixinBasedInProcessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kOobeSkipPostLogin);
}
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
ASSERT_NO_FATAL_FAILURE(GetTestAccessCodeValues(&test_values_)); ASSERT_NO_FATAL_FAILURE(GetTestAccessCodeValues(&test_values_));
ParentAccessService::Get().AddObserver(test_observer_.get()); ParentAccessService::Get().AddObserver(test_observer_.get());
MixinBasedInProcessBrowserTest::SetUpOnMainThread(); MixinBasedInProcessBrowserTest::SetUpOnMainThread();
logged_in_user_mixin_.LogInUser(false /*issue_any_scope_token*/, logged_in_user_mixin_.LogInUser();
true /*wait_for_active_session*/,
true /*request_policy_update*/);
} }
void TearDownOnMainThread() override { void TearDownOnMainThread() override {
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
#include "chrome/browser/supervised_user/logged_in_user_mixin.h" #include "chrome/browser/supervised_user/logged_in_user_mixin.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h" #include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/constants/chromeos_switches.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 "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
...@@ -65,12 +64,6 @@ class ScreenTimeControllerTest : public MixinBasedInProcessBrowserTest { ...@@ -65,12 +64,6 @@ class ScreenTimeControllerTest : public MixinBasedInProcessBrowserTest {
~ScreenTimeControllerTest() override = default; ~ScreenTimeControllerTest() override = default;
// MixinBasedInProcessBrowserTest:
void SetUpCommandLine(base::CommandLine* command_line) override {
MixinBasedInProcessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kOobeSkipPostLogin);
}
// MixinBasedInProcessBrowserTest: // MixinBasedInProcessBrowserTest:
void SetUpInProcessBrowserTestFixture() override { void SetUpInProcessBrowserTestFixture() override {
MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture(); MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture();
...@@ -87,9 +80,7 @@ class ScreenTimeControllerTest : public MixinBasedInProcessBrowserTest { ...@@ -87,9 +80,7 @@ class ScreenTimeControllerTest : public MixinBasedInProcessBrowserTest {
protected: protected:
void LogInChildAndSetupClockWithTime(const char* time) { void LogInChildAndSetupClockWithTime(const char* time) {
SetupTaskRunnerWithTime(utils::TimeFromString(time)); SetupTaskRunnerWithTime(utils::TimeFromString(time));
logged_in_user_mixin_.LogInUser(false /*issue_any_scope_token*/, logged_in_user_mixin_.LogInUser();
true /*wait_for_active_session*/,
true /*request_policy_update*/);
MockClockForActiveUser(); MockClockForActiveUser();
} }
......
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
#include "components/user_manager/user.h" #include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h" #include "components/user_manager/user_manager.h"
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -106,10 +105,7 @@ class AppTimeTest : public MixinBasedInProcessBrowserTest { ...@@ -106,10 +105,7 @@ class AppTimeTest : public MixinBasedInProcessBrowserTest {
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
MixinBasedInProcessBrowserTest::SetUpOnMainThread(); MixinBasedInProcessBrowserTest::SetUpOnMainThread();
ASSERT_TRUE(embedded_test_server()->Started()); ASSERT_TRUE(embedded_test_server()->Started());
host_resolver()->AddIPLiteralRule("*", "127.0.0.1", "localhost"); logged_in_user_mixin_.LogInUser();
logged_in_user_mixin_.LogInUser(false /*issue_any_scope_token*/,
true /*wait_for_active_session*/,
true /*request_policy_update*/);
arc::SetArcPlayStoreEnabledForProfile(browser()->profile(), true); arc::SetArcPlayStoreEnabledForProfile(browser()->profile(), true);
arc_app_list_prefs_ = ArcAppListPrefs::Get(browser()->profile()); arc_app_list_prefs_ = ArcAppListPrefs::Get(browser()->profile());
......
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
#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/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/window_open_disposition.h" #include "ui/base/window_open_disposition.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -101,12 +100,7 @@ void WebTimeCalculationBrowserTest::SetUpOnMainThread() { ...@@ -101,12 +100,7 @@ void WebTimeCalculationBrowserTest::SetUpOnMainThread() {
ASSERT_TRUE(embedded_test_server()->Started()); ASSERT_TRUE(embedded_test_server()->Started());
// Resolve everything to localhost. logged_in_user_mixin_.LogInUser();
host_resolver()->AddIPLiteralRule("*", "127.0.0.1", "localhost");
logged_in_user_mixin_.LogInUser(false /*issue_any_scope_token*/,
true /*wait_for_active_session*/,
true /*request_policy_update*/);
profile_ = browser()->profile(); profile_ = browser()->profile();
// During tests, AppService doesn't notify AppActivityRegistry that chrome app // During tests, AppService doesn't notify AppActivityRegistry that chrome app
......
...@@ -38,7 +38,6 @@ ...@@ -38,7 +38,6 @@
#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/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/window_open_disposition.h" #include "ui/base/window_open_disposition.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -130,12 +129,7 @@ void WebTimeLimitEnforcerThrottleTest::SetUpOnMainThread() { ...@@ -130,12 +129,7 @@ void WebTimeLimitEnforcerThrottleTest::SetUpOnMainThread() {
ASSERT_TRUE(embedded_test_server()->Started()); ASSERT_TRUE(embedded_test_server()->Started());
// Resolve everything to localhost. logged_in_user_mixin_.LogInUser();
host_resolver()->AddIPLiteralRule("*", "127.0.0.1", "localhost");
logged_in_user_mixin_.LogInUser(false /*issue_any_scope_token*/,
true /*wait_for_active_session*/,
true /*request_policy_update*/);
} }
bool WebTimeLimitEnforcerThrottleTest::IsErrorPageBeingShownInWebContents( bool WebTimeLimitEnforcerThrottleTest::IsErrorPageBeingShownInWebContents(
......
...@@ -401,11 +401,7 @@ class LoginApitestWithEnterpriseUser : public LoginApitest { ...@@ -401,11 +401,7 @@ class LoginApitestWithEnterpriseUser : public LoginApitest {
~LoginApitestWithEnterpriseUser() override = default; ~LoginApitestWithEnterpriseUser() override = default;
void LoginUser() { void LoginUser() { logged_in_user_mixin_.LogInUser(); }
logged_in_user_mixin_.LogInUser(/*issue_any_scope_token=*/false,
/*wait_for_active_session=*/true,
/*request_policy_update=*/true);
}
void SetUpInSessionExtension() override { void SetUpInSessionExtension() override {
AccountId account_id = logged_in_user_mixin_.GetAccountId(); AccountId account_id = logged_in_user_mixin_.GetAccountId();
......
...@@ -397,9 +397,7 @@ class UserManagementDisclosureChildTest ...@@ -397,9 +397,7 @@ class UserManagementDisclosureChildTest
// having logged a child account into a session and having locked the screen. // having logged a child account into a session and having locked the screen.
IN_PROC_BROWSER_TEST_F(UserManagementDisclosureChildTest, IN_PROC_BROWSER_TEST_F(UserManagementDisclosureChildTest,
PRE_EnterpriseIconVisibleChildUser) { PRE_EnterpriseIconVisibleChildUser) {
logged_in_user_mixin_.LogInUser(false /*issue_any_scope_token*/, logged_in_user_mixin_.LogInUser();
true /*wait_for_active_session*/,
true /*request_policy_update*/);
ScreenLockerTester screen_locker_tester; ScreenLockerTester screen_locker_tester;
screen_locker_tester.Lock(); screen_locker_tester.Lock();
EXPECT_FALSE(ash::LoginScreenTestApi::IsManagedIconShown( EXPECT_FALSE(ash::LoginScreenTestApi::IsManagedIconShown(
......
...@@ -79,8 +79,7 @@ class SupervisionTransitionScreenTest ...@@ -79,8 +79,7 @@ class SupervisionTransitionScreenTest
// WaitForActiveSession() otherwise. // WaitForActiveSession() otherwise.
logged_in_user_mixin_.LogInUser( logged_in_user_mixin_.LogInUser(
false /*issue_any_scope_token*/, false /*issue_any_scope_token*/,
content::IsPreTest() /*wait_for_active_session*/, content::IsPreTest() /*wait_for_active_session*/);
true /*request_policy_update*/);
} }
// The tests simulate user type changes between regular and child user. // The tests simulate user type changes between regular and child user.
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h" #include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/constants/chromeos_switches.h"
#include "components/arc/arc_features.h" #include "components/arc/arc_features.h"
#include "components/arc/arc_prefs.h" #include "components/arc/arc_prefs.h"
#include "components/policy/core/browser/browser_policy_connector.h" #include "components/policy/core/browser/browser_policy_connector.h"
...@@ -59,16 +58,12 @@ class UserCloudPolicyManagerTest ...@@ -59,16 +58,12 @@ class UserCloudPolicyManagerTest
MixinBasedInProcessBrowserTest::TearDown(); MixinBasedInProcessBrowserTest::TearDown();
} }
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(chromeos::switches::kOobeSkipPostLogin);
MixinBasedInProcessBrowserTest::SetUpCommandLine(command_line);
}
// Sets up fake GAIA for specified user login, and requests login for the user // Sets up fake GAIA for specified user login, and requests login for the user
// (using LoggedInUserMixin). // (using LoggedInUserMixin).
void StartUserLogIn(bool wait_for_active_session) { void StartUserLogIn(bool wait_for_active_session) {
logged_in_user_mixin_.LogInUser(true /*issue_any_scope_token*/, logged_in_user_mixin_.LogInUser(true /*issue_any_scope_token*/,
wait_for_active_session); wait_for_active_session,
/*request_policy_update=*/false);
} }
protected: protected:
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include "chrome/browser/chromeos/login/wizard_controller.h"
#include "chromeos/constants/chromeos_switches.h" #include "chromeos/constants/chromeos_switches.h"
#include "chromeos/login/auth/stub_authenticator_builder.h" #include "chromeos/login/auth/stub_authenticator_builder.h"
#include "chromeos/login/auth/user_context.h" #include "chromeos/login/auth/user_context.h"
...@@ -49,7 +50,9 @@ LoggedInUserMixin::LoggedInUserMixin( ...@@ -49,7 +50,9 @@ LoggedInUserMixin::LoggedInUserMixin(
AccountId::FromUserEmailGaiaId(FakeGaiaMixin::kFakeUserEmail, AccountId::FromUserEmailGaiaId(FakeGaiaMixin::kFakeUserEmail,
FakeGaiaMixin::kFakeUserGaiaId)), FakeGaiaMixin::kFakeUserGaiaId)),
ConvertUserType(type)), ConvertUserType(type)),
login_manager_(mixin_host, GetInitialUsers(user_, include_initial_user)), login_manager_(mixin_host,
GetInitialUsers(user_, include_initial_user),
&fake_gaia_),
local_policy_server_(mixin_host), local_policy_server_(mixin_host),
user_policy_(mixin_host, user_policy_(mixin_host,
user_.account_id, user_.account_id,
...@@ -72,6 +75,8 @@ void LoggedInUserMixin::SetUpOnMainThread() { ...@@ -72,6 +75,8 @@ void LoggedInUserMixin::SetUpOnMainThread() {
// account.google.com requests would never reach fake GAIA server without // account.google.com requests would never reach fake GAIA server without
// this. // this.
test_base_->host_resolver()->AddRule("*", "127.0.0.1"); test_base_->host_resolver()->AddRule("*", "127.0.0.1");
// Ensures logging in doesn't hang on the post login Gaia screens.
WizardController::SkipPostLoginScreensForTesting();
} }
void LoggedInUserMixin::LogInUser(bool issue_any_scope_token, void LoggedInUserMixin::LogInUser(bool issue_any_scope_token,
...@@ -89,17 +94,14 @@ void LoggedInUserMixin::LogInUser(bool issue_any_scope_token, ...@@ -89,17 +94,14 @@ void LoggedInUserMixin::LogInUser(bool issue_any_scope_token,
FakeGaiaMixin::kFakeRefreshToken); FakeGaiaMixin::kFakeRefreshToken);
} }
if (request_policy_update) { if (request_policy_update) {
// Set up policy, which prevents the call to LoginAndWaitForActiveSession() // Child users require user policy, set up an empty one so the user can get
// below from hanging indefinitely in some test scenarios. // through login.
GetUserPolicyMixin()->RequestPolicyUpdate(); GetUserPolicyMixin()->RequestPolicyUpdate();
} }
if (wait_for_active_session) { if (wait_for_active_session) {
login_manager_.LoginAndWaitForActiveSession(user_context); login_manager_.LoginAndWaitForActiveSession(user_context);
// Set the private |browser_| member in InProcessBrowserTest. // If should_launch_browser was set to true, then ensures
// Otherwise calls to InProcessBrowserTest::browser() returns null and leads // InProcessBrowserTest::browser() doesn't return nullptr.
// to segmentation faults.
// Note: |browser_| is only non-null if should_launch_browser was set to
// true in the constructor.
test_base_->SelectFirstBrowser(); test_base_->SelectFirstBrowser();
} else { } else {
login_manager_.AttemptLoginUsingAuthenticator( login_manager_.AttemptLoginUsingAuthenticator(
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#ifndef CHROME_BROWSER_SUPERVISED_USER_LOGGED_IN_USER_MIXIN_H_ #ifndef CHROME_BROWSER_SUPERVISED_USER_LOGGED_IN_USER_MIXIN_H_
#define CHROME_BROWSER_SUPERVISED_USER_LOGGED_IN_USER_MIXIN_H_ #define CHROME_BROWSER_SUPERVISED_USER_LOGGED_IN_USER_MIXIN_H_
#include "base/macros.h"
#include "base/optional.h" #include "base/optional.h"
#include "chrome/browser/chromeos/login/test/embedded_test_server_mixin.h" #include "chrome/browser/chromeos/login/test/embedded_test_server_mixin.h"
#include "chrome/browser/chromeos/login/test/fake_gaia_mixin.h" #include "chrome/browser/chromeos/login/test/fake_gaia_mixin.h"
...@@ -60,15 +59,13 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin { ...@@ -60,15 +59,13 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin {
// that can be passed into this constructor. // that can be passed into this constructor.
// |test_base|: just pass in a pointer to the browser test class. // |test_base|: just pass in a pointer to the browser test class.
// |should_launch_browser| determines whether a browser instance is launched // |should_launch_browser| determines whether a browser instance is launched
// after successful login. Call SelectFirstBrowser() afterwards to ensure // after successful login.
// calls to browser() don't return nullptr. LogInUser() already calls
// SelectFirstBrowser() for convenience.
// |account_id| is the desired test account id for logging in. The default // |account_id| is the desired test account id for logging in. The default
// test account already works for the majority of test cases, unless an // test account already works for the majority of test cases, unless an
// enterprise account is needed for setting up policy. // enterprise account is needed for setting up policy.
// |include_initial_user| determines whether the TestUserInfo should be passed // |include_initial_user| if true, then the user already exists on the login
// to the initial users list of the LoginManagerMixin. Excluding the initial // screen. Otherwise, the user is newly added to the device and the OOBE Gaia
// user causes the OOBE GAIA screen to show on start-up. // screen will show on start-up.
// |use_local_policy_server| determines if the LocalPolicyTestServerMixin // |use_local_policy_server| determines if the LocalPolicyTestServerMixin
// should be passed into the UserPolicyMixin. // should be passed into the UserPolicyMixin.
LoggedInUserMixin(InProcessBrowserTestMixinHost* mixin_host, LoggedInUserMixin(InProcessBrowserTestMixinHost* mixin_host,
...@@ -78,7 +75,10 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin { ...@@ -78,7 +75,10 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin {
bool should_launch_browser = true, bool should_launch_browser = true,
base::Optional<AccountId> account_id = base::nullopt, base::Optional<AccountId> account_id = base::nullopt,
bool include_initial_user = true, bool include_initial_user = true,
// TODO(crbug/1112885): Remove this parameter.
bool use_local_policy_server = true); bool use_local_policy_server = true);
LoggedInUserMixin(const LoggedInUserMixin&) = delete;
LoggedInUserMixin& operator=(const LoggedInUserMixin&) = delete;
~LoggedInUserMixin() override; ~LoggedInUserMixin() override;
// InProcessBrowserTestMixin: // InProcessBrowserTestMixin:
...@@ -93,7 +93,7 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin { ...@@ -93,7 +93,7 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin {
// * If |request_policy_update|, UserPolicyMixin will set up user policy. // * If |request_policy_update|, UserPolicyMixin will set up user policy.
void LogInUser(bool issue_any_scope_token = false, void LogInUser(bool issue_any_scope_token = false,
bool wait_for_active_session = true, bool wait_for_active_session = true,
bool request_policy_update = false); bool request_policy_update = true);
LoginManagerMixin* GetLoginManagerMixin() { return &login_manager_; } LoginManagerMixin* GetLoginManagerMixin() { return &login_manager_; }
...@@ -123,8 +123,6 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin { ...@@ -123,8 +123,6 @@ class LoggedInUserMixin : public InProcessBrowserTestMixin {
FakeGaiaMixin fake_gaia_; FakeGaiaMixin fake_gaia_;
InProcessBrowserTest* test_base_; InProcessBrowserTest* test_base_;
DISALLOW_COPY_AND_ASSIGN(LoggedInUserMixin);
}; };
} // namespace chromeos } // namespace chromeos
......
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h" #include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h" #include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
using content::NavigationController; using content::NavigationController;
...@@ -198,8 +197,6 @@ void SupervisedUserNavigationThrottleTest::SetUp() { ...@@ -198,8 +197,6 @@ void SupervisedUserNavigationThrottleTest::SetUp() {
void SupervisedUserNavigationThrottleTest::SetUpOnMainThread() { void SupervisedUserNavigationThrottleTest::SetUpOnMainThread() {
MixinBasedInProcessBrowserTest::SetUpOnMainThread(); MixinBasedInProcessBrowserTest::SetUpOnMainThread();
// Resolve everything to localhost.
host_resolver()->AddIPLiteralRule("*", "127.0.0.1", "localhost");
ASSERT_TRUE(embedded_test_server()->Started()); ASSERT_TRUE(embedded_test_server()->Started());
......
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