Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / test / plugin / plugin_schedule_timer_test.cc
blob950745d0733b472e901df9d10be5923fe28fd99d
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
3 // found in the LICENSE file.
5 #include "content/test/plugin/plugin_schedule_timer_test.h"
7 #include "base/logging.h"
8 #include "content/test/plugin/plugin_client.h"
10 using base::Time;
12 namespace NPAPIClient {
14 // The times below are accurate but they are not tested against because it
15 // might make the test flakey.
16 ScheduleTimerTest::Event
17 ScheduleTimerTest::schedule_[ScheduleTimerTest::kNumEvents] = {
18 { 0, -1, 0, 100, false, -1 }, // schedule 0 100ms no-repeat
19 { 100, 0, 0, 200, false, -1 }, // schedule 0 200ms no-repeat
20 { 300, 0, 0, 100, true, -1 }, // schedule 0 100ms repeat
21 { 400, 0, 1, 50, true, -1 }, // schedule 1 50ms repeat
22 { 450, 1, -1, 0, true, -1 }, // receive 1 repeating
23 { 500, 0, -1, 0, true, -1 }, // receive 0 repeating
24 { 500, 1, -1, 0, true, -1 }, // receive 1 repeating
25 { 550, 1, -1, 0, true, -1 }, // receive 1 repeating
26 { 600, 0, -1, 0, true, 0 }, // receive 0 repeating and unschedule
27 { 600, 1, 2, 400, true, 1 }, // receive 1 repeating and unschedule
28 { 1000, 2, -1, 0, true, 2 }, // receive final and unschedule
31 ScheduleTimerTest::ScheduleTimerTest(
32 NPP id, NPNetscapeFuncs *host_functions)
33 : PluginTest(id, host_functions),
34 num_received_events_(0) {
35 for (int i = 0; i < kNumTimers; ++i) {
36 timer_ids_[i] = 0;
38 for (int i = 0; i < kNumEvents; ++i) {
39 received_events_[i] = false;
43 NPError ScheduleTimerTest::New(
44 uint16 mode, int16 argc, const char* argn[], const char* argv[],
45 NPSavedData* saved) {
46 NPError error = PluginTest::New(mode, argc, argn, argv, saved);
47 if (error != NPERR_NO_ERROR)
48 return error;
50 start_time_ = Time::Now();
51 HandleEventIndex(0);
53 return NPERR_NO_ERROR;
56 void ScheduleTimerTest::OnTimer(uint32 timer_id) {
57 Time current_time = Time::Now();
58 int relative_time = static_cast<int>(
59 (current_time - start_time_).InMilliseconds());
61 // See if there is a matching unreceived event.
62 int event_index = FindUnreceivedEvent(relative_time, timer_id);
63 if (event_index < 0) {
64 SetError("Received unexpected timer event");
65 SignalTestCompleted();
66 return;
69 HandleEventIndex(event_index);
71 // Finish test if all events have happened.
72 if (num_received_events_ == kNumEvents)
73 SignalTestCompleted();
76 int ScheduleTimerTest::FindUnreceivedEvent(int time, uint32 timer_id) {
77 for (int i = 0; i < kNumEvents; ++i) {
78 const Event& event = schedule_[i];
79 if (!received_events_[i] &&
80 timer_ids_[event.received_index] == timer_id) {
81 return i;
84 return -1;
87 namespace {
88 void OnTimerHelper(NPP id, uint32 timer_id) {
89 ScheduleTimerTest* plugin_object =
90 static_cast<ScheduleTimerTest*>(id->pdata);
91 if (plugin_object) {
92 plugin_object->OnTimer(timer_id);
97 void ScheduleTimerTest::HandleEventIndex(int event_index) {
98 const Event& event = schedule_[event_index];
100 // Mark event as received.
101 DCHECK(!received_events_[event_index]);
102 received_events_[event_index] = true;
103 ++num_received_events_;
105 // Unschedule timer if present.
106 if (event.unscheduled_index >= 0) {
107 HostFunctions()->unscheduletimer(
108 id(), timer_ids_[event.unscheduled_index]);
111 // Schedule timer if present.
112 if (event.scheduled_index >= 0) {
113 timer_ids_[event.scheduled_index] = HostFunctions()->scheduletimer(
114 id(), event.scheduled_interval, event.schedule_repeated, OnTimerHelper);
118 } // namespace NPAPIClient