Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / wifi_sync / wifi_config_delegate_chromeos_unittest.cc
blob2a9cfac09c65563ae0cfc5be5b6df8de6fb866f6
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.
5 #include "components/wifi_sync/wifi_config_delegate_chromeos.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "base/values.h"
10 #include "chromeos/network/managed_network_configuration_handler.h"
11 #include "chromeos/network/network_handler_callbacks.h"
12 #include "components/wifi_sync/wifi_credential.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace wifi_sync {
17 namespace {
18 const char kSsid[] = "fake-ssid";
19 const char kSsidNonUtf8[] = "\xc0";
20 const char kUserHash[] = "fake-user-hash";
23 using chromeos::network_handler::DictionaryResultCallback;
24 using chromeos::network_handler::ErrorCallback;
25 using chromeos::network_handler::StringResultCallback;
27 class FakeManagedNetworkConfigurationHandler
28 : public chromeos::ManagedNetworkConfigurationHandler {
29 public:
30 FakeManagedNetworkConfigurationHandler()
31 : create_configuration_called_(false) {
34 // ManagedNetworkConfigurationHandler implementation.
35 void AddObserver(chromeos::NetworkPolicyObserver* observer) override {
36 NOTIMPLEMENTED();
38 void RemoveObserver(chromeos::NetworkPolicyObserver* observer) override {
39 NOTIMPLEMENTED();
41 void GetProperties(
42 const std::string& service_path,
43 const DictionaryResultCallback& callback,
44 const ErrorCallback& error_callback) override {
45 NOTIMPLEMENTED();
47 void GetManagedProperties(
48 const std::string& userhash,
49 const std::string& service_path,
50 const DictionaryResultCallback& callback,
51 const ErrorCallback& error_callback) override {
52 NOTIMPLEMENTED();
54 void SetProperties(
55 const std::string& service_path,
56 const base::DictionaryValue& user_settings,
57 const base::Closure& callback,
58 const ErrorCallback& error_callback) override {
59 NOTIMPLEMENTED();
61 void CreateConfiguration(
62 const std::string& userhash,
63 const base::DictionaryValue& properties,
64 const StringResultCallback& callback,
65 const ErrorCallback& error_callback) const override {
66 EXPECT_FALSE(create_configuration_called_);
67 create_configuration_called_ = true;
68 create_configuration_success_callback_ = callback;
69 create_configuration_error_callback_ = error_callback;
71 void RemoveConfiguration(
72 const std::string& service_path,
73 const base::Closure& callback,
74 const ErrorCallback& error_callback) const override {
75 NOTIMPLEMENTED();
77 void SetPolicy(
78 ::onc::ONCSource onc_source,
79 const std::string& userhash,
80 const base::ListValue& network_configs_onc,
81 const base::DictionaryValue& global_network_config) override {
82 NOTIMPLEMENTED();
84 bool IsAnyPolicyApplicationRunning() const override {
85 NOTIMPLEMENTED();
86 return false;
88 const base::DictionaryValue* FindPolicyByGUID(
89 const std::string userhash,
90 const std::string& guid,
91 ::onc::ONCSource* onc_source) const override {
92 NOTIMPLEMENTED();
93 return nullptr;
95 const GuidToPolicyMap* GetNetworkConfigsFromPolicy(
96 const std::string& userhash) const override {
97 NOTIMPLEMENTED();
98 return nullptr;
100 const base::DictionaryValue* GetGlobalConfigFromPolicy(
101 const std::string& userhash) const override {
102 NOTIMPLEMENTED();
103 return nullptr;
105 const base::DictionaryValue* FindPolicyByGuidAndProfile(
106 const std::string& guid,
107 const std::string& profile_path) const override {
108 NOTIMPLEMENTED();
109 return nullptr;
112 bool create_configuration_called() const {
113 return create_configuration_called_;
115 const StringResultCallback& create_configuration_success_callback() const {
116 return create_configuration_success_callback_;
118 const ErrorCallback& create_configuration_error_callback() const {
119 return create_configuration_error_callback_;
122 private:
123 // Whether or not CreateConfiguration has been called on this fake.
124 mutable bool create_configuration_called_;
125 // The last |callback| passed to CreateConfiguration.
126 mutable StringResultCallback create_configuration_success_callback_;
127 // The last |error_callback| passed to CreateConfiguration.
128 mutable ErrorCallback create_configuration_error_callback_;
131 class WifiConfigDelegateChromeOsTest : public testing::Test {
132 protected:
133 WifiConfigDelegateChromeOsTest()
134 : fake_managed_network_configuration_handler_(
135 new FakeManagedNetworkConfigurationHandler()) {
136 config_delegate_.reset(
137 new WifiConfigDelegateChromeOs(
138 kUserHash,
139 fake_managed_network_configuration_handler_.get()));
142 // Wrapper for WifiConfigDelegateChromeOs::AddToLocalNetworks.
143 void AddToLocalNetworks(const WifiCredential& network_credential) {
144 config_delegate_->AddToLocalNetworks(network_credential);
147 // Returns a new WifiCredential constructed from the given parameters.
148 WifiCredential MakeCredential(const std::string& ssid,
149 WifiSecurityClass security_class,
150 const std::string& passphrase) {
151 scoped_ptr<WifiCredential> credential =
152 WifiCredential::Create(
153 WifiCredential::MakeSsidBytesForTest(ssid),
154 security_class,
155 passphrase);
156 CHECK(credential);
157 return *credential;
160 // Runs the last |callback| passed to CreateConfiguration, unless
161 // that |callback| is null.
162 void RunCreateConfigurationSuccessCallback() {
163 const char new_service_path[] = "/service/0";
164 const StringResultCallback callback =
165 fake_managed_network_configuration_handler_
166 ->create_configuration_success_callback();
167 if (!callback.is_null())
168 callback.Run(new_service_path);
171 // Returns whether or not CreateConfiguration has been called
172 // on |fake_managed_network_configuration_handler_|.
173 size_t create_configuration_called() const {
174 return fake_managed_network_configuration_handler_
175 ->create_configuration_called();
178 // Returns the last |error_callback| passed to the CreateConfiguration
179 // method of |fake_managed_network_configuration_handler_|.
180 const ErrorCallback& create_configuration_error_callback() const {
181 return fake_managed_network_configuration_handler_
182 ->create_configuration_error_callback();
185 private:
186 scoped_ptr<WifiConfigDelegateChromeOs> config_delegate_;
187 scoped_ptr<FakeManagedNetworkConfigurationHandler>
188 fake_managed_network_configuration_handler_;
190 DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOsTest);
193 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksOpen) {
194 AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_NONE, ""));
195 ASSERT_TRUE(create_configuration_called());
196 RunCreateConfigurationSuccessCallback();
199 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksWep) {
200 AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_WEP, "abcde"));
201 ASSERT_TRUE(create_configuration_called());
202 RunCreateConfigurationSuccessCallback();
205 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksPsk) {
206 AddToLocalNetworks(
207 MakeCredential(kSsid, SECURITY_CLASS_PSK, "fake-psk-passphrase"));
208 ASSERT_TRUE(create_configuration_called());
209 RunCreateConfigurationSuccessCallback();
212 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksNonUtf8) {
213 AddToLocalNetworks(MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, ""));
214 // TODO(quiche): Change to EXPECT_TRUE, once we support non-UTF-8 SSIDs.
215 EXPECT_FALSE(create_configuration_called());
218 TEST_F(WifiConfigDelegateChromeOsTest,
219 AddToLocalNetworksCreateConfigurationFailure) {
220 AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_NONE, ""));
221 EXPECT_TRUE(create_configuration_called());
222 if (!create_configuration_error_callback().is_null()) {
223 create_configuration_error_callback().Run(
224 "Config.CreateConfiguration Failed",
225 make_scoped_ptr(new base::DictionaryValue()));
229 } // namespace wifi_sync