Home | History | Annotate | Download | only in testing
      1 // Copyright 2015 PDFium 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 #ifndef TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_
      6 #define TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_
      7 
      8 #include <map>
      9 #include <utility>
     10 
     11 #include "embedder_test.h"
     12 
     13 class EmbedderTestTimerHandlingDelegate : public EmbedderTest::Delegate {
     14 public:
     15   int SetTimer(int msecs, TimerCallback fn) override {
     16     expiry_to_timer_map_.insert(std::pair<int, Timer>(
     17         msecs + imaginary_elapsed_msecs_, Timer(++next_timer_id_, fn)));
     18     return next_timer_id_;
     19   }
     20 
     21   void KillTimer(int id) override {
     22     for (auto iter = expiry_to_timer_map_.begin();
     23          iter != expiry_to_timer_map_.end(); ++iter) {
     24       if (iter->second.first == id) {
     25         expiry_to_timer_map_.erase(iter);
     26         break;
     27       }
     28     }
     29   }
     30 
     31   void AdvanceTime(int increment_msecs) {
     32     imaginary_elapsed_msecs_ += increment_msecs;
     33     while (1) {
     34       auto iter = expiry_to_timer_map_.begin();
     35       if (iter == expiry_to_timer_map_.end()) {
     36         break;
     37       }
     38       Timer t = iter->second;
     39       if (t.first > imaginary_elapsed_msecs_) {
     40         break;
     41       }
     42       expiry_to_timer_map_.erase(iter);
     43       t.second(t.first);  // Fire timer.
     44     }
     45   }
     46 
     47 protected:
     48   using Timer = std::pair<int, TimerCallback>;  // ID, callback pair.
     49   std::multimap<int, Timer> expiry_to_timer_map_;  // Keyed by timeout.
     50   int next_timer_id_ = 0;
     51   int imaginary_elapsed_msecs_ = 0;
     52 };
     53 
     54 #endif // TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_
     55