diff options
author | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-26 21:49:10 +0000 |
---|---|---|
committer | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-26 21:49:10 +0000 |
commit | 04d2c301427de6e7da85546437d286971cff1cff (patch) | |
tree | d75fbf2ff896288b153a1c1427a2648ec59ed948 /webkit/glue/plugins/test | |
parent | 555707ea60a7cb39d7cc956eb67adffef3a09b60 (diff) | |
download | chromium_src-04d2c301427de6e7da85546437d286971cff1cff.zip chromium_src-04d2c301427de6e7da85546437d286971cff1cff.tar.gz chromium_src-04d2c301427de6e7da85546437d286971cff1cff.tar.bz2 |
Implemented NPN_ScheduleTimer and NPN_UnscheduleTimer.
TEST=none
BUG=18020
Review URL: http://codereview.chromium.org/329013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30105 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue/plugins/test')
-rw-r--r-- | webkit/glue/plugins/test/plugin_client.cc | 4 | ||||
-rw-r--r-- | webkit/glue/plugins/test/plugin_schedule_timer_test.cc | 116 | ||||
-rw-r--r-- | webkit/glue/plugins/test/plugin_schedule_timer_test.h | 70 |
3 files changed, 190 insertions, 0 deletions
diff --git a/webkit/glue/plugins/test/plugin_client.cc b/webkit/glue/plugins/test/plugin_client.cc index 1146a6a..3bde2c1 100644 --- a/webkit/glue/plugins/test/plugin_client.cc +++ b/webkit/glue/plugins/test/plugin_client.cc @@ -16,6 +16,7 @@ #include "webkit/glue/plugins/test/plugin_javascript_open_popup.h" #include "webkit/glue/plugins/test/plugin_new_fails_test.h" #include "webkit/glue/plugins/test/plugin_private_test.h" +#include "webkit/glue/plugins/test/plugin_schedule_timer_test.h" #include "webkit/glue/plugins/test/plugin_npobject_lifetime_test.h" #include "webkit/glue/plugins/test/plugin_npobject_proxy_test.h" #include "webkit/glue/plugins/test/plugin_window_size_test.h" @@ -171,6 +172,9 @@ NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, } else if (test_name == "private") { new_test = new NPAPIClient::PrivateTest(instance, NPAPIClient::PluginClient::HostFunctions()); + } else if (test_name == "schedule_timer") { + new_test = new NPAPIClient::ScheduleTimerTest( + instance, NPAPIClient::PluginClient::HostFunctions()); #if defined(OS_WIN) // TODO(port): plugin_windowed_test.*. } else if (test_name == "hidden_plugin" || diff --git a/webkit/glue/plugins/test/plugin_schedule_timer_test.cc b/webkit/glue/plugins/test/plugin_schedule_timer_test.cc new file mode 100644 index 0000000..fbfce34 --- /dev/null +++ b/webkit/glue/plugins/test/plugin_schedule_timer_test.cc @@ -0,0 +1,116 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "webkit/glue/plugins/test/plugin_schedule_timer_test.h" +#include "webkit/glue/plugins/test/plugin_client.h" + +using base::Time; + +namespace NPAPIClient { + +// The times below are accurate but they are not tested against because it +// might make the test flakey. +ScheduleTimerTest::Event + ScheduleTimerTest::schedule_[ScheduleTimerTest::kNumEvents] = { + { 0, -1, 0, 100, false, -1 }, // schedule 0 100ms no-repeat + { 100, 0, 0, 200, false, -1 }, // schedule 0 200ms no-repeat + { 300, 0, 0, 100, true, -1 }, // schedule 0 100ms repeat + { 400, 0, 1, 50, true, -1 }, // schedule 1 50ms repeat + { 450, 1, -1, 0, true, -1 }, // receive 1 repeating + { 500, 0, -1, 0, true, -1 }, // receive 0 repeating + { 500, 1, -1, 0, true, -1 }, // receive 1 repeating + { 550, 1, -1, 0, true, -1 }, // receive 1 repeating + { 600, 0, -1, 0, true, 0 }, // receive 0 repeating and unschedule + { 600, 1, 2, 400, true, 1 }, // receive 1 repeating and unschedule + { 1000, 2, -1, 0, true, 2 }, // receive final and unschedule +}; + +ScheduleTimerTest::ScheduleTimerTest( + NPP id, NPNetscapeFuncs *host_functions) + : PluginTest(id, host_functions), + num_received_events_(0) { + for (int i = 0; i < kNumTimers; ++i) { + timer_ids_[i] = 0; + } + for (int i = 0; i < kNumEvents; ++i) { + received_events_[i] = false; + } +} + +NPError ScheduleTimerTest::New( + uint16 mode, int16 argc, const char* argn[], const char* argv[], + NPSavedData* saved) { + NPError error = PluginTest::New(mode, argc, argn, argv, saved); + if (error != NPERR_NO_ERROR) + return error; + + start_time_ = Time::Now(); + HandleEvent(0); + + return NPERR_NO_ERROR; +} + +void ScheduleTimerTest::OnTimer(uint32 timer_id) { + Time current_time = Time::Now(); + int relative_time = static_cast<int>( + (current_time - start_time_).InMilliseconds()); + + // See if there is a matching unreceived event. + int event_index = FindUnreceivedEvent(relative_time, timer_id); + if (event_index < 0) { + SetError("Received unexpected timer event"); + SignalTestCompleted(); + return; + } + + HandleEvent(event_index); + + // Finish test if all events have happened. + if (num_received_events_ == kNumEvents) + SignalTestCompleted(); +} + +int ScheduleTimerTest::FindUnreceivedEvent(int time, uint32 timer_id) { + for (int i = 0; i < kNumEvents; ++i) { + const Event& event = schedule_[i]; + if (!received_events_[i] && + timer_ids_[event.received_index] == timer_id) { + return i; + } + } + return -1; +} + +namespace { +void OnTimerHelper(NPP id, uint32 timer_id) { + ScheduleTimerTest* plugin_object = + static_cast<ScheduleTimerTest*>(id->pdata); + if (plugin_object) { + plugin_object->OnTimer(timer_id); + } +} +} + +void ScheduleTimerTest::HandleEvent(int event_index) { + const Event& event = schedule_[event_index]; + + // Mark event as received. + DCHECK(!received_events_[event_index]); + received_events_[event_index] = true; + ++num_received_events_; + + // Unschedule timer if present. + if (event.unscheduled_index >= 0) { + HostFunctions()->unscheduletimer( + id(), timer_ids_[event.unscheduled_index]); + } + + // Schedule timer if present. + if (event.scheduled_index >= 0) { + timer_ids_[event.scheduled_index] = HostFunctions()->scheduletimer( + id(), event.scheduled_interval, event.schedule_repeated, OnTimerHelper); + } +} + +} // namespace NPAPIClient diff --git a/webkit/glue/plugins/test/plugin_schedule_timer_test.h b/webkit/glue/plugins/test/plugin_schedule_timer_test.h new file mode 100644 index 0000000..9ca947f --- /dev/null +++ b/webkit/glue/plugins/test/plugin_schedule_timer_test.h @@ -0,0 +1,70 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef WEBKIT_GLUE_PLUGINS_TEST_PLUGIN_SCHEDULE_TIMER_TEST_H +#define WEBKIT_GLUE_PLUGINS_TEST_PLUGIN_SCHEDULE_TIMER_TEST_H + +#include <vector> + +#include "base/at_exit.h" +#include "base/time.h" +#include "webkit/glue/plugins/test/plugin_test.h" + +namespace NPAPIClient { + +// This class tests scheduling and unscheduling of timers using +// NPN_ScheduleTimer and NPN_UnscheduleTimer. +class ScheduleTimerTest : public PluginTest { + public: + ScheduleTimerTest(NPP id, NPNetscapeFuncs *host_functions); + + virtual NPError New(uint16 mode, int16 argc, const char* argn[], + const char* argv[], NPSavedData* saved); + + void OnTimer(uint32 timer_id); + + private: + // base::Time needs one of these. + base::AtExitManager at_exit_manager_; + + // Table mapping timer index (as used in event schedule) to timer id. + static const int kNumTimers = 3; + uint32 timer_ids_[kNumTimers]; + + // Schedule of events for test. + static const int kNumEvents = 11; + struct Event { + int time; + + // The index of the timer that triggered the event or -1 for the first + // event. + int received_index; + + // The index of the timer to schedule on this event or -1. + int scheduled_index; + + // Info about the timer to be scheduled (if any). + uint32 scheduled_interval; + bool schedule_repeated; + + // The index of the timer to unschedule on this event or -1. + int unscheduled_index; + }; + static Event schedule_[kNumEvents]; + int num_received_events_; + + // Set of events that have been received (by index). + bool received_events_[kNumEvents]; + + // Time of initial event. + base::Time start_time_; + + // Returns index of matching unreceived event or -1 if not found. + int FindUnreceivedEvent(int time, uint32 timer_id); + void HandleEvent(int event_index); +}; + +} // namespace NPAPIClient + +#endif // WEBKIT_GLUE_PLUGINS_TEST_PLUGIN_SCHEDULE_TIMER_TEST_H |