OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/api/settings_private/settings_private_delega
te.h" | 8 #include "chrome/browser/extensions/api/settings_private/settings_private_delega
te.h" |
9 #include "chrome/browser/extensions/api/settings_private/settings_private_delega
te_factory.h" | 9 #include "chrome/browser/extensions/api/settings_private/settings_private_delega
te_factory.h" |
10 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
11 #include "chrome/common/extensions/api/settings_private.h" | 11 #include "chrome/common/extensions/api/settings_private.h" |
| 12 #include "chromeos/chromeos_switches.h" |
12 #include "components/keyed_service/core/keyed_service.h" | 13 #include "components/keyed_service/core/keyed_service.h" |
13 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
14 #include "extensions/common/switches.h" | 15 #include "extensions/common/switches.h" |
15 | 16 |
16 namespace extensions { | 17 namespace extensions { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 const char kTestPrefName[] = "download.default_directory"; | |
21 const char kTestPrefValue[] = "/Downloads"; | |
22 | |
23 class TestDelegate : public SettingsPrivateDelegate { | |
24 public: | |
25 explicit TestDelegate(Profile* profile) : SettingsPrivateDelegate(profile) {} | |
26 | |
27 bool SetPref(const std::string& name, const base::Value* value) override { | |
28 // Write to the actual pref service, so that the SettingsPrivateEventRouter | |
29 // dispatches an onPrefsChanged event. | |
30 PrefService* pref_service = profile_->GetPrefs(); | |
31 pref_service->Set(name.c_str(), *value); | |
32 return true; | |
33 } | |
34 | |
35 scoped_ptr<base::Value> GetPref(const std::string& name) override { | |
36 if (name.compare(kTestPrefName) != 0) | |
37 return base::Value::CreateNullValue(); | |
38 | |
39 return CreateTestPrefObject()->ToValue(); | |
40 } | |
41 | |
42 scoped_ptr<base::Value> GetAllPrefs() override { | |
43 base::ListValue* list_value = new base::ListValue(); | |
44 list_value->Append(CreateTestPrefObject()->ToValue().release()); | |
45 return make_scoped_ptr(list_value); | |
46 } | |
47 | |
48 ~TestDelegate() override {} | |
49 | |
50 private: | |
51 scoped_ptr<api::settings_private::PrefObject> CreateTestPrefObject() { | |
52 scoped_ptr<api::settings_private::PrefObject> pref_object( | |
53 new api::settings_private::PrefObject()); | |
54 pref_object->key = std::string(kTestPrefName); | |
55 pref_object->type = api::settings_private::PrefType::PREF_TYPE_STRING; | |
56 pref_object->value.reset(new base::StringValue(kTestPrefValue)); | |
57 return pref_object.Pass(); | |
58 } | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
61 }; | |
62 | |
63 class SettingsPrivateApiTest : public ExtensionApiTest { | 21 class SettingsPrivateApiTest : public ExtensionApiTest { |
64 public: | 22 public: |
65 SettingsPrivateApiTest() {} | 23 SettingsPrivateApiTest() {} |
66 ~SettingsPrivateApiTest() override {} | 24 ~SettingsPrivateApiTest() override {} |
67 | 25 |
68 static KeyedService* GetSettingsPrivateDelegate( | |
69 content::BrowserContext* profile) { | |
70 CHECK(s_test_delegate_); | |
71 return s_test_delegate_; | |
72 } | |
73 | |
74 void SetUpCommandLine(base::CommandLine* command_line) override { | 26 void SetUpCommandLine(base::CommandLine* command_line) override { |
75 ExtensionApiTest::SetUpCommandLine(command_line); | 27 ExtensionApiTest::SetUpCommandLine(command_line); |
76 } | 28 command_line->AppendSwitch(chromeos::switches::kStubCrosSettings); |
77 | |
78 void SetUpOnMainThread() override { | |
79 ExtensionApiTest::SetUpOnMainThread(); | |
80 if (!s_test_delegate_) | |
81 s_test_delegate_ = new TestDelegate(profile()); | |
82 | |
83 SettingsPrivateDelegateFactory::GetInstance()->SetTestingFactory( | |
84 profile(), &SettingsPrivateApiTest::GetSettingsPrivateDelegate); | |
85 content::RunAllPendingInMessageLoop(); | |
86 } | 29 } |
87 | 30 |
88 protected: | 31 protected: |
89 bool RunSettingsSubtest(const std::string& subtest) { | 32 bool RunSettingsSubtest(const std::string& subtest) { |
90 return RunExtensionSubtest("settings_private", | 33 return RunExtensionSubtest("settings_private", |
91 "main.html?" + subtest, | 34 "main.html?" + subtest, |
92 kFlagLoadAsComponent); | 35 kFlagLoadAsComponent); |
93 } | 36 } |
94 | 37 |
95 // Static pointer to the TestDelegate so that it can be accessed in | |
96 // GetSettingsPrivateDelegate() passed to SetTestingFactory(). | |
97 static TestDelegate* s_test_delegate_; | |
98 | |
99 private: | 38 private: |
100 DISALLOW_COPY_AND_ASSIGN(SettingsPrivateApiTest); | 39 DISALLOW_COPY_AND_ASSIGN(SettingsPrivateApiTest); |
101 }; | 40 }; |
102 | 41 |
103 // static | |
104 TestDelegate* SettingsPrivateApiTest::s_test_delegate_ = NULL; | |
105 | 42 |
106 } // namespace | 43 } // namespace |
107 | 44 |
108 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, SetPref) { | 45 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, SetPref) { |
109 EXPECT_TRUE(RunSettingsSubtest("setPref")) << message_; | 46 EXPECT_TRUE(RunSettingsSubtest("setPref")) << message_; |
110 } | 47 } |
111 | 48 |
| 49 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, SetPref_CrOSSetting) { |
| 50 EXPECT_TRUE(RunSettingsSubtest("setPref_CrOSSetting")) << message_; |
| 51 } |
| 52 |
112 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetPref) { | 53 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetPref) { |
113 EXPECT_TRUE(RunSettingsSubtest("getPref")) << message_; | 54 EXPECT_TRUE(RunSettingsSubtest("getPref")) << message_; |
114 } | 55 } |
115 | 56 |
| 57 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetPref_CrOSSetting) { |
| 58 EXPECT_TRUE(RunSettingsSubtest("getPref_CrOSSetting")) << message_; |
| 59 } |
| 60 |
116 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetAllPrefs) { | 61 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetAllPrefs) { |
117 EXPECT_TRUE(RunSettingsSubtest("getAllPrefs")) << message_; | 62 EXPECT_TRUE(RunSettingsSubtest("getAllPrefs")) << message_; |
118 } | 63 } |
119 | 64 |
120 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, OnPrefsChanged) { | 65 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, OnPrefsChanged) { |
121 EXPECT_TRUE(RunSettingsSubtest("onPrefsChanged")) << message_; | 66 EXPECT_TRUE(RunSettingsSubtest("onPrefsChanged")) << message_; |
122 } | 67 } |
123 | 68 |
| 69 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, OnPrefsChanged_CrOSSetting) { |
| 70 EXPECT_TRUE(RunSettingsSubtest("onPrefsChanged_CrOSSetting")) << message_; |
| 71 } |
| 72 |
124 } // namespace extensions | 73 } // namespace extensions |
OLD | NEW |