QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / ppapi / tests / test_network_monitor.cc
blobdcb712c14e35ade3ad2194e78de85799173ce5f6
1 // Copyright (c) 2012 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 "ppapi/tests/test_network_monitor.h"
7 #include <string.h>
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/instance_handle.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/net_address.h"
13 #include "ppapi/cpp/network_list.h"
14 #include "ppapi/cpp/network_monitor.h"
15 #include "ppapi/tests/test_utils.h"
16 #include "ppapi/tests/testing_instance.h"
18 REGISTER_TEST_CASE(NetworkMonitor);
20 namespace {
22 class MonitorDeletionCallbackDelegate
23 : public TestCompletionCallback::Delegate {
24 public:
25 explicit MonitorDeletionCallbackDelegate(pp::NetworkMonitor* monitor)
26 : monitor_(monitor) {
29 // TestCompletionCallback::Delegate interface.
30 virtual void OnCallback(void* user_data, int32_t result) {
31 delete monitor_;
34 private:
35 pp::NetworkMonitor* monitor_;
38 } // namespace
40 TestNetworkMonitor::TestNetworkMonitor(TestingInstance* instance)
41 : TestCase(instance) {
44 bool TestNetworkMonitor::Init() {
45 if (!pp::NetworkMonitor::IsAvailable())
46 return false;
48 return CheckTestingInterface();
51 void TestNetworkMonitor::RunTests(const std::string& filter) {
52 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter);
53 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter);
54 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback, filter);
57 std::string TestNetworkMonitor::VerifyNetworkList(
58 const pp::NetworkList& network_list) {
59 // Verify that there is at least one network interface.
60 size_t count = network_list.GetCount();
61 ASSERT_TRUE(count >= 1U);
63 // Iterate over all interfaces and verify their properties.
64 for (size_t iface = 0; iface < count; ++iface) {
65 // Verify that the first interface has at least one address.
66 std::vector<pp::NetAddress> addresses;
67 network_list.GetIpAddresses(static_cast<uint32_t>(iface), &addresses);
68 ASSERT_TRUE(addresses.size() >= 1U);
69 // Verify that the addresses are valid.
70 for (size_t i = 0; i < addresses.size(); ++i) {
71 PP_NetAddress_Family family = addresses[i].GetFamily();
73 switch (family) {
74 case PP_NETADDRESS_FAMILY_IPV4: {
75 PP_NetAddress_IPv4 ipv4;
76 ASSERT_TRUE(addresses[i].DescribeAsIPv4Address(&ipv4));
78 // Verify that the address is not zero.
79 bool all_zeros = true;
80 for (size_t j = 0; j < sizeof(ipv4.addr); ++j) {
81 if (ipv4.addr[j] != 0) {
82 all_zeros = false;
83 break;
86 ASSERT_TRUE(!all_zeros);
88 // Verify that port is set to 0.
89 ASSERT_TRUE(ipv4.port == 0);
90 break;
93 case PP_NETADDRESS_FAMILY_IPV6: {
94 PP_NetAddress_IPv6 ipv6;
95 ASSERT_TRUE(addresses[i].DescribeAsIPv6Address(&ipv6));
97 // Verify that the address is not zero.
98 bool all_zeros = true;
99 for (size_t j = 0; j < sizeof(ipv6.addr); ++j) {
100 if (ipv6.addr[j] != 0) {
101 all_zeros = false;
102 break;
105 ASSERT_TRUE(!all_zeros);
107 // Verify that port is set to 0.
108 ASSERT_TRUE(ipv6.port == 0);
109 break;
112 default:
113 ASSERT_TRUE(false);
117 // Verify that each interface has a unique name and a display name.
118 ASSERT_FALSE(network_list.GetName(static_cast<uint32_t>(iface)).empty());
119 ASSERT_FALSE(network_list.GetDisplayName(
120 static_cast<uint32_t>(iface)).empty());
122 PP_NetworkList_Type type =
123 network_list.GetType(static_cast<uint32_t>(iface));
124 ASSERT_TRUE(type >= PP_NETWORKLIST_TYPE_UNKNOWN);
125 ASSERT_TRUE(type <= PP_NETWORKLIST_TYPE_CELLULAR);
127 PP_NetworkList_State state =
128 network_list.GetState(static_cast<uint32_t>(iface));
129 ASSERT_TRUE(state >= PP_NETWORKLIST_STATE_DOWN);
130 ASSERT_TRUE(state <= PP_NETWORKLIST_STATE_UP);
133 PASS();
136 std::string TestNetworkMonitor::TestBasic() {
137 TestCompletionCallbackWithOutput<pp::NetworkList> test_callback(
138 instance_->pp_instance());
139 pp::NetworkMonitor network_monitor(instance_);
140 test_callback.WaitForResult(
141 network_monitor.UpdateNetworkList(test_callback.GetCallback()));
143 ASSERT_EQ(PP_OK, test_callback.result());
144 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback.output()));
146 PASS();
149 std::string TestNetworkMonitor::Test2Monitors() {
150 TestCompletionCallbackWithOutput<pp::NetworkList> test_callback(
151 instance_->pp_instance());
152 pp::NetworkMonitor network_monitor(instance_);
153 test_callback.WaitForResult(
154 network_monitor.UpdateNetworkList(test_callback.GetCallback()));
156 ASSERT_EQ(PP_OK, test_callback.result());
157 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback.output()));
159 TestCompletionCallbackWithOutput<pp::NetworkList> test_callback_2(
160 instance_->pp_instance());
161 pp::NetworkMonitor network_monitor_2(instance_);
162 test_callback_2.WaitForResult(
163 network_monitor_2.UpdateNetworkList(test_callback_2.GetCallback()));
165 ASSERT_EQ(PP_OK, test_callback_2.result());
166 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback_2.output()));
168 PASS();
171 std::string TestNetworkMonitor::TestDeleteInCallback() {
172 pp::NetworkMonitor* network_monitor =
173 new pp::NetworkMonitor(instance_);
174 MonitorDeletionCallbackDelegate deletion_delegate(network_monitor);
175 TestCompletionCallbackWithOutput<pp::NetworkList> test_callback(
176 instance_->pp_instance());
177 test_callback.SetDelegate(&deletion_delegate);
178 test_callback.WaitForResult(
179 network_monitor->UpdateNetworkList(test_callback.GetCallback()));
181 ASSERT_EQ(PP_OK, test_callback.result());
182 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback.output()));
184 PASS();