Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1325)

Unified Diff: Source/platform/Timer.h

Issue 112023010: Make EventHandler::ActiveIntervalTimer mockable. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rename methods and handle pending fires Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« Source/core/testing/TimerTest.cpp ('K') | « Source/core/testing/TimerTest.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/platform/Timer.h
diff --git a/Source/platform/Timer.h b/Source/platform/Timer.h
index 1f03cf11dc894643c79ec0b2ed0eda911cebf9fb..68c6436bde8a06db316523bc7fa2816a8cb895de 100644
--- a/Source/platform/Timer.h
+++ b/Source/platform/Timer.h
@@ -105,8 +105,12 @@ private:
friend class TimerHeapReference;
};
+template <typename TimerFiredClass> class MockableTimer;
+
template <typename TimerFiredClass>
class Timer : public TimerBase {
+ friend class MockableTimer<TimerFiredClass>;
+
public:
typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*);
@@ -120,6 +124,50 @@ private:
TimerFiredFunction m_function;
};
+// Just derive from TimerBase and skip the template-fu?
Rick Byers 2014/01/10 21:46:50 I think you need the template-fu to get the timer-
Zeeshan Qureshi 2014/01/10 22:20:41 I think we already decided on this, forgot to remo
+template <typename TimerFiredClass>
Rick Byers 2014/01/10 21:46:50 I'd add a comment here saying the idea is to allow
Zeeshan Qureshi 2014/01/10 22:20:41 Yes, adding.
+class MockableTimer : public Timer<TimerFiredClass> {
+public:
+ typedef typename Timer<TimerFiredClass>::TimerFiredFunction TimerFiredFunction;
+
+ MockableTimer(TimerFiredClass* o, TimerFiredFunction f)
+ : Timer<TimerFiredClass>(o, f)
+ , m_manualMode(false)
+ , m_firePending(false)
+ {
+ }
+
+ bool inManualModeForTesting() const { return m_manualMode; }
Rick Byers 2014/01/10 21:46:50 blink coding guidelines say to use bare words for
+
+ bool firePendingForTesting() const { return m_firePending; }
Rick Byers 2014/01/10 21:46:50 Again, this is used only by the unit test, right?
Zeeshan Qureshi 2014/01/10 22:20:41 Removing the above methods seems fine, I was under
Rick Byers 2014/01/10 22:24:36 Nope, encapsulation is your friend!
+
+ void enterManualModeForTesting() { m_manualMode = true; }
Rick Byers 2014/01/10 21:46:50 collapse enter/leave into a single 'setManualModeF
+
+ void leaveManualModeForTesting() { m_manualMode = false; }
+
+ void manualFireForTesting()
+ {
+ ASSERT(m_manualMode);
+ ASSERT(Timer<TimerFiredClass>::isActive() || m_firePending);
+ Timer<TimerFiredClass>::fired();
+ if (!Timer<TimerFiredClass>::repeatInterval())
+ Timer<TimerFiredClass>::stop();
+ m_firePending = false;
+ }
+
+private:
+ bool m_manualMode;
+ bool m_firePending;
+
+ virtual void fired()
+ {
+ if (m_manualMode)
+ m_firePending = true;
+ if (!m_manualMode)
+ Timer<TimerFiredClass>::fired();
+ }
+};
+
inline bool TimerBase::isActive() const
{
ASSERT(m_thread == currentThread());
« Source/core/testing/TimerTest.cpp ('K') | « Source/core/testing/TimerTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698