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. |
| 130 template <typename TimerFiredClass> |
| 131 class MockableTimer FINAL : public Timer<TimerFiredClass> { |
| 132 public: |
| 133 typedef typename Timer<TimerFiredClass>::TimerFiredFunction TimerFiredFuncti
on; |
| 134 |
| 135 MockableTimer(TimerFiredClass* o, TimerFiredFunction f) |
| 136 : Timer<TimerFiredClass>(o, f) |
| 137 , m_manualMode(false) |
| 138 , m_firePending(false) |
| 139 { |
| 140 } |
| 141 |
| 142 void setManualModeForTesting(bool manualMode) { m_manualMode = manualMode; } |
| 143 |
| 144 void manualFireForTesting() |
| 145 { |
| 146 ASSERT(m_manualMode); |
| 147 ASSERT(Timer<TimerFiredClass>::isActive() || m_firePending); |
| 148 Timer<TimerFiredClass>::fired(); |
| 149 if (!Timer<TimerFiredClass>::repeatInterval()) |
| 150 Timer<TimerFiredClass>::stop(); |
| 151 m_firePending = false; |
| 152 } |
| 153 |
| 154 private: |
| 155 bool m_manualMode; |
| 156 bool m_firePending; |
| 157 |
| 158 virtual void fired() OVERRIDE |
| 159 { |
| 160 if (m_manualMode) |
| 161 m_firePending = true; |
| 162 else |
| 163 Timer<TimerFiredClass>::fired(); |
| 164 } |
| 165 }; |
| 166 |
123 inline bool TimerBase::isActive() const | 167 inline bool TimerBase::isActive() const |
124 { | 168 { |
125 ASSERT(m_thread == currentThread()); | 169 ASSERT(m_thread == currentThread()); |
126 return m_nextFireTime; | 170 return m_nextFireTime; |
127 } | 171 } |
128 | 172 |
129 template <typename TimerFiredClass> | 173 template <typename TimerFiredClass> |
130 class DeferrableOneShotTimer FINAL : private TimerBase { | 174 class DeferrableOneShotTimer FINAL : private TimerBase { |
131 public: | 175 public: |
132 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*)
; | 176 typedef void (TimerFiredClass::*TimerFiredFunction)(DeferrableOneShotTimer*)
; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 TimerFiredClass* m_object; | 214 TimerFiredClass* m_object; |
171 TimerFiredFunction m_function; | 215 TimerFiredFunction m_function; |
172 | 216 |
173 double m_delay; | 217 double m_delay; |
174 bool m_shouldRestartWhenTimerFires; | 218 bool m_shouldRestartWhenTimerFires; |
175 }; | 219 }; |
176 | 220 |
177 } | 221 } |
178 | 222 |
179 #endif | 223 #endif |
OLD | NEW |