OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/glue/plugins/test/plugin_schedule_timer_test.h" | |
6 #include "webkit/glue/plugins/test/plugin_client.h" | |
7 | |
8 using base::Time; | |
9 | |
10 namespace NPAPIClient { | |
11 | |
12 // The times below are accurate but they are not tested against because it | |
13 // might make the test flakey. | |
14 ScheduleTimerTest::Event | |
15 ScheduleTimerTest::schedule_[ScheduleTimerTest::kNumEvents] = { | |
16 { 0, -1, 0, 100, false, -1 }, // schedule 0 100ms no-repeat | |
17 { 100, 0, 0, 200, false, -1 }, // schedule 0 200ms no-repeat | |
18 { 300, 0, 0, 100, true, -1 }, // schedule 0 100ms repeat | |
19 { 400, 0, 1, 50, true, -1 }, // schedule 1 50ms repeat | |
20 { 450, 1, -1, 0, true, -1 }, // receive 1 repeating | |
21 { 500, 0, -1, 0, true, -1 }, // receive 0 repeating | |
22 { 500, 1, -1, 0, true, -1 }, // receive 1 repeating | |
23 { 550, 1, -1, 0, true, -1 }, // receive 1 repeating | |
24 { 600, 0, -1, 0, true, 0 }, // receive 0 repeating and unschedule | |
25 { 600, 1, 2, 400, true, 1 }, // receive 1 repeating and unschedule | |
26 { 1000, 2, -1, 0, true, 2 }, // receive final and unschedule | |
27 }; | |
28 | |
29 ScheduleTimerTest::ScheduleTimerTest( | |
30 NPP id, NPNetscapeFuncs *host_functions) | |
31 : PluginTest(id, host_functions), | |
32 num_received_events_(0) { | |
33 for (int i = 0; i < kNumTimers; ++i) { | |
34 timer_ids_[i] = 0; | |
35 } | |
36 for (int i = 0; i < kNumEvents; ++i) { | |
37 received_events_[i] = false; | |
38 } | |
39 } | |
40 | |
41 NPError ScheduleTimerTest::New( | |
42 uint16 mode, int16 argc, const char* argn[], const char* argv[], | |
43 NPSavedData* saved) { | |
44 NPError error = PluginTest::New(mode, argc, argn, argv, saved); | |
45 if (error != NPERR_NO_ERROR) | |
46 return error; | |
47 | |
48 start_time_ = Time::Now(); | |
49 HandleEvent(0); | |
50 | |
51 return NPERR_NO_ERROR; | |
52 } | |
53 | |
54 void ScheduleTimerTest::OnTimer(uint32 timer_id) { | |
55 Time current_time = Time::Now(); | |
56 int relative_time = static_cast<int>( | |
57 (current_time - start_time_).InMilliseconds()); | |
58 | |
59 // See if there is a matching unreceived event. | |
60 int event_index = FindUnreceivedEvent(relative_time, timer_id); | |
61 if (event_index < 0) { | |
62 SetError("Received unexpected timer event"); | |
63 SignalTestCompleted(); | |
64 return; | |
65 } | |
66 | |
67 HandleEvent(event_index); | |
68 | |
69 // Finish test if all events have happened. | |
70 if (num_received_events_ == kNumEvents) | |
71 SignalTestCompleted(); | |
72 } | |
73 | |
74 int ScheduleTimerTest::FindUnreceivedEvent(int time, uint32 timer_id) { | |
75 for (int i = 0; i < kNumEvents; ++i) { | |
76 const Event& event = schedule_[i]; | |
77 if (!received_events_[i] && | |
78 timer_ids_[event.received_index] == timer_id) { | |
79 return i; | |
80 } | |
81 } | |
82 return -1; | |
83 } | |
84 | |
85 namespace { | |
86 void OnTimerHelper(NPP id, uint32 timer_id) { | |
87 ScheduleTimerTest* plugin_object = | |
88 static_cast<ScheduleTimerTest*>(id->pdata); | |
89 if (plugin_object) { | |
90 plugin_object->OnTimer(timer_id); | |
91 } | |
92 } | |
93 } | |
94 | |
95 void ScheduleTimerTest::HandleEvent(int event_index) { | |
96 const Event& event = schedule_[event_index]; | |
97 | |
98 // Mark event as received. | |
99 DCHECK(!received_events_[event_index]); | |
100 received_events_[event_index] = true; | |
101 ++num_received_events_; | |
102 | |
103 // Unschedule timer if present. | |
104 if (event.unscheduled_index >= 0) { | |
105 HostFunctions()->unscheduletimer( | |
106 id(), timer_ids_[event.unscheduled_index]); | |
107 } | |
108 | |
109 // Schedule timer if present. | |
110 if (event.scheduled_index >= 0) { | |
111 timer_ids_[event.scheduled_index] = HostFunctions()->scheduletimer( | |
112 id(), event.scheduled_interval, event.schedule_repeated, OnTimerHelper); | |
113 } | |
114 } | |
115 | |
116 } // namespace NPAPIClient | |
OLD | NEW |