Home | History | Annotate | Download | only in plugin
      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.
      4 
      5 #include "content/test/plugin/plugin_schedule_timer_test.h"
      6 
      7 #include "base/logging.h"
      8 #include "content/test/plugin/plugin_client.h"
      9 
     10 using base::Time;
     11 
     12 namespace NPAPIClient {
     13 
     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
     29 };
     30 
     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;
     37   }
     38   for (int i = 0; i < kNumEvents; ++i) {
     39     received_events_[i] = false;
     40   }
     41 }
     42 
     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;
     49 
     50   start_time_ = Time::Now();
     51   HandleEventIndex(0);
     52 
     53   return NPERR_NO_ERROR;
     54 }
     55 
     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());
     60 
     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;
     67   }
     68 
     69   HandleEventIndex(event_index);
     70 
     71   // Finish test if all events have happened.
     72   if (num_received_events_ == kNumEvents)
     73     SignalTestCompleted();
     74 }
     75 
     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;
     82     }
     83   }
     84   return -1;
     85 }
     86 
     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);
     93   }
     94 }
     95 }
     96 
     97 void ScheduleTimerTest::HandleEventIndex(int event_index) {
     98   const Event& event = schedule_[event_index];
     99 
    100   // Mark event as received.
    101   DCHECK(!received_events_[event_index]);
    102   received_events_[event_index] = true;
    103   ++num_received_events_;
    104 
    105   // Unschedule timer if present.
    106   if (event.unscheduled_index >= 0) {
    107     HostFunctions()->unscheduletimer(
    108         id(), timer_ids_[event.unscheduled_index]);
    109   }
    110 
    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);
    115   }
    116 }
    117 
    118 }  // namespace NPAPIClient
    119