Index: chrome/browser/chromeos/proxy_config_service_impl_unittest.cc |
diff --git a/chrome/browser/chromeos/proxy_config_service_impl_unittest.cc b/chrome/browser/chromeos/proxy_config_service_impl_unittest.cc |
index 92ba816647fef2dad97de84f1ba853f2b43a6134..1f336f97e40a9e801ac998d403d5a279bcfda9a5 100644 |
--- a/chrome/browser/chromeos/proxy_config_service_impl_unittest.cc |
+++ b/chrome/browser/chromeos/proxy_config_service_impl_unittest.cc |
@@ -1,4 +1,4 @@ |
-// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2011 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. |
@@ -12,8 +12,14 @@ |
#include "base/logging.h" |
#include "base/string_util.h" |
#include "base/stringprintf.h" |
-#include "chrome/browser/browser_thread.h" |
#include "chrome/browser/chromeos/cros/cros_library.h" |
+#include "chrome/browser/prefs/browser_prefs.h" |
+#include "chrome/browser/prefs/pref_change_registrar.h" |
+#include "chrome/browser/prefs/pref_observer_mock.h" |
+#include "chrome/browser/prefs/proxy_config_dictionary.h" |
+#include "chrome/browser/prefs/proxy_prefs.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/test/testing_pref_service.h" |
#include "net/proxy/proxy_config_service_common_unittest.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "testing/platform_test.h" |
@@ -22,12 +28,14 @@ |
#include "chrome/common/json_value_serializer.h" |
#endif // !defined(NDEBUG) |
+using testing::Mock; |
+ |
namespace chromeos { |
namespace { |
struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig. |
- ProxyConfigServiceImpl::ProxyConfig::Mode mode; |
+ chromeos::ProxyConfigServiceImpl::ProxyConfig::Mode mode; |
const char* pac_url; |
const char* single_uri; |
const char* http_uri; |
@@ -46,7 +54,7 @@ struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig. |
#define MK_SCHM(scheme) net::ProxyServer::SCHEME_##scheme |
// Inspired from net/proxy/proxy_config_service_linux_unittest.cc. |
-const struct { |
+const struct TestData { |
// Short description to identify the test |
std::string description; |
@@ -55,9 +63,9 @@ const struct { |
Input input; |
- // Expected outputs from fields of net::ProxyConfig (via IO). |
- bool auto_detect; |
- GURL pac_url; |
+ // Expected output in Chrome proxy config. |
+ ProxyPrefs::ProxyMode mode; |
+ const char* pac_url; |
net::ProxyRulesExpectation proxy_rules; |
} tests[] = { |
{ |
@@ -71,8 +79,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_DIRECT, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::Empty(), // proxy_rules |
}, |
@@ -87,8 +95,8 @@ const struct { |
}, |
// Expected result. |
- true, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_AUTO_DETECT, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::Empty(), // proxy_rules |
}, |
@@ -104,8 +112,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL("http://wpad/wpad.dat"), // pac_url |
+ ProxyPrefs::MODE_PAC_SCRIPT, // mode |
+ "http://wpad/wpad.dat", // pac_url |
net::ProxyRulesExpectation::Empty(), // proxy_rules |
}, |
@@ -121,8 +129,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_PAC_SCRIPT, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::Empty(), // proxy_rules |
}, |
@@ -139,8 +147,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_FIXED_SERVERS, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::Single( // proxy_rules |
"www.google.com:80", // single proxy |
""), // bypass rules |
@@ -159,8 +167,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_FIXED_SERVERS, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::Single( // proxy_rules |
"www.google.com:99", // single |
""), // bypass rules |
@@ -179,8 +187,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_FIXED_SERVERS, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::Single( // proxy_rules |
"www.google.com:99", // single proxy |
""), // bypass rules |
@@ -203,8 +211,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_FIXED_SERVERS, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules |
"www.google.com:80", // http |
"https://www.foo.com:110", // https |
@@ -228,8 +236,8 @@ const struct { |
}, |
// Expected result. |
- false, // auto_detect |
- GURL(), // pac_url |
+ ProxyPrefs::MODE_FIXED_SERVERS, // mode |
+ "", // pac_url |
net::ProxyRulesExpectation::Single( // proxy_rules |
"www.google.com:80", // single proxy |
"*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8"), // bypass_rules |
@@ -240,20 +248,15 @@ const struct { |
class ProxyConfigServiceImplTest : public PlatformTest { |
protected: |
- ProxyConfigServiceImplTest() |
- : ui_thread_(BrowserThread::UI, &message_loop_), |
- io_thread_(BrowserThread::IO, &message_loop_) { |
- } |
- |
- virtual ~ProxyConfigServiceImplTest() { |
- config_service_ = NULL; |
- MessageLoop::current()->RunAllPending(); |
+ ProxyConfigServiceImplTest() { |
+ browser::RegisterLocalState(&local_state_); |
} |
void CreateConfigService( |
const ProxyConfigServiceImpl::ProxyConfig& init_config) { |
// Instantiate proxy config service with |init_config|. |
- config_service_ = new ProxyConfigServiceImpl(init_config); |
+ config_service_.reset( |
+ new ProxyConfigServiceImpl(&local_state_, init_config)); |
} |
void SetAutomaticProxy( |
@@ -397,17 +400,34 @@ class ProxyConfigServiceImplTest : public PlatformTest { |
} |
} |
- // Synchronously gets the latest proxy config. |
- bool SyncGetLatestProxyConfig(net::ProxyConfig* config) { |
- // Let message loop process all messages. |
- MessageLoop::current()->RunAllPending(); |
- // Calls IOGetProxyConfig (which is called from |
- // ProxyConfigService::GetLatestProxyConfig), running on faked IO thread. |
- return config_service_->IOGetProxyConfig(config); |
+ void CheckPrefProxyConfig(const TestData& test_data) { |
+ ProxyConfigDictionary proxy_dict( |
+ local_state_.GetDictionary(prefs::kProxy)); |
+ |
+ ProxyPrefs::ProxyMode actual_mode; |
+ proxy_dict.GetMode(&actual_mode); |
+ EXPECT_EQ(test_data.mode, actual_mode); |
+ |
+ std::string actual_pac_url; |
+ proxy_dict.GetPacUrl(&actual_pac_url); |
+ EXPECT_EQ(test_data.pac_url, actual_pac_url); |
+ |
+ std::string actual_rules_string; |
+ proxy_dict.GetProxyServer(&actual_rules_string); |
+ net::ProxyConfig::ProxyRules actual_rules; |
+ actual_rules.ParseFromString(actual_rules_string); |
+ std::string actual_bypass_rules_string; |
+ proxy_dict.GetBypassList(&actual_bypass_rules_string); |
+ actual_rules.bypass_rules.ParseFromString(actual_bypass_rules_string); |
+ EXPECT_TRUE(test_data.proxy_rules.Matches(actual_rules)); |
} |
ProxyConfigServiceImpl* config_service() const { |
- return config_service_; |
+ return config_service_.get(); |
+ } |
+ |
+ PrefService* local_state() { |
+ return &local_state_; |
} |
private: |
@@ -416,15 +436,12 @@ class ProxyConfigServiceImplTest : public PlatformTest { |
return source == MK_SRC(POLICY) ? false : true; |
} |
+ TestingPrefService local_state_; |
ScopedStubCrosEnabler stub_cros_enabler_; |
- MessageLoop message_loop_; |
- BrowserThread ui_thread_; |
- BrowserThread io_thread_; |
- |
- scoped_refptr<ProxyConfigServiceImpl> config_service_; |
+ scoped_ptr<ProxyConfigServiceImpl> config_service_; |
}; |
-TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToNetProxyConfig) { |
+TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToChromeProxyConfig) { |
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, |
tests[i].description.c_str())); |
@@ -433,12 +450,8 @@ TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToNetProxyConfig) { |
InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &init_config); |
CreateConfigService(init_config); |
- net::ProxyConfig config; |
- SyncGetLatestProxyConfig(&config); |
- |
- EXPECT_EQ(tests[i].auto_detect, config.auto_detect()); |
- EXPECT_EQ(tests[i].pac_url, config.pac_url()); |
- EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules())); |
+ // Check whether the pref has been set correctly. |
+ CheckPrefProxyConfig(tests[i]); |
} |
} |
@@ -498,12 +511,8 @@ TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) { |
break; |
} |
- // Retrieve config from IO thread. |
- net::ProxyConfig io_config; |
- SyncGetLatestProxyConfig(&io_config); |
- EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect()); |
- EXPECT_EQ(tests[i].pac_url, io_config.pac_url()); |
- EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules())); |
+ // Check whether correct configuration has been written to the pref. |
+ CheckPrefProxyConfig(tests[i]); |
// Retrieve config from UI thread. |
ProxyConfigServiceImpl::ProxyConfig ui_config; |
@@ -536,47 +545,30 @@ TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) { |
} |
TEST_F(ProxyConfigServiceImplTest, ProxyChangedObserver) { |
- // This is used to observe for OnProxyConfigChanged notification. |
- class ProxyChangedObserver : public net::ProxyConfigService::Observer { |
- public: |
- explicit ProxyChangedObserver( |
- const scoped_refptr<ProxyConfigServiceImpl>& config_service) |
- : config_service_(config_service) { |
- config_service_->AddObserver(this); |
- } |
- virtual ~ProxyChangedObserver() { |
- config_service_->RemoveObserver(this); |
- } |
- const net::ProxyConfig& config() const { |
- return config_; |
- } |
- |
- private: |
- virtual void OnProxyConfigChanged(const net::ProxyConfig& config) { |
- config_ = config; |
- } |
- |
- scoped_refptr<ProxyConfigServiceImpl> config_service_; |
- net::ProxyConfig config_; |
- }; |
- |
// Init with direct. |
ProxyConfigServiceImpl::ProxyConfig init_config; |
SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config, |
&init_config.automatic_proxy); |
CreateConfigService(init_config); |
- ProxyChangedObserver observer(config_service()); |
+ PrefObserverMock observer; |
+ PrefChangeRegistrar registrar; |
+ registrar.Init(local_state()); |
+ registrar.Add(prefs::kProxy, &observer); |
// Set to pac script from UI. |
- EXPECT_TRUE(config_service()->UISetProxyConfigToPACScript( |
- GURL("http://wpad.dat"))); |
- // Retrieve config from IO thread. |
- net::ProxyConfig io_config; |
- SyncGetLatestProxyConfig(&io_config); |
+ GURL pac_url("http://wpad.dat"); |
+ scoped_ptr<DictionaryValue> expected_proxy_config( |
+ ProxyConfigDictionary::CreatePacScript(pac_url.spec())); |
+ |
+ observer.Expect(local_state(), prefs::kProxy, expected_proxy_config.get()); |
+ EXPECT_TRUE(config_service()->UISetProxyConfigToPACScript(pac_url)); |
+ Mock::VerifyAndClearExpectations(&observer); |
// Observer should have gotten the same new proxy config. |
- EXPECT_TRUE(io_config.Equals(observer.config())); |
+ const DictionaryValue* actual_config = |
+ local_state()->GetDictionary(prefs::kProxy); |
+ EXPECT_TRUE(expected_proxy_config->Equals(actual_config)); |
} |
TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserialize) { |
@@ -599,22 +591,22 @@ TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserialize) { |
EXPECT_TRUE(target_config.Deserialize(serialized_value)); |
// Compare the configs after serialization and deserialization. |
- net::ProxyConfig net_src_cfg; |
- net::ProxyConfig net_tgt_cfg; |
- source_config.ToNetProxyConfig(&net_src_cfg); |
- target_config.ToNetProxyConfig(&net_tgt_cfg); |
+ scoped_ptr<DictionaryValue> src_cfg_dict( |
+ source_config.ToProxyConfigDictionary()); |
+ scoped_ptr<DictionaryValue> tgt_cfg_dict( |
+ target_config.ToProxyConfigDictionary()); |
#if !defined(NDEBUG) |
- if (!net_src_cfg.Equals(net_tgt_cfg)) { |
+ if (!src_cfg_dict->Equals(tgt_cfg_dict.get())) { |
std::string src_output, tgt_output; |
JSONStringValueSerializer src_serializer(&src_output); |
- src_serializer.Serialize(*net_src_cfg.ToValue()); |
+ src_serializer.Serialize(*src_cfg_dict); |
JSONStringValueSerializer tgt_serializer(&tgt_output); |
- tgt_serializer.Serialize(*net_tgt_cfg.ToValue()); |
+ tgt_serializer.Serialize(*tgt_cfg_dict); |
VLOG(1) << "source:\n" << src_output |
<< "\ntarget:\n" << tgt_output; |
} |
#endif // !defined(NDEBUG) |
- EXPECT_TRUE(net_src_cfg.Equals(net_tgt_cfg)); |
+ EXPECT_TRUE(src_cfg_dict->Equals(tgt_cfg_dict.get())); |
} |
} |