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 CallbackHandler { |
23 explicit CallbackData(PP_Instance instance) | 23 public: |
24 : event(instance), | 24 explicit CallbackHandler(PP_Instance instance) |
25 call_counter(0), | 25 : event_(instance), |
26 delete_monitor(false), | 26 call_counter_(0), |
27 monitor(NULL) { | 27 monitor_to_delete_(NULL), |
28 result_(PP_ERROR_FAILED), | |
29 callback_factory_(this) { | |
28 } | 30 } |
29 ~CallbackData() { | 31 ~CallbackHandler() { |
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: | |
53 explicit TestNetworkListObserver(const pp::InstanceHandle& instance) | |
54 : pp::NetworkListObserverPrivate(instance), | |
55 event(instance.pp_instance()) { | |
56 } | |
57 virtual void OnNetworkListChanged(const pp::NetworkListPrivate& list) { | |
58 current_list = list; | |
59 event.Signal(); | |
60 } | 32 } |
61 | 33 |
62 pp::NetworkListPrivate current_list; | 34 void DeleteMonitorWhenCalled(pp::NetworkMonitorPrivate* monitor) { |
63 NestedEvent event; | 35 monitor_to_delete_ = monitor; |
36 } | |
37 | |
38 pp::CompletionCallbackWithOutput<pp::NetworkListPrivate> GetCallback() { | |
39 return callback_factory_.NewCallbackWithOutput( | |
40 &CallbackHandler::OnNetworkList); | |
41 } | |
42 | |
43 void Wait() { | |
44 event_.Wait(); | |
45 } | |
46 | |
47 int call_counter() const { return call_counter_; } | |
48 int result() const { return result_; } | |
49 const pp::NetworkListPrivate& list() const { return list_; } | |
50 | |
51 private: | |
52 void OnNetworkList(int32_t result, const pp::NetworkListPrivate& list) { | |
53 call_counter_++; | |
54 | |
55 result_ = result; | |
56 list_ = list; | |
57 | |
58 if (monitor_to_delete_) { | |
59 delete monitor_to_delete_; | |
60 monitor_to_delete_ = NULL; | |
61 } | |
62 | |
63 if (call_counter_ == 1) | |
64 event_.Signal(); | |
65 } | |
66 | |
67 NestedEvent event_; | |
68 int call_counter_; | |
69 pp::NetworkListPrivate list_; | |
70 pp::NetworkMonitorPrivate* monitor_to_delete_; | |
71 int32_t result_; | |
72 | |
73 pp::CompletionCallbackFactory<CallbackHandler> callback_factory_; | |
64 }; | 74 }; |
65 | 75 |
66 } // namespace | 76 } // namespace |
67 | 77 |
68 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) | 78 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance* instance) |
69 : TestCase(instance) { | 79 : TestCase(instance) { |
70 } | 80 } |
71 | 81 |
72 bool TestNetworkMonitorPrivate::Init() { | 82 bool TestNetworkMonitorPrivate::Init() { |
73 if (!pp::NetworkMonitorPrivate::IsAvailable()) | 83 if (!pp::NetworkMonitorPrivate::IsAvailable()) |
74 return false; | 84 return false; |
75 | 85 |
76 return CheckTestingInterface(); | 86 return CheckTestingInterface(); |
77 } | 87 } |
78 | 88 |
79 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { | 89 void TestNetworkMonitorPrivate::RunTests(const std::string& filter) { |
80 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); | 90 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); |
81 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); | 91 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors, filter); |
82 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback, filter); | 92 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback, filter); |
83 RUN_TEST_FORCEASYNC_AND_NOT(ListObserver, filter); | |
84 } | 93 } |
85 | 94 |
86 std::string TestNetworkMonitorPrivate::VerifyNetworkList( | 95 std::string TestNetworkMonitorPrivate::VerifyNetworkList( |
87 const pp::NetworkListPrivate& network_list) { | 96 const pp::NetworkListPrivate& network_list) { |
88 // Verify that there is at least one network interface. | 97 // Verify that there is at least one network interface. |
89 size_t count = network_list.GetCount(); | 98 size_t count = network_list.GetCount(); |
90 ASSERT_TRUE(count >= 1U); | 99 ASSERT_TRUE(count >= 1U); |
91 | 100 |
92 // Iterate over all interfaces and verify their properties. | 101 // Iterate over all interfaces and verify their properties. |
93 for (size_t iface = 0; iface < count; ++iface) { | 102 for (size_t iface = 0; iface < count; ++iface) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 | 162 |
154 PP_NetworkListState_Private state = network_list.GetState(iface); | 163 PP_NetworkListState_Private state = network_list.GetState(iface); |
155 ASSERT_TRUE(state >= PP_NETWORKLIST_DOWN); | 164 ASSERT_TRUE(state >= PP_NETWORKLIST_DOWN); |
156 ASSERT_TRUE(state <= PP_NETWORKLIST_UP); | 165 ASSERT_TRUE(state <= PP_NETWORKLIST_UP); |
157 } | 166 } |
158 | 167 |
159 PASS(); | 168 PASS(); |
160 } | 169 } |
161 | 170 |
162 std::string TestNetworkMonitorPrivate::TestBasic() { | 171 std::string TestNetworkMonitorPrivate::TestBasic() { |
163 CallbackData callback_data(instance_->pp_instance()); | 172 CallbackHandler callback_handler(instance_->pp_instance()); |
yzshen1
2013/09/04 19:52:01
Please consider using TestCompletionCallbackWithOu
Sergey Ulanov
2013/09/04 22:22:32
Done.
| |
173 pp::NetworkMonitorPrivate network_monitor(instance_); | |
174 network_monitor.UpdateNetworkList(callback_handler.GetCallback()); | |
175 callback_handler.Wait(); | |
164 | 176 |
165 pp::NetworkMonitorPrivate network_monitor( | 177 ASSERT_EQ(callback_handler.call_counter(), 1); |
166 instance_, &TestCallback, &callback_data); | 178 ASSERT_EQ(callback_handler.result(), PP_OK); |
167 callback_data.event.Wait(); | 179 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_handler.list())); |
168 ASSERT_EQ(callback_data.call_counter, 1); | |
169 | |
170 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data.network_list)); | |
171 | 180 |
172 PASS(); | 181 PASS(); |
173 } | 182 } |
174 | 183 |
175 std::string TestNetworkMonitorPrivate::Test2Monitors() { | 184 std::string TestNetworkMonitorPrivate::Test2Monitors() { |
176 CallbackData callback_data(instance_->pp_instance()); | 185 CallbackHandler callback_handler(instance_->pp_instance()); |
186 pp::NetworkMonitorPrivate network_monitor(instance_); | |
187 network_monitor.UpdateNetworkList(callback_handler.GetCallback()); | |
188 callback_handler.Wait(); | |
177 | 189 |
178 pp::NetworkMonitorPrivate network_monitor( | 190 ASSERT_EQ(callback_handler.call_counter(), 1); |
179 instance_, &TestCallback, &callback_data); | 191 ASSERT_EQ(callback_handler.result(), PP_OK); |
180 callback_data.event.Wait(); | 192 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_handler.list())); |
181 ASSERT_EQ(callback_data.call_counter, 1); | |
182 | 193 |
183 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data.network_list)); | 194 CallbackHandler callback_handler_2(instance_->pp_instance()); |
195 pp::NetworkMonitorPrivate network_monitor_2(instance_); | |
196 network_monitor_2.UpdateNetworkList(callback_handler_2.GetCallback()); | |
197 callback_handler_2.Wait(); | |
184 | 198 |
185 CallbackData callback_data_2(instance_->pp_instance()); | 199 ASSERT_EQ(callback_handler_2.call_counter(), 1); |
186 | 200 ASSERT_EQ(callback_handler_2.result(), PP_OK); |
187 pp::NetworkMonitorPrivate network_monitor_2( | 201 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_handler_2.list())); |
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 | 202 |
194 PASS(); | 203 PASS(); |
195 } | 204 } |
196 | 205 |
197 std::string TestNetworkMonitorPrivate::TestDeleteInCallback() { | 206 std::string TestNetworkMonitorPrivate::TestDeleteInCallback() { |
198 CallbackData callback_data(instance_->pp_instance()); | 207 CallbackHandler callback_handler(instance_->pp_instance()); |
208 pp::NetworkMonitorPrivate* network_monitor = | |
209 new pp::NetworkMonitorPrivate(instance_); | |
210 callback_handler.DeleteMonitorWhenCalled(network_monitor); | |
211 network_monitor->UpdateNetworkList(callback_handler.GetCallback()); | |
212 callback_handler.Wait(); | |
199 | 213 |
200 pp::NetworkMonitorPrivate* network_monitor = new pp::NetworkMonitorPrivate( | 214 ASSERT_EQ(callback_handler.call_counter(), 1); |
201 instance_, &TestCallback, &callback_data); | 215 ASSERT_EQ(callback_handler.result(), PP_OK); |
202 callback_data.delete_monitor = true; | 216 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_handler.list())); |
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 | 217 |
210 PASS(); | 218 PASS(); |
211 } | 219 } |
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 |