| 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/completion_callback.h" |
| 9 #include "ppapi/cpp/instance_handle.h" | 10 #include "ppapi/cpp/instance_handle.h" |
| 10 #include "ppapi/cpp/module.h" | 11 #include "ppapi/cpp/module.h" |
| 11 #include "ppapi/cpp/net_address.h" | 12 #include "ppapi/cpp/net_address.h" |
| 12 #include "ppapi/cpp/private/network_list_private.h" | 13 #include "ppapi/cpp/private/network_list_private.h" |
| 13 #include "ppapi/cpp/private/network_monitor_private.h" | 14 #include "ppapi/cpp/private/network_monitor_private.h" |
| 14 #include "ppapi/tests/testing_instance.h" | 15 #include "ppapi/tests/testing_instance.h" |
| 15 #include "ppapi/tests/test_utils.h" | 16 #include "ppapi/tests/test_utils.h" |
| 16 #include "ppapi/utility/private/network_list_observer_private.h" | |
| 17 | 17 |
| 18 REGISTER_TEST_CASE(NetworkMonitorPrivate); | 18 REGISTER_TEST_CASE(NetworkMonitorPrivate); |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 struct CallbackData { | 22 class MonitorDeletionCallbackDelegate |
| 23 explicit CallbackData(PP_Instance instance) | 23 : public TestCompletionCallback::Delegate { |
| 24 : event(instance), | |
| 25 call_counter(0), | |
| 26 delete_monitor(false), | |
| 27 monitor(NULL) { | |
| 28 } | |
| 29 ~CallbackData() { | |
| 30 } | |
| 31 NestedEvent event; | |
| 32 int call_counter; | |
| 33 pp::NetworkListPrivate network_list; | |
| 34 bool delete_monitor; | |
| 35 pp::NetworkMonitorPrivate* monitor; | |
| 36 }; | |
| 37 | |
| 38 void TestCallback(void* user_data, PP_Resource pp_network_list) { | |
| 39 CallbackData* data = static_cast<CallbackData*>(user_data); | |
| 40 data->call_counter++; | |
| 41 | |
| 42 data->network_list = pp::NetworkListPrivate(pp::PASS_REF, pp_network_list); | |
| 43 | |
| 44 if (data->delete_monitor) | |
| 45 delete data->monitor; | |
| 46 | |
| 47 if (data->call_counter == 1) | |
| 48 data->event.Signal(); | |
| 49 } | |
| 50 | |
| 51 class TestNetworkListObserver : public pp::NetworkListObserverPrivate { | |
| 52 public: | 24 public: |
| 53 explicit TestNetworkListObserver(const pp::InstanceHandle& instance) | 25 explicit MonitorDeletionCallbackDelegate(pp::NetworkMonitorPrivate* monitor) |
| 54 : pp::NetworkListObserverPrivate(instance), | 26 : monitor_(monitor) { |
| 55 event(instance.pp_instance()) { | |
| 56 } | |
| 57 virtual void OnNetworkListChanged(const pp::NetworkListPrivate& list) { | |
| 58 current_list = list; | |
| 59 event.Signal(); | |
| 60 } | 27 } |
| 61 | 28 |
| 62 pp::NetworkListPrivate current_list; | 29 // TestCompletionCallback::Delegate interface. |
| 63 NestedEvent event; | 30 virtual void OnCallback(void* user_data, int32_t result) { |
| 31 delete monitor_; |
| 32 } |
| 33 |
| 34 private: |
| 35 pp::NetworkMonitorPrivate* monitor_; |
| 64 }; | 36 }; |
| 65 | 37 |
| 66 } // namespace | 38 } // namespace |
| 67 | 39 |
| 68 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) | 40 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) |
| 69 : TestCase(instance) { | 41 : TestCase(instance) { |
| 70 } | 42 } |
| 71 | 43 |
| 72 bool TestNetworkMonitorPrivate::Init() { | 44 bool TestNetworkMonitorPrivate::Init() { |
| 73 if (!pp::NetworkMonitorPrivate::IsAvailable()) | 45 if (!pp::NetworkMonitorPrivate::IsAvailable()) |
| 74 return false; | 46 return false; |
| 75 | 47 |
| 76 return CheckTestingInterface(); | 48 return CheckTestingInterface(); |
| 77 } | 49 } |
| 78 | 50 |
| 79 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { | 51 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { |
| 80 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); | 52 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); |
| 81 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); | 53 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); |
| 82 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback, filter); | 54 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback, filter); |
| 83 RUN_TEST_FORCEASYNC_AND_NOT(ListObserver, filter); | |
| 84 } | 55 } |
| 85 | 56 |
| 86 std::string TestNetworkMonitorPrivate::VerifyNetworkList( | 57 std::string TestNetworkMonitorPrivate::VerifyNetworkList( |
| 87 const pp::NetworkListPrivate& network_list) { | 58 const pp::NetworkListPrivate& network_list) { |
| 88 // Verify that there is at least one network interface. | 59 // Verify that there is at least one network interface. |
| 89 size_t count = network_list.GetCount(); | 60 size_t count = network_list.GetCount(); |
| 90 ASSERT_TRUE(count >= 1U); | 61 ASSERT_TRUE(count >= 1U); |
| 91 | 62 |
| 92 // Iterate over all interfaces and verify their properties. | 63 // Iterate over all interfaces and verify their properties. |
| 93 for (size_t iface = 0; iface < count; ++iface) { | 64 for (size_t iface = 0; iface < count; ++iface) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 124 |
| 154 PP_NetworkListState_Private state = network_list.GetState(iface); | 125 PP_NetworkListState_Private state = network_list.GetState(iface); |
| 155 ASSERT_TRUE(state >= PP_NETWORKLIST_DOWN); | 126 ASSERT_TRUE(state >= PP_NETWORKLIST_DOWN); |
| 156 ASSERT_TRUE(state <= PP_NETWORKLIST_UP); | 127 ASSERT_TRUE(state <= PP_NETWORKLIST_UP); |
| 157 } | 128 } |
| 158 | 129 |
| 159 PASS(); | 130 PASS(); |
| 160 } | 131 } |
| 161 | 132 |
| 162 std::string TestNetworkMonitorPrivate::TestBasic() { | 133 std::string TestNetworkMonitorPrivate::TestBasic() { |
| 163 CallbackData callback_data(instance_->pp_instance()); | 134 TestCompletionCallbackWithOutput<pp::NetworkListPrivate> test_callback( |
| 135 instance_->pp_instance()); |
| 136 pp::NetworkMonitorPrivate network_monitor(instance_); |
| 137 test_callback.WaitForResult( |
| 138 network_monitor.UpdateNetworkList(test_callback.GetCallback())); |
| 164 | 139 |
| 165 pp::NetworkMonitorPrivate network_monitor( | 140 ASSERT_EQ(test_callback.result(), PP_OK); |
| 166 instance_, &TestCallback, &callback_data); | 141 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback.output())); |
| 167 callback_data.event.Wait(); | |
| 168 ASSERT_EQ(callback_data.call_counter, 1); | |
| 169 | |
| 170 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data.network_list)); | |
| 171 | 142 |
| 172 PASS(); | 143 PASS(); |
| 173 } | 144 } |
| 174 | 145 |
| 175 std::string TestNetworkMonitorPrivate::Test2Monitors() { | 146 std::string TestNetworkMonitorPrivate::Test2Monitors() { |
| 176 CallbackData callback_data(instance_->pp_instance()); | 147 TestCompletionCallbackWithOutput<pp::NetworkListPrivate> test_callback( |
| 148 instance_->pp_instance()); |
| 149 pp::NetworkMonitorPrivate network_monitor(instance_); |
| 150 test_callback.WaitForResult( |
| 151 network_monitor.UpdateNetworkList(test_callback.GetCallback())); |
| 177 | 152 |
| 178 pp::NetworkMonitorPrivate network_monitor( | 153 ASSERT_EQ(test_callback.result(), PP_OK); |
| 179 instance_, &TestCallback, &callback_data); | 154 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback.output())); |
| 180 callback_data.event.Wait(); | |
| 181 ASSERT_EQ(callback_data.call_counter, 1); | |
| 182 | 155 |
| 183 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data.network_list)); | 156 TestCompletionCallbackWithOutput<pp::NetworkListPrivate> test_callback_2( |
| 157 instance_->pp_instance()); |
| 158 pp::NetworkMonitorPrivate network_monitor_2(instance_); |
| 159 test_callback_2.WaitForResult( |
| 160 network_monitor_2.UpdateNetworkList(test_callback_2.GetCallback())); |
| 184 | 161 |
| 185 CallbackData callback_data_2(instance_->pp_instance()); | 162 ASSERT_EQ(test_callback_2.result(), PP_OK); |
| 186 | 163 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback_2.output())); |
| 187 pp::NetworkMonitorPrivate network_monitor_2( | |
| 188 instance_, &TestCallback, &callback_data_2); | |
| 189 callback_data_2.event.Wait(); | |
| 190 ASSERT_EQ(callback_data_2.call_counter, 1); | |
| 191 | |
| 192 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data_2.network_list)); | |
| 193 | 164 |
| 194 PASS(); | 165 PASS(); |
| 195 } | 166 } |
| 196 | 167 |
| 197 std::string TestNetworkMonitorPrivate::TestDeleteInCallback() { | 168 std::string TestNetworkMonitorPrivate::TestDeleteInCallback() { |
| 198 CallbackData callback_data(instance_->pp_instance()); | 169 pp::NetworkMonitorPrivate* network_monitor = |
| 170 new pp::NetworkMonitorPrivate(instance_); |
| 171 MonitorDeletionCallbackDelegate deletion_delegate(network_monitor); |
| 172 TestCompletionCallbackWithOutput<pp::NetworkListPrivate> test_callback( |
| 173 instance_->pp_instance()); |
| 174 test_callback.SetDelegate(&deletion_delegate); |
| 175 test_callback.WaitForResult( |
| 176 network_monitor->UpdateNetworkList(test_callback.GetCallback())); |
| 199 | 177 |
| 200 pp::NetworkMonitorPrivate* network_monitor = new pp::NetworkMonitorPrivate( | 178 ASSERT_EQ(test_callback.result(), PP_OK); |
| 201 instance_, &TestCallback, &callback_data); | 179 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(test_callback.output())); |
| 202 callback_data.delete_monitor = true; | |
| 203 callback_data.monitor = network_monitor; | |
| 204 | |
| 205 callback_data.event.Wait(); | |
| 206 ASSERT_EQ(callback_data.call_counter, 1); | |
| 207 | |
| 208 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data.network_list)); | |
| 209 | 180 |
| 210 PASS(); | 181 PASS(); |
| 211 } | 182 } |
| 212 | |
| 213 std::string TestNetworkMonitorPrivate::TestListObserver() { | |
| 214 TestNetworkListObserver observer(instance_); | |
| 215 observer.event.Wait(); | |
| 216 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(observer.current_list)); | |
| 217 PASS(); | |
| 218 } | |
| OLD | NEW |