blob: a32ad202461209a59af8504f99257fe22660f6c1 [file] [log] [blame]
Tom Sepez6efc0ad2015-06-02 17:11:18 -07001// 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>
Lei Zhang79e893a2015-11-04 16:02:47 -08009#include <string>
Tom Sepez6efc0ad2015-06-02 17:11:18 -070010#include <utility>
Lei Zhang79e893a2015-11-04 16:02:47 -080011#include <vector>
Tom Sepez6efc0ad2015-06-02 17:11:18 -070012
Dan Sinclairefbc1912016-02-17 16:54:43 -050013#include "testing/embedder_test.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000014#include "testing/fx_string_testhelpers.h"
Tom Sepez6efc0ad2015-06-02 17:11:18 -070015
Tom Sepez55865452018-08-27 20:18:04 +000016class EmbedderTestTimerHandlingDelegate final : public EmbedderTest::Delegate {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070017 public:
tsepez8e120292016-08-03 14:03:35 -070018 struct AlertRecord {
Lei Zhang79e893a2015-11-04 16:02:47 -080019 std::wstring message;
20 std::wstring title;
21 int type;
22 int icon;
23 };
24
tsepez8e120292016-08-03 14:03:35 -070025 struct Timer {
26 int id;
27 int interval;
28 TimerCallback fn;
29 };
30
Lei Zhang79e893a2015-11-04 16:02:47 -080031 int Alert(FPDF_WIDESTRING message,
32 FPDF_WIDESTRING title,
33 int type,
34 int icon) override {
tsepez8e120292016-08-03 14:03:35 -070035 alerts_.push_back(
36 {GetPlatformWString(message), GetPlatformWString(title), type, icon});
Lei Zhang79e893a2015-11-04 16:02:47 -080037 return 0;
38 }
39
Tom Sepez6efc0ad2015-06-02 17:11:18 -070040 int SetTimer(int msecs, TimerCallback fn) override {
tsepez6cf5eca2017-01-12 11:21:12 -080041 int id = fail_next_timer_ ? 0 : ++next_timer_id_;
42 expiry_to_timer_map_.insert(
43 std::pair<int, Timer>(msecs + fake_elapsed_msecs_, {id, msecs, fn}));
44 fail_next_timer_ = false;
45 return id;
Tom Sepez6efc0ad2015-06-02 17:11:18 -070046 }
47
48 void KillTimer(int id) override {
49 for (auto iter = expiry_to_timer_map_.begin();
50 iter != expiry_to_timer_map_.end(); ++iter) {
tsepez8e120292016-08-03 14:03:35 -070051 if (iter->second.id == id) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -070052 expiry_to_timer_map_.erase(iter);
53 break;
54 }
55 }
56 }
57
58 void AdvanceTime(int increment_msecs) {
tsepez8e120292016-08-03 14:03:35 -070059 fake_elapsed_msecs_ += increment_msecs;
Tom Sepez6efc0ad2015-06-02 17:11:18 -070060 while (1) {
61 auto iter = expiry_to_timer_map_.begin();
62 if (iter == expiry_to_timer_map_.end()) {
63 break;
64 }
tsepez8e120292016-08-03 14:03:35 -070065 if (iter->first > fake_elapsed_msecs_) {
Tom Sepez6efc0ad2015-06-02 17:11:18 -070066 break;
67 }
tsepez8e120292016-08-03 14:03:35 -070068 Timer t = iter->second;
Tom Sepez6efc0ad2015-06-02 17:11:18 -070069 expiry_to_timer_map_.erase(iter);
tsepez8e120292016-08-03 14:03:35 -070070 expiry_to_timer_map_.insert(
71 std::pair<int, Timer>(fake_elapsed_msecs_ + t.interval, t));
72 t.fn(t.id); // Fire timer.
Tom Sepez6efc0ad2015-06-02 17:11:18 -070073 }
74 }
75
tsepez8e120292016-08-03 14:03:35 -070076 const std::vector<AlertRecord>& GetAlerts() const { return alerts_; }
Lei Zhang79e893a2015-11-04 16:02:47 -080077
tsepez6cf5eca2017-01-12 11:21:12 -080078 void SetFailNextTimer() { fail_next_timer_ = true; }
79
Tom Sepezcb798252018-09-17 18:25:32 +000080 private:
Tom Sepez6efc0ad2015-06-02 17:11:18 -070081 std::multimap<int, Timer> expiry_to_timer_map_; // Keyed by timeout.
tsepez6cf5eca2017-01-12 11:21:12 -080082 bool fail_next_timer_ = false;
Tom Sepez6efc0ad2015-06-02 17:11:18 -070083 int next_timer_id_ = 0;
tsepez8e120292016-08-03 14:03:35 -070084 int fake_elapsed_msecs_ = 0;
85 std::vector<AlertRecord> alerts_;
Tom Sepez6efc0ad2015-06-02 17:11:18 -070086};
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088#endif // TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_