Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/chromeos/arc/arc_util_unittest.cc

Issue 2642783003: Move more utility functions to arc_util. (Closed)
Patch Set: address comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 #include "chrome/browser/chromeos/arc/arc_util.h"
6
7 #include <memory>
8
9 #include "base/command_line.h"
10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/test/scoped_command_line.h"
13 #include "chrome/browser/chromeos/login/supervised/supervised_user_creation_flow .h"
14 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
16 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
17 #include "chrome/browser/chromeos/profiles/profile_helper.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/signin/core/account_id/account_id.h"
21 #include "components/user_manager/user_manager.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace arc {
26 namespace util {
27
28 namespace {
29
30 constexpr char kTestProfileName[] = "user@gmail.com";
31 constexpr char kTestGaiaId[] = "1234567890";
32
33 class ScopedLogIn {
34 public:
35 ScopedLogIn(
36 chromeos::FakeChromeUserManager* fake_user_manager,
37 const AccountId& account_id,
38 user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR)
39 : fake_user_manager_(fake_user_manager), account_id_(account_id) {
40 switch (user_type) {
41 case user_manager::USER_TYPE_REGULAR:
42 LogIn();
43 break;
44 case user_manager::USER_TYPE_ARC_KIOSK_APP:
45 LogInArcKioskApp();
46 break;
47 default:
48 NOTREACHED();
49 }
50 }
51
52 ~ScopedLogIn() { LogOut(); }
53
54 private:
55 void LogIn() {
56 fake_user_manager_->AddUser(account_id_);
57 fake_user_manager_->LoginUser(account_id_);
58 }
59
60 void LogInArcKioskApp() {
61 fake_user_manager_->AddArcKioskAppUser(account_id_);
62 fake_user_manager_->LoginUser(account_id_);
63 }
64
65 void LogOut() { fake_user_manager_->RemoveUserFromList(account_id_); }
66
67 chromeos::FakeChromeUserManager* fake_user_manager_;
68 const AccountId account_id_;
69
70 DISALLOW_COPY_AND_ASSIGN(ScopedLogIn);
71 };
72
73 } // namespace
74
75 class ChromeArcUtilTest : public testing::Test {
76 public:
77 ChromeArcUtilTest() = default;
78 ~ChromeArcUtilTest() override = default;
79
80 void SetUp() override {
81 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>();
82 command_line_->GetProcessCommandLine()->InitFromArgv({"", "--enable-arc"});
83
84 user_manager_enabler_ =
85 base::MakeUnique<chromeos::ScopedUserManagerEnabler>(
86 new chromeos::FakeChromeUserManager());
87 chromeos::WallpaperManager::Initialize();
88 profile_ = base::MakeUnique<TestingProfile>();
89 profile_->set_profile_name(kTestProfileName);
90 }
91
92 void TearDown() override {
93 profile_.reset();
94 chromeos::WallpaperManager::Shutdown();
95 user_manager_enabler_.reset();
96 command_line_.reset();
97 }
98
99 TestingProfile* profile() { return profile_.get(); }
100
101 chromeos::FakeChromeUserManager* GetFakeUserManager() const {
102 return static_cast<chromeos::FakeChromeUserManager*>(
103 user_manager::UserManager::Get());
104 }
105
106 void LogIn() {
107 const auto account_id = AccountId::FromUserEmailGaiaId(
108 profile()->GetProfileUserName(), kTestGaiaId);
109 GetFakeUserManager()->AddUser(account_id);
110 GetFakeUserManager()->LoginUser(account_id);
111 }
112
113 private:
114 std::unique_ptr<base::test::ScopedCommandLine> command_line_;
115 content::TestBrowserThreadBundle thread_bundle_;
116 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
117 std::unique_ptr<TestingProfile> profile_;
118
119 DISALLOW_COPY_AND_ASSIGN(ChromeArcUtilTest);
120 };
121
122 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile) {
123 ScopedLogIn login(GetFakeUserManager(),
124 AccountId::FromUserEmailGaiaId(
125 profile()->GetProfileUserName(), kTestGaiaId));
126 EXPECT_TRUE(IsArcAllowedForProfile(profile()));
127
128 // false for nullptr.
129 EXPECT_FALSE(IsArcAllowedForProfile(nullptr));
130
131 // false for incognito mode profile.
132 EXPECT_FALSE(IsArcAllowedForProfile(profile()->GetOffTheRecordProfile()));
133
134 // false for Legacy supervised user.
135 profile()->SetSupervisedUserId("foo");
136 EXPECT_FALSE(IsArcAllowedForProfile(profile()));
137 }
138
139 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_DisableArc) {
140 base::CommandLine::ForCurrentProcess()->InitFromArgv({""});
141 ScopedLogIn login(GetFakeUserManager(),
142 AccountId::FromUserEmailGaiaId(
143 profile()->GetProfileUserName(), kTestGaiaId));
144 EXPECT_FALSE(IsArcAllowedForProfile(profile()));
145 }
146
147 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_NonPrimaryProfile) {
148 ScopedLogIn login2(
149 GetFakeUserManager(),
150 AccountId::FromUserEmailGaiaId("user2@gmail.com", "0123456789"));
151 ScopedLogIn login(GetFakeUserManager(),
152 AccountId::FromUserEmailGaiaId(
153 profile()->GetProfileUserName(), kTestGaiaId));
154 EXPECT_FALSE(IsArcAllowedForProfile(profile()));
155 }
156
157 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_Kiosk) {
158 ScopedLogIn login(GetFakeUserManager(),
159 AccountId::FromUserEmail(profile()->GetProfileUserName()),
160 user_manager::USER_TYPE_ARC_KIOSK_APP);
161 EXPECT_FALSE(chromeos::ProfileHelper::Get()
162 ->GetUserByProfile(profile())
163 ->HasGaiaAccount());
164 EXPECT_TRUE(IsArcAllowedForProfile(profile()));
165 }
166
167 TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_SupervisedUserFlow) {
168 auto manager_id = AccountId::FromUserEmailGaiaId(
169 profile()->GetProfileUserName(), kTestGaiaId);
170 ScopedLogIn login(GetFakeUserManager(), manager_id);
171 GetFakeUserManager()->SetUserFlow(
172 manager_id, new chromeos::SupervisedUserCreationFlow(manager_id));
173 EXPECT_FALSE(IsArcAllowedForProfile(profile()));
174 }
175
176 // TODO(hidehiko): Add test for Ephemeral users. There seems no way to easily
177 // simulate ephemeral user.
178
179 } // namespace util
180 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_util.cc ('k') | chrome/browser/chromeos/extensions/file_manager/event_router.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698