Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_util_unittest.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_util_unittest.cc b/chrome/browser/chromeos/arc/arc_util_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7f77a92415f93e7455d23e974b27cc38f061ebcd |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/arc_util_unittest.cc |
| @@ -0,0 +1,180 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/arc/arc_util.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/command_line.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/test/scoped_command_line.h" |
| +#include "chrome/browser/chromeos/login/supervised/supervised_user_creation_flow.h" |
| +#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| +#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| +#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
| +#include "chrome/browser/chromeos/profiles/profile_helper.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "components/signin/core/account_id/account_id.h" |
| +#include "components/user_manager/user_manager.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace arc { |
| +namespace util { |
| + |
| +namespace { |
| + |
| +constexpr char kTestProfileName[] = "user@gmail.com"; |
| +constexpr char kTestGaiaId[] = "1234567890"; |
| + |
| +class ScopedLogIn { |
| + public: |
| + ScopedLogIn( |
| + chromeos::FakeChromeUserManager* fake_user_manager, |
| + const AccountId& account_id, |
| + user_manager::UserType user_type = user_manager::USER_TYPE_REGULAR) |
| + : fake_user_manager_(fake_user_manager), account_id_(account_id) { |
| + switch (user_type) { |
| + case user_manager::USER_TYPE_REGULAR: |
| + LogIn(); |
| + break; |
| + case user_manager::USER_TYPE_ARC_KIOSK_APP: |
| + LogInArcKioskApp(); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + } |
| + |
| + ~ScopedLogIn() { LogOut(); } |
| + |
| + private: |
| + void LogIn() { |
| + fake_user_manager_->AddUser(account_id_); |
| + fake_user_manager_->LoginUser(account_id_); |
| + } |
| + |
| + void LogInArcKioskApp() { |
| + fake_user_manager_->AddArcKioskAppUser(account_id_); |
| + fake_user_manager_->LoginUser(account_id_); |
| + } |
| + |
| + void LogOut() { fake_user_manager_->RemoveUserFromList(account_id_); } |
| + |
| + chromeos::FakeChromeUserManager* fake_user_manager_; |
| + const AccountId account_id_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ScopedLogIn); |
| +}; |
| + |
| +} // namespace |
| + |
| +class ChromeArcUtilTest : public testing::Test { |
| + public: |
| + ChromeArcUtilTest() = default; |
| + ~ChromeArcUtilTest() override = default; |
| + |
| + void SetUp() override { |
| + command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); |
| + command_line_->GetProcessCommandLine()->InitFromArgv({"", "--enable-arc"}); |
| + |
| + user_manager_enabler_ = |
| + base::MakeUnique<chromeos::ScopedUserManagerEnabler>( |
| + new chromeos::FakeChromeUserManager()); |
| + chromeos::WallpaperManager::Initialize(); |
| + profile_ = base::MakeUnique<TestingProfile>(); |
| + profile_->set_profile_name(kTestProfileName); |
| + } |
| + |
| + void TearDown() override { |
| + profile_.reset(); |
| + chromeos::WallpaperManager::Shutdown(); |
| + user_manager_enabler_.reset(); |
| + command_line_.reset(); |
| + } |
| + |
| + TestingProfile* profile() { return profile_.get(); } |
| + |
| + chromeos::FakeChromeUserManager* GetFakeUserManager() const { |
| + return static_cast<chromeos::FakeChromeUserManager*>( |
| + user_manager::UserManager::Get()); |
| + } |
| + |
| + void LogIn() { |
| + const auto account_id = AccountId::FromUserEmailGaiaId( |
| + profile()->GetProfileUserName(), kTestGaiaId); |
| + GetFakeUserManager()->AddUser(account_id); |
| + GetFakeUserManager()->LoginUser(account_id); |
| + } |
| + |
| + private: |
| + std::unique_ptr<base::test::ScopedCommandLine> command_line_; |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| + std::unique_ptr<TestingProfile> profile_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ChromeArcUtilTest); |
| +}; |
| + |
| +TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile) { |
| + ScopedLogIn login(GetFakeUserManager(), |
| + AccountId::FromUserEmailGaiaId( |
| + profile()->GetProfileUserName(), kTestGaiaId)); |
| + EXPECT_TRUE(IsArcAllowedForProfile(profile())); |
| + |
| + // false for nullptr. |
| + EXPECT_FALSE(IsArcAllowedForProfile(nullptr)); |
| + |
| + // false for incognito mode profile. |
| + EXPECT_FALSE(IsArcAllowedForProfile(profile()->GetOffTheRecordProfile())); |
| + |
| + // false for Legacy sueprvised user. |
|
Luis Héctor Chávez
2017/01/19 18:09:15
nit: s/sueprvised/supervised/
hidehiko
2017/01/24 17:46:12
Done.
|
| + profile()->SetSupervisedUserId("foo"); |
| + EXPECT_FALSE(IsArcAllowedForProfile(profile())); |
| +} |
| + |
| +TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_DisableArc) { |
| + base::CommandLine::ForCurrentProcess()->InitFromArgv({""}); |
| + ScopedLogIn login(GetFakeUserManager(), |
| + AccountId::FromUserEmailGaiaId( |
| + profile()->GetProfileUserName(), kTestGaiaId)); |
| + EXPECT_FALSE(IsArcAllowedForProfile(profile())); |
| +} |
| + |
| +TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_NonPrimaryProfile) { |
| + ScopedLogIn login2( |
| + GetFakeUserManager(), |
| + AccountId::FromUserEmailGaiaId("user2@gmail.com", "0123456789")); |
| + ScopedLogIn login(GetFakeUserManager(), |
| + AccountId::FromUserEmailGaiaId( |
| + profile()->GetProfileUserName(), kTestGaiaId)); |
| + EXPECT_FALSE(IsArcAllowedForProfile(profile())); |
| +} |
| + |
| +TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_Kiosk) { |
| + ScopedLogIn login(GetFakeUserManager(), |
| + AccountId::FromUserEmail(profile()->GetProfileUserName()), |
| + user_manager::USER_TYPE_ARC_KIOSK_APP); |
| + EXPECT_FALSE(chromeos::ProfileHelper::Get() |
| + ->GetUserByProfile(profile()) |
| + ->HasGaiaAccount()); |
| + EXPECT_TRUE(IsArcAllowedForProfile(profile())); |
| +} |
| + |
| +TEST_F(ChromeArcUtilTest, IsArcAllowedForProfile_SupervisedUserFlow) { |
| + auto manager_id = AccountId::FromUserEmailGaiaId( |
| + profile()->GetProfileUserName(), kTestGaiaId); |
| + ScopedLogIn login(GetFakeUserManager(), manager_id); |
| + GetFakeUserManager()->SetUserFlow( |
| + manager_id, new chromeos::SupervisedUserCreationFlow(manager_id)); |
| + EXPECT_FALSE(IsArcAllowedForProfile(profile())); |
| +} |
| + |
| +// TODO(hidehiko): Add test for Ephemral users. There seems no way to easily |
|
Luis Héctor Chávez
2017/01/19 18:09:15
nit: s/Ephemral/Ephemeral/
hidehiko
2017/01/24 17:46:12
Done.
|
| +// simulate ephemeral user. |
|
xiyuan
2017/01/19 19:12:46
If we make FakeUserManager::AreEphemeralUsersEnabl
khmel
2017/01/19 19:18:54
We already have this configurable:
https://cs.chro
xiyuan
2017/01/19 19:21:51
So we need to implement that in FakeUserManager un
hidehiko
2017/01/24 17:46:12
xiyuan@, khmel@, thanks for the info.
I'll give it
|
| + |
| +} // namespace util |
| +} // namespace arc |