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

Side by Side Diff: chrome/browser/extensions/extension_proxy_apitest.cc

Issue 6240013: Make proxy settings one atomic dictionary in the PrefStores such that modifications are atomic. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/out/Debug
Patch Set: Created 9 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/browser/extensions/extension_apitest.h" 5 #include "chrome/browser/extensions/extension_apitest.h"
6 #include "chrome/browser/prefs/pref_service.h" 6 #include "chrome/browser/prefs/pref_service.h"
7 #include "chrome/browser/prefs/proxy_prefs.h" 7 #include "chrome/browser/prefs/proxy_prefs.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/chrome_switches.h" 10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/extensions/extension.h" 11 #include "chrome/common/extensions/extension.h"
12 #include "chrome/common/pref_names.h" 12 #include "chrome/common/pref_names.h"
13 13
14 class ProxySettingsApiTest : public ExtensionApiTest {
15 protected:
16 void AssertSettings(int expected_mode,
17 const char* expected_server,
18 const char* expected_pac_url,
19 PrefService* pref_service) {
20 AssertExtensionControlled(prefs::kProxyMode, pref_service);
21 int mode = pref_service->GetInteger(prefs::kProxyMode);
22 EXPECT_EQ(expected_mode, mode);
23
24 AssertExtensionControlled(prefs::kProxyPacUrl, pref_service);
25 EXPECT_EQ(expected_pac_url, pref_service->GetString(prefs::kProxyPacUrl));
26
27 AssertExtensionControlled(prefs::kProxyServer, pref_service);
28 EXPECT_EQ(expected_server, pref_service->GetString(prefs::kProxyServer));
29 }
30
31 void AssertNoSettings(PrefService* pref_service) {
32 AssertNotExtensionControlled(prefs::kProxyServer, pref_service);
33 AssertNotExtensionControlled(prefs::kProxyMode, pref_service);
34 AssertNotExtensionControlled(prefs::kProxyPacUrl, pref_service);
35 }
36 private:
37 void AssertExtensionControlled(const char* pref_key,
38 PrefService* pref_service) {
39 const PrefService::Preference* pref =
40 pref_service->FindPreference(pref_key);
41 ASSERT_TRUE(pref != NULL);
42 ASSERT_TRUE(pref->IsExtensionControlled());
43 }
44
45 void AssertNotExtensionControlled(const char* pref_key,
46 PrefService* pref_service) {
47 const PrefService::Preference* pref =
48 pref_service->FindPreference(pref_key);
49 ASSERT_TRUE(pref != NULL);
50 ASSERT_FALSE(pref->IsExtensionControlled());
51 }
52 };
53
54 namespace { 14 namespace {
55 15
56 const char NO_SERVER[] = ""; 16 const char NO_SERVER[] = "";
57 const char NO_PAC[] = ""; 17 const char NO_PAC[] = "";
58 18
59 } // namespace 19 } // namespace
60 20
21 class ProxySettingsApiTest : public ExtensionApiTest {
22 protected:
23 void ExpectSettings(int expected_mode,
24 const std::string& expected_server,
25 const std::string& expected_pac_url,
26 PrefService* pref_service) {
27 const PrefService::Preference* pref =
28 pref_service->FindPreference(prefs::kProxy);
29 ASSERT_TRUE(pref != NULL);
30 ASSERT_TRUE(pref->IsExtensionControlled());
31
32 ProxyPrefsDictionary dict(pref_service->GetDictionary(prefs::kProxy));
33
34 ProxyPrefs::ProxyMode mode;
35 ASSERT_TRUE(dict.GetMode(&mode));
36 EXPECT_EQ(expected_mode, mode);
37
38 std::string value;
39 if (!expected_pac_url.empty()) {
40 ASSERT_TRUE(dict.GetPacUrl(&value));
41 EXPECT_EQ(expected_pac_url, value);
42 } else {
43 EXPECT_FALSE(dict.GetPacUrl(&value));
44 }
45
46 if (!expected_server.empty()) {
47 ASSERT_TRUE(dict.GetProxyServer(&value));
48 EXPECT_EQ(expected_server, value);
49 } else {
50 EXPECT_FALSE(dict.GetProxyServer(&value));
51 }
52 }
53
54 void ExpectNoSettings(PrefService* pref_service) {
55 const PrefService::Preference* pref =
56 pref_service->FindPreference(prefs::kProxy);
57 ASSERT_TRUE(pref != NULL);
58 EXPECT_FALSE(pref->IsExtensionControlled());
59 }
60 };
61
61 // Tests direct connection settings. 62 // Tests direct connection settings.
62 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyDirectSettings) { 63 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyDirectSettings) {
63 CommandLine::ForCurrentProcess()->AppendSwitch( 64 CommandLine::ForCurrentProcess()->AppendSwitch(
64 switches::kEnableExperimentalExtensionApis); 65 switches::kEnableExperimentalExtensionApis);
65 66
66 ASSERT_TRUE(RunExtensionTest("proxy/direct")) << message_; 67 ASSERT_TRUE(RunExtensionTest("proxy/direct")) << message_;
67 const Extension* extension = GetSingleLoadedExtension(); 68 const Extension* extension = GetSingleLoadedExtension();
68 ASSERT_TRUE(extension); 69 ASSERT_TRUE(extension);
69 70
70 PrefService* pref_service = browser()->profile()->GetPrefs(); 71 PrefService* pref_service = browser()->profile()->GetPrefs();
71 AssertSettings(ProxyPrefs::MODE_DIRECT, NO_SERVER, NO_PAC, pref_service); 72 ExpectSettings(ProxyPrefs::MODE_DIRECT, NO_SERVER, NO_PAC, pref_service);
72 } 73 }
73 74
74 // Tests auto-detect settings. 75 // Tests auto-detect settings.
75 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyAutoSettings) { 76 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyAutoSettings) {
76 CommandLine::ForCurrentProcess()->AppendSwitch( 77 CommandLine::ForCurrentProcess()->AppendSwitch(
77 switches::kEnableExperimentalExtensionApis); 78 switches::kEnableExperimentalExtensionApis);
78 79
79 ASSERT_TRUE(RunExtensionTest("proxy/auto")) << message_; 80 ASSERT_TRUE(RunExtensionTest("proxy/auto")) << message_;
80 const Extension* extension = GetSingleLoadedExtension(); 81 const Extension* extension = GetSingleLoadedExtension();
81 ASSERT_TRUE(extension); 82 ASSERT_TRUE(extension);
82 83
83 PrefService* pref_service = browser()->profile()->GetPrefs(); 84 PrefService* pref_service = browser()->profile()->GetPrefs();
84 AssertSettings(ProxyPrefs::MODE_AUTO_DETECT, NO_SERVER, NO_PAC, pref_service); 85 ExpectSettings(ProxyPrefs::MODE_AUTO_DETECT, NO_SERVER, NO_PAC, pref_service);
85 } 86 }
86 87
87 // Tests PAC proxy settings. 88 // Tests PAC proxy settings.
88 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyPacScript) { 89 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyPacScript) {
89 CommandLine::ForCurrentProcess()->AppendSwitch( 90 CommandLine::ForCurrentProcess()->AppendSwitch(
90 switches::kEnableExperimentalExtensionApis); 91 switches::kEnableExperimentalExtensionApis);
91 92
92 ASSERT_TRUE(RunExtensionTest("proxy/pac")) << message_; 93 ASSERT_TRUE(RunExtensionTest("proxy/pac")) << message_;
93 const Extension* extension = GetSingleLoadedExtension(); 94 const Extension* extension = GetSingleLoadedExtension();
94 ASSERT_TRUE(extension); 95 ASSERT_TRUE(extension);
95 96
96 PrefService* pref_service = browser()->profile()->GetPrefs(); 97 PrefService* pref_service = browser()->profile()->GetPrefs();
97 AssertSettings(ProxyPrefs::MODE_PAC_SCRIPT, NO_SERVER, 98 ExpectSettings(ProxyPrefs::MODE_PAC_SCRIPT, NO_SERVER,
98 "http://wpad/windows.pac", pref_service); 99 "http://wpad/windows.pac", pref_service);
99 } 100 }
100 101
101 // Tests setting a single proxy to cover all schemes. 102 // Tests setting a single proxy to cover all schemes.
102 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyFixedSingle) { 103 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyFixedSingle) {
103 CommandLine::ForCurrentProcess()->AppendSwitch( 104 CommandLine::ForCurrentProcess()->AppendSwitch(
104 switches::kEnableExperimentalExtensionApis); 105 switches::kEnableExperimentalExtensionApis);
105 106
106 ASSERT_TRUE(RunExtensionTest("proxy/single")) << message_; 107 ASSERT_TRUE(RunExtensionTest("proxy/single")) << message_;
107 const Extension* extension = GetSingleLoadedExtension(); 108 const Extension* extension = GetSingleLoadedExtension();
108 ASSERT_TRUE(extension); 109 ASSERT_TRUE(extension);
109 110
110 PrefService* pref_service = browser()->profile()->GetPrefs(); 111 PrefService* pref_service = browser()->profile()->GetPrefs();
111 AssertSettings(ProxyPrefs::MODE_FIXED_SERVERS, 112 ExpectSettings(ProxyPrefs::MODE_FIXED_SERVERS,
112 "http=http://127.0.0.1:100;" 113 "http=http://127.0.0.1:100;"
113 "https=http://127.0.0.1:100;" 114 "https=http://127.0.0.1:100;"
114 "ftp=http://127.0.0.1:100;" 115 "ftp=http://127.0.0.1:100;"
115 "socks=http://9.9.9.9", 116 "socks=http://9.9.9.9",
116 NO_PAC, 117 NO_PAC,
117 pref_service); 118 pref_service);
118 } 119 }
119 120
120 // Tests setting to use the system's proxy settings. 121 // Tests setting to use the system's proxy settings.
121 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxySystem) { 122 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxySystem) {
122 CommandLine::ForCurrentProcess()->AppendSwitch( 123 CommandLine::ForCurrentProcess()->AppendSwitch(
123 switches::kEnableExperimentalExtensionApis); 124 switches::kEnableExperimentalExtensionApis);
124 125
125 ASSERT_TRUE(RunExtensionTest("proxy/system")) << message_; 126 ASSERT_TRUE(RunExtensionTest("proxy/system")) << message_;
126 const Extension* extension = GetSingleLoadedExtension(); 127 const Extension* extension = GetSingleLoadedExtension();
127 ASSERT_TRUE(extension); 128 ASSERT_TRUE(extension);
128 129
129 PrefService* pref_service = browser()->profile()->GetPrefs(); 130 PrefService* pref_service = browser()->profile()->GetPrefs();
130 AssertSettings(ProxyPrefs::MODE_SYSTEM, NO_SERVER, NO_PAC, pref_service); 131 ExpectSettings(ProxyPrefs::MODE_SYSTEM, NO_SERVER, NO_PAC, pref_service);
131 } 132 }
132 133
133 // Tests setting separate proxies for each scheme. 134 // Tests setting separate proxies for each scheme.
134 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyFixedIndividual) { 135 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyFixedIndividual) {
135 CommandLine::ForCurrentProcess()->AppendSwitch( 136 CommandLine::ForCurrentProcess()->AppendSwitch(
136 switches::kEnableExperimentalExtensionApis); 137 switches::kEnableExperimentalExtensionApis);
137 138
138 ASSERT_TRUE(RunExtensionTest("proxy/individual")) << message_; 139 ASSERT_TRUE(RunExtensionTest("proxy/individual")) << message_;
139 const Extension* extension = GetSingleLoadedExtension(); 140 const Extension* extension = GetSingleLoadedExtension();
140 ASSERT_TRUE(extension); 141 ASSERT_TRUE(extension);
141 142
142 PrefService* pref_service = browser()->profile()->GetPrefs(); 143 PrefService* pref_service = browser()->profile()->GetPrefs();
143 AssertSettings(ProxyPrefs::MODE_FIXED_SERVERS, 144 ExpectSettings(ProxyPrefs::MODE_FIXED_SERVERS,
144 "http=http://1.1.1.1;" 145 "http=http://1.1.1.1;"
145 "https=socks://2.2.2.2;" 146 "https=socks://2.2.2.2;"
146 "ftp=http://3.3.3.3:9000;" 147 "ftp=http://3.3.3.3:9000;"
147 "socks=socks4://4.4.4.4:9090", 148 "socks=socks4://4.4.4.4:9090",
148 NO_PAC, 149 NO_PAC,
149 pref_service); 150 pref_service);
150 151
151 // Now check the incognito preferences. 152 // Now check the incognito preferences.
152 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs(); 153 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
153 AssertSettings(ProxyPrefs::MODE_FIXED_SERVERS, 154 ExpectSettings(ProxyPrefs::MODE_FIXED_SERVERS,
154 "http=http://1.1.1.1;" 155 "http=http://1.1.1.1;"
155 "https=socks://2.2.2.2;" 156 "https=socks://2.2.2.2;"
156 "ftp=http://3.3.3.3:9000;" 157 "ftp=http://3.3.3.3:9000;"
157 "socks=socks4://4.4.4.4:9090", 158 "socks=socks4://4.4.4.4:9090",
158 NO_PAC, 159 NO_PAC,
159 pref_service); 160 pref_service);
160 } 161 }
161 162
162 // Tests setting values only for incognito mode 163 // Tests setting values only for incognito mode
163 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, 164 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest,
164 ProxyFixedIndividualIncognitoOnly) { 165 ProxyFixedIndividualIncognitoOnly) {
165 CommandLine::ForCurrentProcess()->AppendSwitch( 166 CommandLine::ForCurrentProcess()->AppendSwitch(
166 switches::kEnableExperimentalExtensionApis); 167 switches::kEnableExperimentalExtensionApis);
167 168
168 ASSERT_TRUE(RunExtensionTest("proxy/individual_incognito_only")) << message_; 169 ASSERT_TRUE(RunExtensionTest("proxy/individual_incognito_only")) << message_;
169 const Extension* extension = GetSingleLoadedExtension(); 170 const Extension* extension = GetSingleLoadedExtension();
170 ASSERT_TRUE(extension); 171 ASSERT_TRUE(extension);
171 172
172 PrefService* pref_service = browser()->profile()->GetPrefs(); 173 PrefService* pref_service = browser()->profile()->GetPrefs();
173 AssertNoSettings(pref_service); 174 ExpectNoSettings(pref_service);
174 175
175 // Now check the incognito preferences. 176 // Now check the incognito preferences.
176 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs(); 177 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
177 AssertSettings(ProxyPrefs::MODE_FIXED_SERVERS, 178 ExpectSettings(ProxyPrefs::MODE_FIXED_SERVERS,
178 "http=http://1.1.1.1;" 179 "http=http://1.1.1.1;"
179 "https=socks://2.2.2.2;" 180 "https=socks://2.2.2.2;"
180 "ftp=http://3.3.3.3:9000;" 181 "ftp=http://3.3.3.3:9000;"
181 "socks=socks4://4.4.4.4:9090", 182 "socks=socks4://4.4.4.4:9090",
182 NO_PAC, 183 NO_PAC,
183 pref_service); 184 pref_service);
184 } 185 }
185 186
186 // Tests setting values also for incognito mode 187 // Tests setting values also for incognito mode
187 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, 188 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest,
188 ProxyFixedIndividualIncognitoAlso) { 189 ProxyFixedIndividualIncognitoAlso) {
189 CommandLine::ForCurrentProcess()->AppendSwitch( 190 CommandLine::ForCurrentProcess()->AppendSwitch(
190 switches::kEnableExperimentalExtensionApis); 191 switches::kEnableExperimentalExtensionApis);
191 192
192 ASSERT_TRUE(RunExtensionTest("proxy/individual_incognito_also")) << message_; 193 ASSERT_TRUE(RunExtensionTest("proxy/individual_incognito_also")) << message_;
193 const Extension* extension = GetSingleLoadedExtension(); 194 const Extension* extension = GetSingleLoadedExtension();
194 ASSERT_TRUE(extension); 195 ASSERT_TRUE(extension);
195 196
196 PrefService* pref_service = browser()->profile()->GetPrefs(); 197 PrefService* pref_service = browser()->profile()->GetPrefs();
197 AssertSettings(ProxyPrefs::MODE_FIXED_SERVERS, 198 ExpectSettings(ProxyPrefs::MODE_FIXED_SERVERS,
198 "http=http://1.1.1.1;" 199 "http=http://1.1.1.1;"
199 "https=socks://2.2.2.2;" 200 "https=socks://2.2.2.2;"
200 "ftp=http://3.3.3.3:9000;" 201 "ftp=http://3.3.3.3:9000;"
201 "socks=socks4://4.4.4.4:9090", 202 "socks=socks4://4.4.4.4:9090",
202 NO_PAC, 203 NO_PAC,
203 pref_service); 204 pref_service);
204 205
205 // Now check the incognito preferences. 206 // Now check the incognito preferences.
206 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs(); 207 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
207 AssertSettings(ProxyPrefs::MODE_FIXED_SERVERS, 208 ExpectSettings(ProxyPrefs::MODE_FIXED_SERVERS,
208 "http=http://5.5.5.5;" 209 "http=http://5.5.5.5;"
209 "https=socks://6.6.6.6;" 210 "https=socks://6.6.6.6;"
210 "ftp=http://7.7.7.7:9000;" 211 "ftp=http://7.7.7.7:9000;"
211 "socks=socks4://8.8.8.8:9090", 212 "socks=socks4://8.8.8.8:9090",
212 NO_PAC, 213 NO_PAC,
213 pref_service); 214 pref_service);
214 } 215 }
215 216
216 // Tests setting and unsetting values 217 // Tests setting and unsetting values
217 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyFixedIndividualRemove) { 218 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyFixedIndividualRemove) {
218 CommandLine::ForCurrentProcess()->AppendSwitch( 219 CommandLine::ForCurrentProcess()->AppendSwitch(
219 switches::kEnableExperimentalExtensionApis); 220 switches::kEnableExperimentalExtensionApis);
220 221
221 ASSERT_TRUE(RunExtensionTest("proxy/individual_remove")) << message_; 222 ASSERT_TRUE(RunExtensionTest("proxy/individual_remove")) << message_;
222 const Extension* extension = GetSingleLoadedExtension(); 223 const Extension* extension = GetSingleLoadedExtension();
223 ASSERT_TRUE(extension); 224 ASSERT_TRUE(extension);
224 225
225 PrefService* pref_service = browser()->profile()->GetPrefs(); 226 PrefService* pref_service = browser()->profile()->GetPrefs();
226 AssertNoSettings(pref_service); 227 ExpectNoSettings(pref_service);
227 } 228 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698