| 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/network_list_private.h" | 9 #include "ppapi/cpp/private/network_list_private.h" |
| 10 #include "ppapi/cpp/private/network_monitor_private.h" | 10 #include "ppapi/cpp/private/network_monitor_private.h" |
| 11 #include "ppapi/tests/testing_instance.h" | 11 #include "ppapi/tests/testing_instance.h" |
| 12 #include "ppapi/tests/test_utils.h" | 12 #include "ppapi/tests/test_utils.h" |
| 13 #include "ppapi/cpp/module.h" | 13 #include "ppapi/cpp/module.h" |
| 14 | 14 |
| 15 REGISTER_TEST_CASE(NetworkMonitorPrivate); | 15 REGISTER_TEST_CASE(NetworkMonitorPrivate); |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 struct CallbackData { | 19 struct CallbackData { |
| 20 explicit CallbackData(PP_Instance instance) | 20 explicit CallbackData(PP_Instance instance) |
| 21 : call_counter(0), | 21 : call_counter(0), |
| 22 completion_callback(instance), | 22 completion_callback(instance), |
| 23 list_resource(0) { | 23 list_resource(0), |
| 24 delete_monitor(false), |
| 25 monitor(NULL) { |
| 24 } | 26 } |
| 25 ~CallbackData() { | 27 ~CallbackData() { |
| 26 if (list_resource) | 28 if (list_resource) |
| 27 pp::Module::Get()->core()->ReleaseResource(list_resource); | 29 pp::Module::Get()->core()->ReleaseResource(list_resource); |
| 28 } | 30 } |
| 29 int call_counter; | 31 int call_counter; |
| 30 TestCompletionCallback completion_callback; | 32 TestCompletionCallback completion_callback; |
| 31 PP_Resource list_resource; | 33 PP_Resource list_resource; |
| 34 bool delete_monitor; |
| 35 pp::NetworkMonitorPrivate* monitor; |
| 32 }; | 36 }; |
| 33 | 37 |
| 34 void TestCallback(void* user_data, PP_Resource network_list) { | 38 void TestCallback(void* user_data, PP_Resource network_list) { |
| 35 CallbackData* data = static_cast<CallbackData*>(user_data); | 39 CallbackData* data = static_cast<CallbackData*>(user_data); |
| 36 data->call_counter++; | 40 data->call_counter++; |
| 37 | 41 |
| 38 if (data->list_resource) | 42 if (data->list_resource) |
| 39 pp::Module::Get()->core()->ReleaseResource(data->list_resource); | 43 pp::Module::Get()->core()->ReleaseResource(data->list_resource); |
| 40 data->list_resource = network_list; | 44 data->list_resource = network_list; |
| 41 | 45 |
| 46 if (data->delete_monitor) |
| 47 delete data->monitor; |
| 48 |
| 42 // Invoke completion callback only for the first change notification. | 49 // Invoke completion callback only for the first change notification. |
| 43 if (data->call_counter == 1) | 50 if (data->call_counter == 1) |
| 44 static_cast<pp::CompletionCallback>(data->completion_callback).Run(PP_OK); | 51 static_cast<pp::CompletionCallback>(data->completion_callback).Run(PP_OK); |
| 45 } | 52 } |
| 46 | 53 |
| 47 } // namespace | 54 } // namespace |
| 48 | 55 |
| 49 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) | 56 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) |
| 50 : TestCase(instance) { | 57 : TestCase(instance) { |
| 51 } | 58 } |
| 52 | 59 |
| 53 bool TestNetworkMonitorPrivate::Init() { | 60 bool TestNetworkMonitorPrivate::Init() { |
| 54 if (!pp::NetworkMonitorPrivate::IsAvailable()) | 61 if (!pp::NetworkMonitorPrivate::IsAvailable()) |
| 55 return false; | 62 return false; |
| 56 | 63 |
| 57 return true; | 64 return true; |
| 58 } | 65 } |
| 59 | 66 |
| 60 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { | 67 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { |
| 61 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); | 68 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); |
| 62 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); | 69 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); |
| 70 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback, filter); |
| 63 } | 71 } |
| 64 | 72 |
| 65 std::string TestNetworkMonitorPrivate::VerifyNetworkList( | 73 std::string TestNetworkMonitorPrivate::VerifyNetworkList( |
| 66 PP_Resource network_resource) { | 74 PP_Resource network_resource) { |
| 67 pp::NetworkListPrivate network_list(network_resource); | 75 pp::NetworkListPrivate network_list(network_resource); |
| 68 | 76 |
| 69 // Verify that there is at least one network interface. | 77 // Verify that there is at least one network interface. |
| 70 size_t count = network_list.GetCount(); | 78 size_t count = network_list.GetCount(); |
| 71 ASSERT_TRUE(count >= 1U); | 79 ASSERT_TRUE(count >= 1U); |
| 72 | 80 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 167 |
| 160 pp::NetworkMonitorPrivate network_monitor_2( | 168 pp::NetworkMonitorPrivate network_monitor_2( |
| 161 instance_, &TestCallback, reinterpret_cast<void*>(&callback_data_2)); | 169 instance_, &TestCallback, reinterpret_cast<void*>(&callback_data_2)); |
| 162 ASSERT_EQ(callback_data_2.completion_callback.WaitForResult(), PP_OK); | 170 ASSERT_EQ(callback_data_2.completion_callback.WaitForResult(), PP_OK); |
| 163 ASSERT_EQ(callback_data_2.call_counter, 1); | 171 ASSERT_EQ(callback_data_2.call_counter, 1); |
| 164 | 172 |
| 165 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data_2.list_resource)); | 173 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data_2.list_resource)); |
| 166 | 174 |
| 167 PASS(); | 175 PASS(); |
| 168 } | 176 } |
| 177 |
| 178 std::string TestNetworkMonitorPrivate::TestDeleteInCallback() { |
| 179 CallbackData callback_data(instance_->pp_instance()); |
| 180 |
| 181 pp::NetworkMonitorPrivate* network_monitor = new pp::NetworkMonitorPrivate( |
| 182 instance_, &TestCallback, reinterpret_cast<void*>(&callback_data)); |
| 183 callback_data.delete_monitor = true; |
| 184 callback_data.monitor = network_monitor; |
| 185 |
| 186 ASSERT_EQ(callback_data.completion_callback.WaitForResult(), PP_OK); |
| 187 ASSERT_EQ(callback_data.call_counter, 1); |
| 188 |
| 189 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data.list_resource)); |
| 190 |
| 191 PASS(); |
| 192 } |
| OLD | NEW |