| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/arc/arc_util.h" | 5 #include "components/arc/arc_util.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "ash/shared/app_types.h" | 10 #include "ash/shared/app_types.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/test/scoped_feature_list.h" | 14 #include "base/test/scoped_feature_list.h" |
| 15 #include "components/signin/core/account_id/account_id.h" |
| 16 #include "components/user_manager/fake_user_manager.h" |
| 17 #include "components/user_manager/user.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "ui/aura/client/aura_constants.h" | 19 #include "ui/aura/client/aura_constants.h" |
| 17 #include "ui/aura/test/test_windows.h" | 20 #include "ui/aura/test/test_windows.h" |
| 18 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
| 19 | 22 |
| 20 namespace arc { | 23 namespace arc { |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 // If an instance is created, based on the value passed to the consturctor, | 26 // If an instance is created, based on the value passed to the consturctor, |
| 24 // EnableARC feature is enabled/disabled in the scope. | 27 // EnableARC feature is enabled/disabled in the scope. |
| 25 class ScopedArcFeature { | 28 class ScopedArcFeature { |
| 26 public: | 29 public: |
| 27 explicit ScopedArcFeature(bool enabled) { | 30 explicit ScopedArcFeature(bool enabled) { |
| 28 constexpr char kArcFeatureName[] = "EnableARC"; | 31 constexpr char kArcFeatureName[] = "EnableARC"; |
| 29 if (enabled) { | 32 if (enabled) { |
| 30 feature_list.InitFromCommandLine(kArcFeatureName, std::string()); | 33 feature_list.InitFromCommandLine(kArcFeatureName, std::string()); |
| 31 } else { | 34 } else { |
| 32 feature_list.InitFromCommandLine(std::string(), kArcFeatureName); | 35 feature_list.InitFromCommandLine(std::string(), kArcFeatureName); |
| 33 } | 36 } |
| 34 } | 37 } |
| 35 ~ScopedArcFeature() = default; | 38 ~ScopedArcFeature() = default; |
| 36 | 39 |
| 37 private: | 40 private: |
| 38 base::test::ScopedFeatureList feature_list; | 41 base::test::ScopedFeatureList feature_list; |
| 39 DISALLOW_COPY_AND_ASSIGN(ScopedArcFeature); | 42 DISALLOW_COPY_AND_ASSIGN(ScopedArcFeature); |
| 40 }; | 43 }; |
| 41 | 44 |
| 45 // Helper to enable user_manager::FakeUserManager while it is in scope. |
| 46 // TODO(xiyuan): Remove after ScopedUserManagerEnabler is moved to user_manager. |
| 47 class ScopedUserManager { |
| 48 public: |
| 49 explicit ScopedUserManager(user_manager::UserManager* user_manager) |
| 50 : user_manager_(user_manager) { |
| 51 DCHECK(!user_manager::UserManager::IsInitialized()); |
| 52 user_manager->Initialize(); |
| 53 } |
| 54 ~ScopedUserManager() { |
| 55 DCHECK_EQ(user_manager::UserManager::Get(), user_manager_); |
| 56 user_manager_->Shutdown(); |
| 57 user_manager_->Destroy(); |
| 58 } |
| 59 |
| 60 private: |
| 61 user_manager::UserManager* const user_manager_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(ScopedUserManager); |
| 64 }; |
| 65 |
| 66 // Fake user that can be created with a specified type. |
| 67 class FakeUser : public user_manager::User { |
| 68 public: |
| 69 explicit FakeUser(user_manager::UserType user_type) |
| 70 : User(AccountId::FromUserEmail("user@test.com")), |
| 71 user_type_(user_type) {} |
| 72 ~FakeUser() override = default; |
| 73 |
| 74 // user_manager::User: |
| 75 user_manager::UserType GetType() const override { return user_type_; } |
| 76 |
| 77 private: |
| 78 const user_manager::UserType user_type_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(FakeUser); |
| 81 }; |
| 82 |
| 42 using ArcUtilTest = testing::Test; | 83 using ArcUtilTest = testing::Test; |
| 43 | 84 |
| 44 TEST_F(ArcUtilTest, IsArcAvailable_None) { | 85 TEST_F(ArcUtilTest, IsArcAvailable_None) { |
| 45 auto* command_line = base::CommandLine::ForCurrentProcess(); | 86 auto* command_line = base::CommandLine::ForCurrentProcess(); |
| 46 | 87 |
| 47 command_line->InitFromArgv({"", "--arc-availability=none"}); | 88 command_line->InitFromArgv({"", "--arc-availability=none"}); |
| 48 EXPECT_FALSE(IsArcAvailable()); | 89 EXPECT_FALSE(IsArcAvailable()); |
| 49 | 90 |
| 50 // If --arc-availability flag is set to "none", even if Finch experiment is | 91 // If --arc-availability flag is set to "none", even if Finch experiment is |
| 51 // turned on, ARC cannot be used. | 92 // turned on, ARC cannot be used. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 window->SetProperty(aura::client::kAppType, | 195 window->SetProperty(aura::client::kAppType, |
| 155 static_cast<int>(ash::AppType::CHROME_APP)); | 196 static_cast<int>(ash::AppType::CHROME_APP)); |
| 156 EXPECT_FALSE(IsArcAppWindow(window.get())); | 197 EXPECT_FALSE(IsArcAppWindow(window.get())); |
| 157 window->SetProperty(aura::client::kAppType, | 198 window->SetProperty(aura::client::kAppType, |
| 158 static_cast<int>(ash::AppType::ARC_APP)); | 199 static_cast<int>(ash::AppType::ARC_APP)); |
| 159 EXPECT_TRUE(IsArcAppWindow(window.get())); | 200 EXPECT_TRUE(IsArcAppWindow(window.get())); |
| 160 | 201 |
| 161 EXPECT_FALSE(IsArcAppWindow(nullptr)); | 202 EXPECT_FALSE(IsArcAppWindow(nullptr)); |
| 162 } | 203 } |
| 163 | 204 |
| 205 TEST_F(ArcUtilTest, IsArcAllowedForUser) { |
| 206 user_manager::FakeUserManager fake_user_manager; |
| 207 ScopedUserManager scoped_user_manager(&fake_user_manager); |
| 208 |
| 209 struct { |
| 210 user_manager::UserType user_type; |
| 211 bool expected_allowed; |
| 212 } const kTestCases[] = { |
| 213 {user_manager::USER_TYPE_REGULAR, true}, |
| 214 {user_manager::USER_TYPE_GUEST, false}, |
| 215 {user_manager::USER_TYPE_PUBLIC_ACCOUNT, false}, |
| 216 {user_manager::USER_TYPE_SUPERVISED, false}, |
| 217 {user_manager::USER_TYPE_KIOSK_APP, false}, |
| 218 {user_manager::USER_TYPE_CHILD, true}, |
| 219 {user_manager::USER_TYPE_ARC_KIOSK_APP, true}, |
| 220 {user_manager::USER_TYPE_ACTIVE_DIRECTORY, true}, |
| 221 }; |
| 222 for (const auto& test_case : kTestCases) { |
| 223 const FakeUser user(test_case.user_type); |
| 224 EXPECT_EQ(test_case.expected_allowed, IsArcAllowedForUser(&user)) |
| 225 << "User type=" << test_case.user_type; |
| 226 } |
| 227 |
| 228 // An ephemeral user is a logged in user but unknown to UserManager when |
| 229 // ephemeral policy is set. |
| 230 fake_user_manager.SetEphemeralUsersEnabled(true); |
| 231 fake_user_manager.UserLoggedIn(AccountId::FromUserEmail("test@test.com"), |
| 232 "test@test.com-hash", false); |
| 233 const user_manager::User* ephemeral_user = fake_user_manager.GetActiveUser(); |
| 234 ASSERT_TRUE(ephemeral_user); |
| 235 ASSERT_TRUE(fake_user_manager.IsUserCryptohomeDataEphemeral( |
| 236 ephemeral_user->GetAccountId())); |
| 237 |
| 238 // Ephemeral user is not allowed for ARC. |
| 239 EXPECT_FALSE(IsArcAllowedForUser(ephemeral_user)); |
| 240 } |
| 241 |
| 164 } // namespace | 242 } // namespace |
| 165 } // namespace arc | 243 } // namespace arc |
| OLD | NEW |