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

Side by Side Diff: chrome/browser/extensions/api/settings_private/settings_private_apitest.cc

Issue 1163593005: Update chrome.settingsPrivate to support CrOS-only settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix issues preventing compilation on non-CrOS builds. Created 5 years, 6 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
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 "components/keyed_service/core/keyed_service.h" 12 #include "components/keyed_service/core/keyed_service.h"
13 #include "content/public/test/test_utils.h" 13 #include "content/public/test/test_utils.h"
14 #include "extensions/common/switches.h" 14 #include "extensions/common/switches.h"
15 15
16 #if defined(OS_CHROMEOS)
17 #include "chromeos/chromeos_switches.h"
18 #endif
19
16 namespace extensions { 20 namespace extensions {
17 21
18 namespace { 22 namespace {
19 23
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 { 24 class SettingsPrivateApiTest : public ExtensionApiTest {
64 public: 25 public:
65 SettingsPrivateApiTest() {} 26 SettingsPrivateApiTest() {}
66 ~SettingsPrivateApiTest() override {} 27 ~SettingsPrivateApiTest() override {}
67 28
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 { 29 void SetUpCommandLine(base::CommandLine* command_line) override {
75 ExtensionApiTest::SetUpCommandLine(command_line); 30 ExtensionApiTest::SetUpCommandLine(command_line);
76 } 31 #if defined(OS_CHROMEOS)
77 32 command_line->AppendSwitch(chromeos::switches::kStubCrosSettings);
78 void SetUpOnMainThread() override { 33 #endif
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 } 34 }
87 35
88 protected: 36 protected:
89 bool RunSettingsSubtest(const std::string& subtest) { 37 bool RunSettingsSubtest(const std::string& subtest) {
90 return RunExtensionSubtest("settings_private", 38 return RunExtensionSubtest("settings_private",
91 "main.html?" + subtest, 39 "main.html?" + subtest,
92 kFlagLoadAsComponent); 40 kFlagLoadAsComponent);
93 } 41 }
94 42
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: 43 private:
100 DISALLOW_COPY_AND_ASSIGN(SettingsPrivateApiTest); 44 DISALLOW_COPY_AND_ASSIGN(SettingsPrivateApiTest);
101 }; 45 };
102 46
103 // static
104 TestDelegate* SettingsPrivateApiTest::s_test_delegate_ = NULL;
105 47
106 } // namespace 48 } // namespace
107 49
108 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, SetPref) { 50 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, SetPref) {
109 EXPECT_TRUE(RunSettingsSubtest("setPref")) << message_; 51 EXPECT_TRUE(RunSettingsSubtest("setPref")) << message_;
110 } 52 }
111 53
112 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetPref) { 54 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetPref) {
113 EXPECT_TRUE(RunSettingsSubtest("getPref")) << message_; 55 EXPECT_TRUE(RunSettingsSubtest("getPref")) << message_;
114 } 56 }
115 57
116 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetAllPrefs) { 58 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetAllPrefs) {
117 EXPECT_TRUE(RunSettingsSubtest("getAllPrefs")) << message_; 59 EXPECT_TRUE(RunSettingsSubtest("getAllPrefs")) << message_;
118 } 60 }
119 61
120 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, OnPrefsChanged) { 62 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, OnPrefsChanged) {
121 EXPECT_TRUE(RunSettingsSubtest("onPrefsChanged")) << message_; 63 EXPECT_TRUE(RunSettingsSubtest("onPrefsChanged")) << message_;
122 } 64 }
123 65
66 #if defined(OS_CHROMEOS)
67 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, GetPref_CrOSSetting) {
68 EXPECT_TRUE(RunSettingsSubtest("getPref_CrOSSetting")) << message_;
69 }
70
71 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, SetPref_CrOSSetting) {
72 EXPECT_TRUE(RunSettingsSubtest("setPref_CrOSSetting")) << message_;
73 }
74
75 IN_PROC_BROWSER_TEST_F(SettingsPrivateApiTest, OnPrefsChanged_CrOSSetting) {
76 EXPECT_TRUE(RunSettingsSubtest("onPrefsChanged_CrOSSetting")) << message_;
77 }
78 #endif
79
124 } // namespace extensions 80 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698