1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_SUPERVISED_USER_LOGGED_IN_USER_MIXIN_H_ 6 #define CHROME_BROWSER_SUPERVISED_USER_LOGGED_IN_USER_MIXIN_H_ 7 8 #include "base/macros.h" 9 #include "base/optional.h" 10 #include "chrome/browser/chromeos/login/test/embedded_test_server_mixin.h" 11 #include "chrome/browser/chromeos/login/test/fake_gaia_mixin.h" 12 #include "chrome/browser/chromeos/login/test/local_policy_test_server_mixin.h" 13 #include "chrome/browser/chromeos/login/test/login_manager_mixin.h" 14 #include "chrome/browser/chromeos/login/test/user_policy_mixin.h" 15 #include "chrome/browser/chromeos/policy/user_policy_test_helper.h" 16 #include "chrome/test/base/mixin_based_in_process_browser_test.h" 17 18 class AccountId; 19 20 namespace chromeos { 21 22 // Compound mixin class for easily logging in as regular or child accounts for 23 // browser tests. Initiates other mixins required to log in users, sets up their 24 // user policies and gaia auth. 25 // To use: 26 // * Make your browser test class inherit from MixinBasedInProcessBrowserTest. 27 // * Instantiate this class while passing in the inherited mixin_host_ member to 28 // the constructor. 29 // Note: the desired LogInType must be known at construction time. 30 // * Pass the inherited embedded_test_server() and pointer to 31 // InProcessBrowserTest instance into the constructor as well. 32 // * Call LogInUser() to log in. 33 // Example: 34 /* 35 class MyBrowserTestClass : public MixinBasedInProcessBrowserTest { 36 protected: 37 void SetUpOnMainThread() override { 38 MixinBasedInProcessBrowserTest::SetUpOnMainThread(); 39 // The call below logs in as child user. 40 logged_in_user_mixin_.LogInUser(); 41 } 42 43 private: 44 LoggedInUserMixin logged_in_user_mixin_{&mixin_host_, 45 LoggedInUserMixin::LogInType::kChild, 46 embedded_test_server(), this}; 47 }; 48 */ 49 class LoggedInUserMixin : public InProcessBrowserTestMixin { 50 public: 51 enum class LogInType { kRegular, kChild }; 52 53 // |mixin_host| coordinates the other mixins. Since your browser test class 54 // inherits from MixinBasedInProcessBrowserTest, there is an inherited 55 // mixin_host_ member that can be passed into this constructor. 56 // |type| specifies the desired user log in type, currently either regular or 57 // child. 58 // |embedded_test_server|: your browser test class should already inherit from 59 // BrowserTestBase. That means there is an inherited embedded_test_server() 60 // that can be passed into this constructor. 61 // |test_base|: just pass in a pointer to the browser test class. 62 // |should_launch_browser| determines whether a browser instance is launched 63 // after successful login. Call SelectFirstBrowser() afterwards to ensure 64 // calls to browser() don't return nullptr. LogInUser() already calls 65 // SelectFirstBrowser() for convenience. 66 // |account_id| is the desired test account id for logging in. The default 67 // test account already works for the majority of test cases, unless an 68 // enterprise account is needed for setting up policy. 69 // |include_initial_user| determines whether the TestUserInfo should be passed 70 // to the initial users list of the LoginManagerMixin. Excluding the initial 71 // user causes the OOBE GAIA screen to show on start-up. 72 // |use_local_policy_server| determines if the LocalPolicyTestServerMixin 73 // should be passed into the UserPolicyMixin. 74 LoggedInUserMixin(InProcessBrowserTestMixinHost* mixin_host, 75 LogInType type, 76 net::EmbeddedTestServer* embedded_test_server, 77 InProcessBrowserTest* test_base, 78 bool should_launch_browser = true, 79 base::Optional<AccountId> account_id = base::nullopt, 80 bool include_initial_user = true, 81 bool use_local_policy_server = true); 82 ~LoggedInUserMixin() override; 83 84 // InProcessBrowserTestMixin: 85 void SetUpOnMainThread() override; 86 87 // Log in as regular or child account depending on the |type| argument passed 88 // to the constructor. 89 // * If |issue_any_scope_token|, FakeGaiaMixin will issue a special all-access 90 // token associated with the test refresh token. Only matters for child login. 91 // * If |wait_for_active_session|, LoginManagerMixin will wait for the session 92 // state to change to ACTIVE after logging in. 93 // * If |request_policy_update|, UserPolicyMixin will set up user policy. 94 void LogInUser(bool issue_any_scope_token = false, 95 bool wait_for_active_session = true, 96 bool request_policy_update = false); 97 GetLoginManagerMixin()98 LoginManagerMixin* GetLoginManagerMixin() { return &login_manager_; } 99 GetUserPolicyMixin()100 UserPolicyMixin* GetUserPolicyMixin() { return &user_policy_; } 101 GetLocalPolicyTestServerMixin()102 LocalPolicyTestServerMixin* GetLocalPolicyTestServerMixin() { 103 return &local_policy_server_; 104 } 105 GetUserPolicyTestHelper()106 policy::UserPolicyTestHelper* GetUserPolicyTestHelper() { 107 return &user_policy_helper_; 108 } 109 GetAccountId()110 const AccountId& GetAccountId() { return user_.account_id; } 111 GetFakeGaiaMixin()112 FakeGaiaMixin* GetFakeGaiaMixin() { return &fake_gaia_; } 113 114 private: 115 LoginManagerMixin::TestUserInfo user_; 116 LoginManagerMixin login_manager_; 117 118 LocalPolicyTestServerMixin local_policy_server_; 119 UserPolicyMixin user_policy_; 120 policy::UserPolicyTestHelper user_policy_helper_; 121 122 EmbeddedTestServerSetupMixin embedded_test_server_setup_; 123 FakeGaiaMixin fake_gaia_; 124 125 InProcessBrowserTest* test_base_; 126 127 DISALLOW_COPY_AND_ASSIGN(LoggedInUserMixin); 128 }; 129 130 } // namespace chromeos 131 132 #endif // CHROME_BROWSER_SUPERVISED_USER_LOGGED_IN_USER_MIXIN_H_ 133