1 // Copyright 2015 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.
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/chromeos/extensions/users_private/users_private_delegate.h"
11 #include "chrome/browser/chromeos/extensions/users_private/users_private_delegate_factory.h"
12 #include "chrome/browser/extensions/api/settings_private/prefs_util.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/common/extensions/api/users_private.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "content/public/test/test_utils.h"
17 #include "extensions/common/switches.h"
19 #if defined(OS_CHROMEOS)
20 #include "chromeos/chromeos_switches.h"
23 namespace extensions
{
27 class TestPrefsUtil
: public PrefsUtil
{
29 explicit TestPrefsUtil(Profile
* profile
) : PrefsUtil(profile
) {}
31 scoped_ptr
<api::settings_private::PrefObject
> GetPref(
32 const std::string
& name
) override
{
33 if (name
!= "cros.accounts.users")
34 return PrefsUtil::GetPref(name
);
36 scoped_ptr
<api::settings_private::PrefObject
> pref_object(
37 new api::settings_private::PrefObject());
38 pref_object
->key
= name
;
39 pref_object
->type
= api::settings_private::PrefType::PREF_TYPE_LIST
;
41 base::ListValue
* value
= new base::ListValue();
42 for (auto& email
: whitelisted_users_
) {
43 value
->AppendString(email
);
45 pref_object
->value
.reset(value
);
47 return pref_object
.Pass();
50 bool AppendToListCrosSetting(const std::string
& pref_name
,
51 const base::Value
& value
) override
{
53 value
.GetAsString(&email
);
55 for (auto& user
: whitelisted_users_
) {
60 whitelisted_users_
.push_back(email
);
64 bool RemoveFromListCrosSetting(const std::string
& pref_name
,
65 const base::Value
& value
) override
{
67 value
.GetAsString(&email
);
70 std::find(whitelisted_users_
.begin(), whitelisted_users_
.end(), email
);
71 if (iter
!= whitelisted_users_
.end())
72 whitelisted_users_
.erase(iter
);
78 std::vector
<std::string
> whitelisted_users_
;
81 class TestDelegate
: public UsersPrivateDelegate
{
83 explicit TestDelegate(Profile
* profile
) : UsersPrivateDelegate(profile
) {
85 prefs_util_
= nullptr;
88 PrefsUtil
* GetPrefsUtil() override
{
90 prefs_util_
.reset(new TestPrefsUtil(profile_
));
92 return prefs_util_
.get();
95 ~TestDelegate() override
{}
98 Profile
* profile_
; // weak
99 scoped_ptr
<TestPrefsUtil
> prefs_util_
;
101 DISALLOW_COPY_AND_ASSIGN(TestDelegate
);
104 class UsersPrivateApiTest
: public ExtensionApiTest
{
106 UsersPrivateApiTest() {}
107 ~UsersPrivateApiTest() override
{}
109 static scoped_ptr
<KeyedService
> GetUsersPrivateDelegate(
110 content::BrowserContext
* profile
) {
111 CHECK(s_test_delegate_
);
112 return make_scoped_ptr(s_test_delegate_
);
115 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
116 ExtensionApiTest::SetUpCommandLine(command_line
);
117 #if defined(OS_CHROMEOS)
118 command_line
->AppendSwitch(chromeos::switches::kStubCrosSettings
);
122 void SetUpOnMainThread() override
{
123 ExtensionApiTest::SetUpOnMainThread();
124 if (!s_test_delegate_
)
125 s_test_delegate_
= new TestDelegate(profile());
127 UsersPrivateDelegateFactory::GetInstance()->SetTestingFactory(
128 profile(), &UsersPrivateApiTest::GetUsersPrivateDelegate
);
129 content::RunAllPendingInMessageLoop();
133 bool RunSubtest(const std::string
& subtest
) {
134 return RunExtensionSubtest("users_private", "main.html?" + subtest
,
135 kFlagLoadAsComponent
);
138 // Static pointer to the TestDelegate so that it can be accessed in
139 // GetUsersPrivateDelegate() passed to SetTestingFactory().
140 static TestDelegate
* s_test_delegate_
;
143 DISALLOW_COPY_AND_ASSIGN(UsersPrivateApiTest
);
147 TestDelegate
* UsersPrivateApiTest::s_test_delegate_
= NULL
;
151 #if defined(OS_CHROMEOS)
152 IN_PROC_BROWSER_TEST_F(UsersPrivateApiTest
, AddUser
) {
153 EXPECT_TRUE(RunSubtest("addUser")) << message_
;
156 IN_PROC_BROWSER_TEST_F(UsersPrivateApiTest
, AddAndRemoveUsers
) {
157 EXPECT_TRUE(RunSubtest("addAndRemoveUsers")) << message_
;
160 IN_PROC_BROWSER_TEST_F(UsersPrivateApiTest
, IsOwner
) {
161 EXPECT_TRUE(RunSubtest("isOwner")) << message_
;
165 } // namespace extensions