Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / api / networking_private / networking_private_apitest.cc
blob0325f4f2d59e6602073ad9eefd237155f9a9300b
1 // Copyright 2014 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 <map>
6 #include <vector>
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "components/onc/onc_constants.h"
14 #include "content/public/test/test_utils.h"
15 #include "extensions/browser/api/networking_private/networking_private_delegate.h"
16 #include "extensions/browser/api/networking_private/networking_private_delegate_factory.h"
17 #include "extensions/common/switches.h"
19 namespace extensions {
21 // This tests just the interface for the networkingPrivate API, i.e. it ensures
22 // that the delegate methods are called as expected.
24 // The implementations (which differ significantly between chromeos and
25 // windows/mac) are tested independently in
26 // networking_private_[chromeos|service_client]_apitest.cc.
27 // See also crbug.com/460119.
29 namespace {
31 const char kFailure[] = "Failure";
32 const char kSuccess[] = "Success";
33 const char kGuid[] = "SOME_GUID";
35 class TestDelegate : public NetworkingPrivateDelegate {
36 public:
37 explicit TestDelegate(scoped_ptr<VerifyDelegate> verify_delegate)
38 : NetworkingPrivateDelegate(verify_delegate.Pass()), fail_(false) {}
40 ~TestDelegate() override {}
42 // Asynchronous methods
43 void GetProperties(const std::string& guid,
44 const DictionaryCallback& success_callback,
45 const FailureCallback& failure_callback) override {
46 DictionaryResult(guid, success_callback, failure_callback);
49 void GetManagedProperties(const std::string& guid,
50 const DictionaryCallback& success_callback,
51 const FailureCallback& failure_callback) override {
52 DictionaryResult(guid, success_callback, failure_callback);
55 void GetState(const std::string& guid,
56 const DictionaryCallback& success_callback,
57 const FailureCallback& failure_callback) override {
58 DictionaryResult(guid, success_callback, failure_callback);
61 void SetProperties(const std::string& guid,
62 scoped_ptr<base::DictionaryValue> properties,
63 const VoidCallback& success_callback,
64 const FailureCallback& failure_callback) override {
65 VoidResult(success_callback, failure_callback);
68 void CreateNetwork(bool shared,
69 scoped_ptr<base::DictionaryValue> properties,
70 const StringCallback& success_callback,
71 const FailureCallback& failure_callback) override {
72 StringResult(success_callback, failure_callback);
75 void ForgetNetwork(const std::string& guid,
76 const VoidCallback& success_callback,
77 const FailureCallback& failure_callback) override {
78 VoidResult(success_callback, failure_callback);
81 void GetNetworks(const std::string& network_type,
82 bool configured_only,
83 bool visible_only,
84 int limit,
85 const NetworkListCallback& success_callback,
86 const FailureCallback& failure_callback) override {
87 if (fail_) {
88 failure_callback.Run(kFailure);
89 } else {
90 scoped_ptr<base::ListValue> result(new base::ListValue);
91 scoped_ptr<base::DictionaryValue> network(new base::DictionaryValue);
92 network->SetString(::onc::network_config::kType,
93 ::onc::network_config::kEthernet);
94 network->SetString(::onc::network_config::kGUID, kGuid);
95 result->Append(network.release());
96 success_callback.Run(result.Pass());
100 void StartConnect(const std::string& guid,
101 const VoidCallback& success_callback,
102 const FailureCallback& failure_callback) override {
103 VoidResult(success_callback, failure_callback);
106 void StartDisconnect(const std::string& guid,
107 const VoidCallback& success_callback,
108 const FailureCallback& failure_callback) override {
109 VoidResult(success_callback, failure_callback);
112 void StartActivate(const std::string& guid,
113 const std::string& carrier,
114 const VoidCallback& success_callback,
115 const FailureCallback& failure_callback) override {
116 VoidResult(success_callback, failure_callback);
119 void SetWifiTDLSEnabledState(
120 const std::string& ip_or_mac_address,
121 bool enabled,
122 const StringCallback& success_callback,
123 const FailureCallback& failure_callback) override {
124 StringResult(success_callback, failure_callback);
127 void GetWifiTDLSStatus(const std::string& ip_or_mac_address,
128 const StringCallback& success_callback,
129 const FailureCallback& failure_callback) override {
130 StringResult(success_callback, failure_callback);
133 void GetCaptivePortalStatus(
134 const std::string& guid,
135 const StringCallback& success_callback,
136 const FailureCallback& failure_callback) override {
137 StringResult(success_callback, failure_callback);
140 // Synchronous methods
141 scoped_ptr<base::ListValue> GetEnabledNetworkTypes() override {
142 scoped_ptr<base::ListValue> result;
143 if (!fail_) {
144 result.reset(new base::ListValue);
145 result->AppendString(::onc::network_config::kEthernet);
147 return result.Pass();
150 scoped_ptr<DeviceStateList> GetDeviceStateList() override {
151 scoped_ptr<DeviceStateList> result;
152 if (fail_)
153 return result.Pass();
154 result.reset(new DeviceStateList);
155 scoped_ptr<api::networking_private::DeviceStateProperties> properties(
156 new api::networking_private::DeviceStateProperties);
157 properties->type = api::networking_private::NETWORK_TYPE_ETHERNET;
158 properties->state = api::networking_private::DEVICE_STATE_TYPE_ENABLED;
159 result->push_back(properties.Pass());
160 return result.Pass();
163 bool EnableNetworkType(const std::string& type) override {
164 enabled_[type] = true;
165 return !fail_;
168 bool DisableNetworkType(const std::string& type) override {
169 disabled_[type] = true;
170 return !fail_;
173 bool RequestScan() override {
174 scan_requested_.push_back(true);
175 return !fail_;
178 void set_fail(bool fail) { fail_ = fail; }
179 bool GetEnabled(const std::string& type) { return enabled_[type]; }
180 bool GetDisabled(const std::string& type) { return disabled_[type]; }
181 size_t GetScanRequested() { return scan_requested_.size(); }
183 void DictionaryResult(const std::string& guid,
184 const DictionaryCallback& success_callback,
185 const FailureCallback& failure_callback) {
186 if (fail_) {
187 failure_callback.Run(kFailure);
188 } else {
189 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
190 result->SetString(::onc::network_config::kGUID, guid);
191 result->SetString(::onc::network_config::kType,
192 ::onc::network_config::kWiFi);
193 success_callback.Run(result.Pass());
197 void StringResult(const StringCallback& success_callback,
198 const FailureCallback& failure_callback) {
199 if (fail_) {
200 failure_callback.Run(kFailure);
201 } else {
202 success_callback.Run(kSuccess);
206 void BoolResult(const BoolCallback& success_callback,
207 const FailureCallback& failure_callback) {
208 if (fail_) {
209 failure_callback.Run(kFailure);
210 } else {
211 success_callback.Run(true);
215 void VoidResult(const VoidCallback& success_callback,
216 const FailureCallback& failure_callback) {
217 if (fail_) {
218 failure_callback.Run(kFailure);
219 } else {
220 success_callback.Run();
224 private:
225 bool fail_;
226 std::map<std::string, bool> enabled_;
227 std::map<std::string, bool> disabled_;
228 std::vector<bool> scan_requested_;
230 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
233 class TestVerifyDelegate : public NetworkingPrivateDelegate::VerifyDelegate {
234 public:
235 TestVerifyDelegate() : owner_(NULL) {}
237 ~TestVerifyDelegate() override {}
239 void VerifyDestination(
240 const VerificationProperties& verification_properties,
241 const BoolCallback& success_callback,
242 const FailureCallback& failure_callback) override {
243 owner_->BoolResult(success_callback, failure_callback);
245 void VerifyAndEncryptCredentials(
246 const std::string& guid,
247 const VerificationProperties& verification_properties,
248 const StringCallback& success_callback,
249 const FailureCallback& failure_callback) override {
250 owner_->StringResult(success_callback, failure_callback);
252 void VerifyAndEncryptData(
253 const VerificationProperties& verification_properties,
254 const std::string& data,
255 const StringCallback& success_callback,
256 const FailureCallback& failure_callback) override {
257 owner_->StringResult(success_callback, failure_callback);
260 void set_owner(TestDelegate* owner) { owner_ = owner; }
262 private:
263 TestDelegate* owner_;
265 DISALLOW_COPY_AND_ASSIGN(TestVerifyDelegate);
268 class NetworkingPrivateApiTest : public ExtensionApiTest {
269 public:
270 NetworkingPrivateApiTest() {
271 if (!s_test_delegate_) {
272 TestVerifyDelegate* verify_delegate = new TestVerifyDelegate;
273 scoped_ptr<NetworkingPrivateDelegate::VerifyDelegate> verify_delegate_ptr(
274 verify_delegate);
275 s_test_delegate_ = new TestDelegate(verify_delegate_ptr.Pass());
276 verify_delegate->set_owner(s_test_delegate_);
280 static scoped_ptr<KeyedService> GetNetworkingPrivateDelegate(
281 content::BrowserContext* profile) {
282 CHECK(s_test_delegate_);
283 return make_scoped_ptr(s_test_delegate_);
286 void SetUpCommandLine(base::CommandLine* command_line) override {
287 ExtensionApiTest::SetUpCommandLine(command_line);
288 // Whitelist the extension ID of the test extension.
289 command_line->AppendSwitchASCII(
290 extensions::switches::kWhitelistedExtensionID,
291 "epcifkihnkjgphfkloaaleeakhpmgdmn");
294 void SetUpOnMainThread() override {
295 ExtensionApiTest::SetUpOnMainThread();
296 NetworkingPrivateDelegateFactory::GetInstance()->SetTestingFactory(
297 profile(), &NetworkingPrivateApiTest::GetNetworkingPrivateDelegate);
298 content::RunAllPendingInMessageLoop();
301 bool GetEnabled(const std::string& type) {
302 return s_test_delegate_->GetEnabled(type);
305 bool GetDisabled(const std::string& type) {
306 return s_test_delegate_->GetDisabled(type);
309 size_t GetScanRequested() {
310 return s_test_delegate_->GetScanRequested();
313 protected:
314 bool RunNetworkingSubtest(const std::string& subtest) {
315 return RunExtensionSubtest("networking_private",
316 "main.html?" + subtest,
317 kFlagEnableFileAccess | kFlagLoadAsComponent);
320 // Static pointer to the TestDelegate so that it can be accessed in
321 // GetNetworkingPrivateDelegate() passed to SetTestingFactory().
322 static TestDelegate* s_test_delegate_;
324 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTest);
327 // static
328 TestDelegate* NetworkingPrivateApiTest::s_test_delegate_ = NULL;
330 } // namespace
332 // Place each subtest into a separate browser test so that the stub networking
333 // library state is reset for each subtest run. This way they won't affect each
334 // other. TODO(stevenjb): Use extensions::ApiUnitTest once moved to
335 // src/extensions.
337 // These fail on Windows due to crbug.com/177163. Note: we still have partial
338 // coverage in NetworkingPrivateServiceClientApiTest. TODO(stevenjb): Enable
339 // these on Windows once we switch to extensions::ApiUnitTest.
341 #if !defined(OS_WIN)
343 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetProperties) {
344 EXPECT_TRUE(RunNetworkingSubtest("getProperties")) << message_;
347 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetManagedProperties) {
348 EXPECT_TRUE(RunNetworkingSubtest("getManagedProperties")) << message_;
351 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetState) {
352 EXPECT_TRUE(RunNetworkingSubtest("getState")) << message_;
355 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, SetProperties) {
356 EXPECT_TRUE(RunNetworkingSubtest("setProperties")) << message_;
359 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, CreateNetwork) {
360 EXPECT_TRUE(RunNetworkingSubtest("createNetwork")) << message_;
363 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, ForgetNetwork) {
364 EXPECT_TRUE(RunNetworkingSubtest("forgetNetwork")) << message_;
367 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetNetworks) {
368 EXPECT_TRUE(RunNetworkingSubtest("getNetworks")) << message_;
371 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetVisibleNetworks) {
372 EXPECT_TRUE(RunNetworkingSubtest("getVisibleNetworks")) << message_;
375 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetEnabledNetworkTypes) {
376 EXPECT_TRUE(RunNetworkingSubtest("getEnabledNetworkTypes")) << message_;
379 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetDeviceStates) {
380 EXPECT_TRUE(RunNetworkingSubtest("getDeviceStates")) << message_;
383 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, EnableNetworkType) {
384 EXPECT_TRUE(RunNetworkingSubtest("enableNetworkType")) << message_;
385 EXPECT_TRUE(GetEnabled(::onc::network_config::kEthernet));
388 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, DisableNetworkType) {
389 EXPECT_TRUE(RunNetworkingSubtest("disableNetworkType")) << message_;
390 EXPECT_TRUE(GetDisabled(::onc::network_config::kEthernet));
393 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, RequestNetworkScan) {
394 EXPECT_TRUE(RunNetworkingSubtest("requestNetworkScan")) << message_;
395 EXPECT_EQ(1u, GetScanRequested());
398 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, StartConnect) {
399 EXPECT_TRUE(RunNetworkingSubtest("startConnect")) << message_;
402 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, StartDisconnect) {
403 EXPECT_TRUE(RunNetworkingSubtest("startDisconnect")) << message_;
406 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, StartActivate) {
407 EXPECT_TRUE(RunNetworkingSubtest("startActivate")) << message_;
410 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, VerifyDestination) {
411 EXPECT_TRUE(RunNetworkingSubtest("verifyDestination")) << message_;
414 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, VerifyAndEncryptCredentials) {
415 EXPECT_TRUE(RunNetworkingSubtest("verifyAndEncryptCredentials")) << message_;
418 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, VerifyAndEncryptData) {
419 EXPECT_TRUE(RunNetworkingSubtest("verifyAndEncryptData")) << message_;
422 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, SetWifiTDLSEnabledState) {
423 EXPECT_TRUE(RunNetworkingSubtest("setWifiTDLSEnabledState")) << message_;
426 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetWifiTDLSStatus) {
427 EXPECT_TRUE(RunNetworkingSubtest("getWifiTDLSStatus")) << message_;
430 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTest, GetCaptivePortalStatus) {
431 EXPECT_TRUE(RunNetworkingSubtest("getCaptivePortalStatus")) << message_;
434 // Test failure case
436 class NetworkingPrivateApiTestFail : public NetworkingPrivateApiTest {
437 public:
438 NetworkingPrivateApiTestFail() { s_test_delegate_->set_fail(true); }
440 protected:
441 DISALLOW_COPY_AND_ASSIGN(NetworkingPrivateApiTestFail);
444 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetProperties) {
445 EXPECT_FALSE(RunNetworkingSubtest("getProperties")) << message_;
448 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetManagedProperties) {
449 EXPECT_FALSE(RunNetworkingSubtest("getManagedProperties")) << message_;
452 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetState) {
453 EXPECT_FALSE(RunNetworkingSubtest("getState")) << message_;
456 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, SetProperties) {
457 EXPECT_FALSE(RunNetworkingSubtest("setProperties")) << message_;
460 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, CreateNetwork) {
461 EXPECT_FALSE(RunNetworkingSubtest("createNetwork")) << message_;
464 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, ForgetNetwork) {
465 EXPECT_FALSE(RunNetworkingSubtest("forgetNetwork")) << message_;
468 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetNetworks) {
469 EXPECT_FALSE(RunNetworkingSubtest("getNetworks")) << message_;
472 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetVisibleNetworks) {
473 EXPECT_FALSE(RunNetworkingSubtest("getVisibleNetworks")) << message_;
476 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetEnabledNetworkTypes) {
477 EXPECT_FALSE(RunNetworkingSubtest("getEnabledNetworkTypes")) << message_;
480 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetDeviceStates) {
481 EXPECT_FALSE(RunNetworkingSubtest("getDeviceStates")) << message_;
484 // Note: Synchronous methods never fail:
485 // * disableNetworkType
486 // * enableNetworkType
487 // * requestNetworkScan
489 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, StartConnect) {
490 EXPECT_FALSE(RunNetworkingSubtest("startConnect")) << message_;
493 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, StartDisconnect) {
494 EXPECT_FALSE(RunNetworkingSubtest("startDisconnect")) << message_;
497 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, StartActivate) {
498 EXPECT_FALSE(RunNetworkingSubtest("startActivate")) << message_;
501 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, VerifyDestination) {
502 EXPECT_FALSE(RunNetworkingSubtest("verifyDestination")) << message_;
505 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail,
506 VerifyAndEncryptCredentials) {
507 EXPECT_FALSE(RunNetworkingSubtest("verifyAndEncryptCredentials")) << message_;
510 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, VerifyAndEncryptData) {
511 EXPECT_FALSE(RunNetworkingSubtest("verifyAndEncryptData")) << message_;
514 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, SetWifiTDLSEnabledState) {
515 EXPECT_FALSE(RunNetworkingSubtest("setWifiTDLSEnabledState")) << message_;
518 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetWifiTDLSStatus) {
519 EXPECT_FALSE(RunNetworkingSubtest("getWifiTDLSStatus")) << message_;
522 IN_PROC_BROWSER_TEST_F(NetworkingPrivateApiTestFail, GetCaptivePortalStatus) {
523 EXPECT_FALSE(RunNetworkingSubtest("getCaptivePortalStatus")) << message_;
526 #endif // defined(OS_WIN)
528 } // namespace extensions