OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/plugins/npapi/test/plugin_schedule_timer_test.h" | 5 #include "webkit/plugins/npapi/test/plugin_schedule_timer_test.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "webkit/plugins/npapi/test/plugin_client.h" | 8 #include "webkit/plugins/npapi/test/plugin_client.h" |
9 | 9 |
10 using base::Time; | 10 using base::Time; |
(...skipping 30 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 NPError ScheduleTimerTest::New( | 43 NPError ScheduleTimerTest::New( |
44 uint16 mode, int16 argc, const char* argn[], const char* argv[], | 44 uint16 mode, int16 argc, const char* argn[], const char* argv[], |
45 NPSavedData* saved) { | 45 NPSavedData* saved) { |
46 NPError error = PluginTest::New(mode, argc, argn, argv, saved); | 46 NPError error = PluginTest::New(mode, argc, argn, argv, saved); |
47 if (error != NPERR_NO_ERROR) | 47 if (error != NPERR_NO_ERROR) |
48 return error; | 48 return error; |
49 | 49 |
50 start_time_ = Time::Now(); | 50 start_time_ = Time::Now(); |
51 HandleEvent(0); | 51 HandleEventIndex(0); |
52 | 52 |
53 return NPERR_NO_ERROR; | 53 return NPERR_NO_ERROR; |
54 } | 54 } |
55 | 55 |
56 void ScheduleTimerTest::OnTimer(uint32 timer_id) { | 56 void ScheduleTimerTest::OnTimer(uint32 timer_id) { |
57 Time current_time = Time::Now(); | 57 Time current_time = Time::Now(); |
58 int relative_time = static_cast<int>( | 58 int relative_time = static_cast<int>( |
59 (current_time - start_time_).InMilliseconds()); | 59 (current_time - start_time_).InMilliseconds()); |
60 | 60 |
61 // See if there is a matching unreceived event. | 61 // See if there is a matching unreceived event. |
62 int event_index = FindUnreceivedEvent(relative_time, timer_id); | 62 int event_index = FindUnreceivedEvent(relative_time, timer_id); |
63 if (event_index < 0) { | 63 if (event_index < 0) { |
64 SetError("Received unexpected timer event"); | 64 SetError("Received unexpected timer event"); |
65 SignalTestCompleted(); | 65 SignalTestCompleted(); |
66 return; | 66 return; |
67 } | 67 } |
68 | 68 |
69 HandleEvent(event_index); | 69 HandleEventIndex(event_index); |
70 | 70 |
71 // Finish test if all events have happened. | 71 // Finish test if all events have happened. |
72 if (num_received_events_ == kNumEvents) | 72 if (num_received_events_ == kNumEvents) |
73 SignalTestCompleted(); | 73 SignalTestCompleted(); |
74 } | 74 } |
75 | 75 |
76 int ScheduleTimerTest::FindUnreceivedEvent(int time, uint32 timer_id) { | 76 int ScheduleTimerTest::FindUnreceivedEvent(int time, uint32 timer_id) { |
77 for (int i = 0; i < kNumEvents; ++i) { | 77 for (int i = 0; i < kNumEvents; ++i) { |
78 const Event& event = schedule_[i]; | 78 const Event& event = schedule_[i]; |
79 if (!received_events_[i] && | 79 if (!received_events_[i] && |
80 timer_ids_[event.received_index] == timer_id) { | 80 timer_ids_[event.received_index] == timer_id) { |
81 return i; | 81 return i; |
82 } | 82 } |
83 } | 83 } |
84 return -1; | 84 return -1; |
85 } | 85 } |
86 | 86 |
87 namespace { | 87 namespace { |
88 void OnTimerHelper(NPP id, uint32 timer_id) { | 88 void OnTimerHelper(NPP id, uint32 timer_id) { |
89 ScheduleTimerTest* plugin_object = | 89 ScheduleTimerTest* plugin_object = |
90 static_cast<ScheduleTimerTest*>(id->pdata); | 90 static_cast<ScheduleTimerTest*>(id->pdata); |
91 if (plugin_object) { | 91 if (plugin_object) { |
92 plugin_object->OnTimer(timer_id); | 92 plugin_object->OnTimer(timer_id); |
93 } | 93 } |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 void ScheduleTimerTest::HandleEvent(int event_index) { | 97 void ScheduleTimerTest::HandleEventIndex(int event_index) { |
98 const Event& event = schedule_[event_index]; | 98 const Event& event = schedule_[event_index]; |
99 | 99 |
100 // Mark event as received. | 100 // Mark event as received. |
101 DCHECK(!received_events_[event_index]); | 101 DCHECK(!received_events_[event_index]); |
102 received_events_[event_index] = true; | 102 received_events_[event_index] = true; |
103 ++num_received_events_; | 103 ++num_received_events_; |
104 | 104 |
105 // Unschedule timer if present. | 105 // Unschedule timer if present. |
106 if (event.unscheduled_index >= 0) { | 106 if (event.unscheduled_index >= 0) { |
107 HostFunctions()->unscheduletimer( | 107 HostFunctions()->unscheduletimer( |
108 id(), timer_ids_[event.unscheduled_index]); | 108 id(), timer_ids_[event.unscheduled_index]); |
109 } | 109 } |
110 | 110 |
111 // Schedule timer if present. | 111 // Schedule timer if present. |
112 if (event.scheduled_index >= 0) { | 112 if (event.scheduled_index >= 0) { |
113 timer_ids_[event.scheduled_index] = HostFunctions()->scheduletimer( | 113 timer_ids_[event.scheduled_index] = HostFunctions()->scheduletimer( |
114 id(), event.scheduled_interval, event.schedule_repeated, OnTimerHelper); | 114 id(), event.scheduled_interval, event.schedule_repeated, OnTimerHelper); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 } // namespace NPAPIClient | 118 } // namespace NPAPIClient |
OLD | NEW |