| OLD | NEW |
| 1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_ | 5 #ifndef TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_ |
| 6 #define TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_ | 6 #define TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "testing/embedder_test.h" | 13 #include "testing/embedder_test.h" |
| 14 #include "testing/test_support.h" | 14 #include "testing/test_support.h" |
| 15 | 15 |
| 16 class EmbedderTestTimerHandlingDelegate : public EmbedderTest::Delegate { | 16 class EmbedderTestTimerHandlingDelegate : public EmbedderTest::Delegate { |
| 17 public: | 17 public: |
| 18 struct ReceivedAlert { | 18 struct AlertRecord { |
| 19 ReceivedAlert(FPDF_WIDESTRING message_in, | |
| 20 FPDF_WIDESTRING title_in, | |
| 21 int type_in, | |
| 22 int icon_in) | |
| 23 : type(type_in), icon(icon_in) { | |
| 24 message = GetPlatformWString(message_in); | |
| 25 title = GetPlatformWString(title_in); | |
| 26 } | |
| 27 | |
| 28 std::wstring message; | 19 std::wstring message; |
| 29 std::wstring title; | 20 std::wstring title; |
| 30 int type; | 21 int type; |
| 31 int icon; | 22 int icon; |
| 32 }; | 23 }; |
| 33 | 24 |
| 25 struct Timer { |
| 26 int id; |
| 27 int interval; |
| 28 TimerCallback fn; |
| 29 }; |
| 30 |
| 34 int Alert(FPDF_WIDESTRING message, | 31 int Alert(FPDF_WIDESTRING message, |
| 35 FPDF_WIDESTRING title, | 32 FPDF_WIDESTRING title, |
| 36 int type, | 33 int type, |
| 37 int icon) override { | 34 int icon) override { |
| 38 alerts_.push_back(ReceivedAlert(message, title, type, icon)); | 35 alerts_.push_back( |
| 36 {GetPlatformWString(message), GetPlatformWString(title), type, icon}); |
| 39 return 0; | 37 return 0; |
| 40 } | 38 } |
| 41 | 39 |
| 42 int SetTimer(int msecs, TimerCallback fn) override { | 40 int SetTimer(int msecs, TimerCallback fn) override { |
| 43 expiry_to_timer_map_.insert(std::pair<int, Timer>( | 41 expiry_to_timer_map_.insert(std::pair<int, Timer>( |
| 44 msecs + imaginary_elapsed_msecs_, Timer(++next_timer_id_, fn))); | 42 msecs + fake_elapsed_msecs_, {++next_timer_id_, msecs, fn})); |
| 45 return next_timer_id_; | 43 return next_timer_id_; |
| 46 } | 44 } |
| 47 | 45 |
| 48 void KillTimer(int id) override { | 46 void KillTimer(int id) override { |
| 49 for (auto iter = expiry_to_timer_map_.begin(); | 47 for (auto iter = expiry_to_timer_map_.begin(); |
| 50 iter != expiry_to_timer_map_.end(); ++iter) { | 48 iter != expiry_to_timer_map_.end(); ++iter) { |
| 51 if (iter->second.first == id) { | 49 if (iter->second.id == id) { |
| 52 expiry_to_timer_map_.erase(iter); | 50 expiry_to_timer_map_.erase(iter); |
| 53 break; | 51 break; |
| 54 } | 52 } |
| 55 } | 53 } |
| 56 } | 54 } |
| 57 | 55 |
| 58 void AdvanceTime(int increment_msecs) { | 56 void AdvanceTime(int increment_msecs) { |
| 59 imaginary_elapsed_msecs_ += increment_msecs; | 57 fake_elapsed_msecs_ += increment_msecs; |
| 60 while (1) { | 58 while (1) { |
| 61 auto iter = expiry_to_timer_map_.begin(); | 59 auto iter = expiry_to_timer_map_.begin(); |
| 62 if (iter == expiry_to_timer_map_.end()) { | 60 if (iter == expiry_to_timer_map_.end()) { |
| 63 break; | 61 break; |
| 64 } | 62 } |
| 65 Timer t = iter->second; | 63 if (iter->first > fake_elapsed_msecs_) { |
| 66 if (t.first > imaginary_elapsed_msecs_) { | |
| 67 break; | 64 break; |
| 68 } | 65 } |
| 66 Timer t = iter->second; |
| 69 expiry_to_timer_map_.erase(iter); | 67 expiry_to_timer_map_.erase(iter); |
| 70 t.second(t.first); // Fire timer. | 68 expiry_to_timer_map_.insert( |
| 69 std::pair<int, Timer>(fake_elapsed_msecs_ + t.interval, t)); |
| 70 t.fn(t.id); // Fire timer. |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 const std::vector<ReceivedAlert>& GetAlerts() const { return alerts_; } | 74 const std::vector<AlertRecord>& GetAlerts() const { return alerts_; } |
| 75 | 75 |
| 76 protected: | 76 protected: |
| 77 using Timer = std::pair<int, TimerCallback>; // ID, callback pair. | |
| 78 std::multimap<int, Timer> expiry_to_timer_map_; // Keyed by timeout. | 77 std::multimap<int, Timer> expiry_to_timer_map_; // Keyed by timeout. |
| 79 int next_timer_id_ = 0; | 78 int next_timer_id_ = 0; |
| 80 int imaginary_elapsed_msecs_ = 0; | 79 int fake_elapsed_msecs_ = 0; |
| 81 std::vector<ReceivedAlert> alerts_; | 80 std::vector<AlertRecord> alerts_; |
| 82 }; | 81 }; |
| 83 | 82 |
| 84 #endif // TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_ | 83 #endif // TESTING_EMBEDDER_TEST_TIMER_HANDLING_DELEGATE_H_ |
| OLD | NEW |