Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ppapi/tests/test_network_monitor_private.h" | 5 #include "ppapi/tests/test_network_monitor_private.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "ppapi/cpp/private/net_address_private.h" | |
| 9 #include "ppapi/cpp/private/network_list_private.h" | 10 #include "ppapi/cpp/private/network_list_private.h" |
| 10 #include "ppapi/cpp/private/network_monitor_private.h" | 11 #include "ppapi/cpp/private/network_monitor_private.h" |
| 11 #include "ppapi/tests/testing_instance.h" | 12 #include "ppapi/tests/testing_instance.h" |
| 12 #include "ppapi/tests/test_utils.h" | 13 #include "ppapi/tests/test_utils.h" |
| 13 #include "ppapi/cpp/module.h" | 14 #include "ppapi/cpp/module.h" |
| 14 | 15 |
| 15 REGISTER_TEST_CASE(NetworkMonitorPrivate); | 16 REGISTER_TEST_CASE(NetworkMonitorPrivate); |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 struct CallbackData { | 20 struct CallbackData { |
| 20 explicit CallbackData(PP_Instance instance) | 21 explicit CallbackData(PP_Instance instance) |
| 21 : call_counter(0), | 22 : call_counter(0), |
| 22 completion_callback(instance), | 23 completion_callback(instance), |
| 23 list_resource(0) { | 24 list_resource(0), |
| 25 delete_monitor(false), | |
| 26 monitor(NULL) { | |
| 24 } | 27 } |
| 25 ~CallbackData() { | 28 ~CallbackData() { |
| 26 if (list_resource) | 29 if (list_resource) |
| 27 pp::Module::Get()->core()->ReleaseResource(list_resource); | 30 pp::Module::Get()->core()->ReleaseResource(list_resource); |
| 28 } | 31 } |
| 29 int call_counter; | 32 int call_counter; |
| 30 TestCompletionCallback completion_callback; | 33 TestCompletionCallback completion_callback; |
| 31 PP_Resource list_resource; | 34 PP_Resource list_resource; |
| 35 bool delete_monitor; | |
| 36 pp::NetworkMonitorPrivate* monitor; | |
| 32 }; | 37 }; |
| 33 | 38 |
| 34 void TestCallback(void* user_data, PP_Resource network_list) { | 39 void TestCallback(void* user_data, PP_Resource network_list) { |
| 35 CallbackData* data = static_cast<CallbackData*>(user_data); | 40 CallbackData* data = static_cast<CallbackData*>(user_data); |
| 36 data->call_counter++; | 41 data->call_counter++; |
| 37 | 42 |
| 38 if (data->list_resource) | 43 if (data->list_resource) |
| 39 pp::Module::Get()->core()->ReleaseResource(data->list_resource); | 44 pp::Module::Get()->core()->ReleaseResource(data->list_resource); |
| 40 data->list_resource = network_list; | 45 data->list_resource = network_list; |
| 41 | 46 |
| 47 if (data->delete_monitor) | |
| 48 delete data->monitor; | |
| 49 | |
| 42 // Invoke completion callback only for the first change notification. | 50 // Invoke completion callback only for the first change notification. |
| 43 if (data->call_counter == 1) | 51 if (data->call_counter == 1) |
| 44 static_cast<pp::CompletionCallback>(data->completion_callback).Run(PP_OK); | 52 static_cast<pp::CompletionCallback>(data->completion_callback).Run(PP_OK); |
| 45 } | 53 } |
| 46 | 54 |
| 47 } // namespace | 55 } // namespace |
| 48 | 56 |
| 49 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) | 57 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) |
| 50 : TestCase(instance) { | 58 : TestCase(instance) { |
| 51 } | 59 } |
| 52 | 60 |
| 53 bool TestNetworkMonitorPrivate::Init() { | 61 bool TestNetworkMonitorPrivate::Init() { |
| 54 if (!pp::NetworkMonitorPrivate::IsAvailable()) | 62 if (!pp::NetworkMonitorPrivate::IsAvailable()) |
| 55 return false; | 63 return false; |
| 56 | 64 |
| 57 return true; | 65 return true; |
| 58 } | 66 } |
| 59 | 67 |
| 60 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { | 68 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { |
| 61 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); | 69 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); |
| 62 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); | 70 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); |
| 71 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback, filter); | |
| 63 } | 72 } |
| 64 | 73 |
| 65 std::string TestNetworkMonitorPrivate::VerifyNetworkList( | 74 std::string TestNetworkMonitorPrivate::VerifyNetworkList( |
| 66 PP_Resource network_resource) { | 75 PP_Resource network_resource) { |
| 67 pp::NetworkListPrivate network_list(network_resource); | 76 pp::NetworkListPrivate network_list(network_resource); |
| 68 | 77 |
| 69 // Verify that there is at least one network interface. | 78 // Verify that there is at least one network interface. |
| 70 size_t count = network_list.GetCount(); | 79 size_t count = network_list.GetCount(); |
| 71 ASSERT_TRUE(count >= 1U); | 80 ASSERT_TRUE(count >= 1U); |
| 72 | 81 |
| 73 // Iterate over all interfaces and verify their properties. | 82 // Iterate over all interfaces and verify their properties. |
| 74 for (size_t iface = 0; iface < count; ++iface) { | 83 for (size_t iface = 0; iface < count; ++iface) { |
| 75 // Verify that the first interface has at least one address. | 84 // Verify that the first interface has at least one address. |
| 76 std::vector<PP_NetAddress_Private> addresses; | 85 std::vector<PP_NetAddress_Private> addresses; |
| 77 network_list.GetIpAddresses(iface, &addresses); | 86 network_list.GetIpAddresses(iface, &addresses); |
| 78 ASSERT_TRUE(addresses.size() >= 1U); | 87 ASSERT_TRUE(addresses.size() >= 1U); |
| 79 // Verify that the addresses are valid. | 88 // Verify that the addresses are valid. |
| 80 for (size_t i = 0; i < addresses.size(); ++i) { | 89 for (size_t i = 0; i < addresses.size(); ++i) { |
| 81 ASSERT_TRUE(addresses[i].size == 4 || addresses[i].size == 16); | 90 PP_NetAddressFamily_Private family = |
| 91 pp::NetAddressPrivate::GetFamily(addresses[i]); | |
| 92 | |
| 93 ASSERT_TRUE(family == PP_NETADDRESSFAMILY_IPV4 || | |
| 94 family == PP_NETADDRESSFAMILY_IPV6); | |
| 95 | |
| 96 char ip[16] = { 0 }; | |
| 97 ASSERT_TRUE(pp::NetAddressPrivate::GetAddress( | |
| 98 addresses[i], ip, sizeof(ip))); | |
| 82 | 99 |
| 83 // Verify that the address is not zero. | 100 // Verify that the address is not zero. |
| 84 size_t j; | 101 size_t j; |
| 85 for (j = 0; j < addresses[i].size; ++j) { | 102 for (j = 0; j < sizeof(ip[i]); ++j) { |
|
dmichael (off chromium)
2012/03/16 19:55:22
I think you mean sizeof(ip)?
Sergey Ulanov
2012/03/16 23:37:28
Yes, thanks for catching it!
| |
| 86 if (addresses[i].data[j] != 0) | 103 if (ip[j] != 0) |
| 87 break; | 104 break; |
| 88 } | 105 } |
| 89 ASSERT_TRUE(j != addresses[i].size); | 106 ASSERT_TRUE(j != addresses[i].size); |
| 107 | |
| 108 // Verify that port is set to 0. | |
| 109 ASSERT_TRUE(pp::NetAddressPrivate::GetPort(addresses[i]) == 0); | |
| 90 } | 110 } |
| 91 | 111 |
| 92 // Verify that each interface has a unique name and a display name. | 112 // Verify that each interface has a unique name and a display name. |
| 93 ASSERT_FALSE(network_list.GetName(iface).empty()); | 113 ASSERT_FALSE(network_list.GetName(iface).empty()); |
| 94 ASSERT_FALSE(network_list.GetDisplayName(iface).empty()); | 114 ASSERT_FALSE(network_list.GetDisplayName(iface).empty()); |
| 95 | 115 |
| 96 PP_NetworkListType_Private type = network_list.GetType(iface); | 116 PP_NetworkListType_Private type = network_list.GetType(iface); |
| 97 ASSERT_TRUE(type >= PP_NETWORKLIST_UNKNOWN); | 117 ASSERT_TRUE(type >= PP_NETWORKLIST_UNKNOWN); |
| 98 ASSERT_TRUE(type <= PP_NETWORKLIST_CELLULAR); | 118 ASSERT_TRUE(type <= PP_NETWORKLIST_CELLULAR); |
| 99 | 119 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 | 179 |
| 160 pp::NetworkMonitorPrivate network_monitor_2( | 180 pp::NetworkMonitorPrivate network_monitor_2( |
| 161 instance_, &TestCallback, reinterpret_cast<void*>(&callback_data_2)); | 181 instance_, &TestCallback, reinterpret_cast<void*>(&callback_data_2)); |
| 162 ASSERT_EQ(callback_data_2.completion_callback.WaitForResult(), PP_OK); | 182 ASSERT_EQ(callback_data_2.completion_callback.WaitForResult(), PP_OK); |
| 163 ASSERT_EQ(callback_data_2.call_counter, 1); | 183 ASSERT_EQ(callback_data_2.call_counter, 1); |
| 164 | 184 |
| 165 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data_2.list_resource)); | 185 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data_2.list_resource)); |
| 166 | 186 |
| 167 PASS(); | 187 PASS(); |
| 168 } | 188 } |
| 189 | |
| 190 std::string TestNetworkMonitorPrivate::TestDeleteInCallback() { | |
| 191 CallbackData callback_data(instance_->pp_instance()); | |
| 192 | |
| 193 pp::NetworkMonitorPrivate* network_monitor = new pp::NetworkMonitorPrivate( | |
| 194 instance_, &TestCallback, reinterpret_cast<void*>(&callback_data)); | |
|
dmichael (off chromium)
2012/03/16 19:55:22
is the reinterpret_cast necessary?
Sergey Ulanov
2012/03/16 23:37:28
Done.
dmichael (off chromium)
2012/03/19 16:25:08
nit: missed this one
Sergey Ulanov
2012/03/19 20:43:29
Done.
| |
| 195 callback_data.delete_monitor = true; | |
| 196 callback_data.monitor = network_monitor; | |
| 197 | |
| 198 ASSERT_EQ(callback_data.completion_callback.WaitForResult(), PP_OK); | |
| 199 ASSERT_EQ(callback_data.call_counter, 1); | |
| 200 | |
| 201 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data.list_resource)); | |
| 202 | |
| 203 PASS(); | |
| 204 } | |
| OLD | NEW |