OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 | 98 |
99 #ifndef NDEBUG | 99 #ifndef NDEBUG |
100 ThreadIdentifier m_thread; | 100 ThreadIdentifier m_thread; |
101 #endif | 101 #endif |
102 | 102 |
103 friend class ThreadTimers; | 103 friend class ThreadTimers; |
104 friend class TimerHeapLessThanFunction; | 104 friend class TimerHeapLessThanFunction; |
105 friend class TimerHeapReference; | 105 friend class TimerHeapReference; |
106 }; | 106 }; |
107 | 107 |
108 template <typename TimerFiredClass> class MockableTimer; | |
109 | |
108 template <typename TimerFiredClass> | 110 template <typename TimerFiredClass> |
109 class Timer FINAL : public TimerBase { | 111 class Timer : public TimerBase { |
112 friend class MockableTimer<TimerFiredClass>; | |
113 | |
110 public: | 114 public: |
111 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*); | 115 typedef void (TimerFiredClass::*TimerFiredFunction)(Timer*); |
112 | 116 |
113 Timer(TimerFiredClass* o, TimerFiredFunction f) | 117 Timer(TimerFiredClass* o, TimerFiredFunction f) |
114 : m_object(o), m_function(f) { } | 118 : m_object(o), m_function(f) { } |
115 | 119 |
116 private: | 120 private: |
117 virtual void fired() OVERRIDE { (m_object->*m_function)(this); } | 121 virtual void fired() OVERRIDE { (m_object->*m_function)(this); } |
118 | 122 |
119 TimerFiredClass* m_object; | 123 TimerFiredClass* m_object; |
120 TimerFiredFunction m_function; | 124 TimerFiredFunction m_function; |
121 }; | 125 }; |
122 | 126 |
127 // A mockable timer is useful in manually controlling timers from tests | |
128 // to avoid flakiness. To access a specific instance in layout tests, | |
129 // it has to be plumbed through internals. The plan is to eventually have | |
130 // a mocked system clock that would make this unnecessary. | |
eseidel
2014/01/21 21:24:42
If this is no longer the plan, we should update th
| |
131 template <typename TimerFiredClass> | |
132 class MockableTimer FINAL : public Timer<TimerFiredClass> { | |
133 public: | |
134 typedef typename Timer<TimerFiredClass>::TimerFiredFunction TimerFiredFuncti on; | |
135 | |
136 MockableTimer(TimerFiredClass* o, TimerFiredFunction f) | |
137 : Timer<TimerFiredClass>(o, f) | |
138 , m_manualMode(false) | |
139 , m_firePending(false) | |
140 { | |
141 } | |
142 | |
143 void setManualModeForTesting(bool manualMode) { m_manualMode = manualMode; } | |
144 | |
145 void manualFireForTesting() | |
146 { | |
147 ASSERT(m_manualMode); | |
148 ASSERT(Timer<TimerFiredClass>::isActive() || m_firePending); | |
149 Timer<TimerFiredClass>::fired(); | |
150 if (!Timer<TimerFiredClass>::repeatInterval()) | |
151 Timer<TimerFiredClass>::stop(); | |
152 m_firePending = false; | |
153 } | |
154 | |
155 private: | |
156 bool m_manualMode; | |
157 bool m_firePending; | |
158 | |
159 virtual void fired() OVERRIDE | |
160 { | |
161 if (m_manualMode) | |
162 m_firePending = true; | |
163 else | |
164 Timer<TimerFiredClass>::fired(); | |
165 } | |
166 }; | |
167 | |
123 inline bool TimerBase::isActive() const | 168 inline bool TimerBase::isActive() const |
124 { | 169 { |
125 ASSERT(m_thread == currentThread()); | 170 ASSERT(m_thread == currentThread()); |
126 return m_nextFireTime; | 171 return m_nextFireTime; |
127 } | 172 } |
128 | 173 |
129 template <typename TimerFiredClass> | 174 template <typename TimerFiredClass> |
130 class DeferrableOneShotTimer FINAL : private TimerBase { | 175 class DeferrableOneShotTimer FINAL : private TimerBase { |
131 public: | 176 public: |
132 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*) ; | 177 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*) ; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 TimerFiredClass* m_object; | 215 TimerFiredClass* m_object; |
171 TimerFiredFunction m_function; | 216 TimerFiredFunction m_function; |
172 | 217 |
173 double m_delay; | 218 double m_delay; |
174 bool m_shouldRestartWhenTimerFires; | 219 bool m_shouldRestartWhenTimerFires; |
175 }; | 220 }; |
176 | 221 |
177 } | 222 } |
178 | 223 |
179 #endif | 224 #endif |
OLD | NEW |