-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathIClock.cs
270 lines (254 loc) · 11.2 KB
/
IClock.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Threading.Tasks;
#nullable enable
namespace Microsoft.Playwright;
/// <summary>
/// <para>
/// Accurately simulating time-dependent behavior is essential for verifying the correctness
/// of applications. Learn more about <a href="https://playwright.dev/dotnet/docs/clock">clock
/// emulation</a>.
/// </para>
/// <para>
/// Note that clock is installed for the entire <see cref="IBrowserContext"/>, so the
/// time in all the pages and iframes is controlled by the same clock.
/// </para>
/// </summary>
public partial interface IClock
{
/// <summary>
/// <para>
/// Advance the clock by jumping forward in time. Only fires due timers at most once.
/// This is equivalent to user closing the laptop lid for a while and reopening it later,
/// after given time.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.FastForwardAsync(1000);<br/>
/// await page.Clock.FastForwardAsync("30:00");
/// </code>
/// </summary>
/// <param name="ticks">
/// Time may be the number of milliseconds to advance the clock by or a human-readable
/// string. Valid string formats are "08" for eight seconds, "01:00" for one minute
/// and "02:34:10" for two hours, 34 minutes and ten seconds.
/// </param>
Task FastForwardAsync(long ticks);
/// <summary>
/// <para>
/// Advance the clock by jumping forward in time. Only fires due timers at most once.
/// This is equivalent to user closing the laptop lid for a while and reopening it later,
/// after given time.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.FastForwardAsync(1000);<br/>
/// await page.Clock.FastForwardAsync("30:00");
/// </code>
/// </summary>
/// <param name="ticks">
/// Time may be the number of milliseconds to advance the clock by or a human-readable
/// string. Valid string formats are "08" for eight seconds, "01:00" for one minute
/// and "02:34:10" for two hours, 34 minutes and ten seconds.
/// </param>
Task FastForwardAsync(string ticks);
/// <summary>
/// <para>Install fake implementations for the following time-related functions:</para>
/// <list type="bullet">
/// <item><description><c>Date</c></description></item>
/// <item><description><c>setTimeout</c></description></item>
/// <item><description><c>clearTimeout</c></description></item>
/// <item><description><c>setInterval</c></description></item>
/// <item><description><c>clearInterval</c></description></item>
/// <item><description><c>requestAnimationFrame</c></description></item>
/// <item><description><c>cancelAnimationFrame</c></description></item>
/// <item><description><c>requestIdleCallback</c></description></item>
/// <item><description><c>cancelIdleCallback</c></description></item>
/// <item><description><c>performance</c></description></item>
/// </list>
/// <para>
/// Fake timers are used to manually control the flow of time in tests. They allow you
/// to advance time, fire timers, and control the behavior of time-dependent functions.
/// See <see cref="IClock.RunForAsync"/> and <see cref="IClock.FastForwardAsync"/> for
/// more information.
/// </para>
/// </summary>
/// <param name="options">Call options</param>
Task InstallAsync(ClockInstallOptions? options = default);
/// <summary>
/// <para>Advance the clock, firing all the time-related callbacks.</para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.RunForAsync(1000);<br/>
/// await page.Clock.RunForAsync("30:00");
/// </code>
/// </summary>
/// <param name="ticks">
/// Time may be the number of milliseconds to advance the clock by or a human-readable
/// string. Valid string formats are "08" for eight seconds, "01:00" for one minute
/// and "02:34:10" for two hours, 34 minutes and ten seconds.
/// </param>
Task RunForAsync(long ticks);
/// <summary>
/// <para>Advance the clock, firing all the time-related callbacks.</para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.RunForAsync(1000);<br/>
/// await page.Clock.RunForAsync("30:00");
/// </code>
/// </summary>
/// <param name="ticks">
/// Time may be the number of milliseconds to advance the clock by or a human-readable
/// string. Valid string formats are "08" for eight seconds, "01:00" for one minute
/// and "02:34:10" for two hours, 34 minutes and ten seconds.
/// </param>
Task RunForAsync(string ticks);
/// <summary>
/// <para>
/// Advance the clock by jumping forward in time and pause the time. Once this method
/// is called, no timers are fired unless <see cref="IClock.RunForAsync"/>, <see cref="IClock.FastForwardAsync"/>,
/// <see cref="IClock.PauseAtAsync"/> or <see cref="IClock.ResumeAsync"/> is called.
/// </para>
/// <para>
/// Only fires due timers at most once. This is equivalent to user closing the laptop
/// lid for a while and reopening it at the specified time and pausing.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.PauseAtAsync(DateTime.Parse("2020-02-02"));<br/>
/// await page.Clock.PauseAtAsync("2020-02-02");
/// </code>
/// <para>
/// For best results, install the clock before navigating the page and set it to a time
/// slightly before the intended test time. This ensures that all timers run normally
/// during page loading, preventing the page from getting stuck. Once the page has fully
/// loaded, you can safely use <see cref="IClock.PauseAtAsync"/> to pause the clock.
/// </para>
/// </summary>
/// <param name="time">Time to pause at.</param>
Task PauseAtAsync(DateTime time);
/// <summary>
/// <para>
/// Advance the clock by jumping forward in time and pause the time. Once this method
/// is called, no timers are fired unless <see cref="IClock.RunForAsync"/>, <see cref="IClock.FastForwardAsync"/>,
/// <see cref="IClock.PauseAtAsync"/> or <see cref="IClock.ResumeAsync"/> is called.
/// </para>
/// <para>
/// Only fires due timers at most once. This is equivalent to user closing the laptop
/// lid for a while and reopening it at the specified time and pausing.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.PauseAtAsync(DateTime.Parse("2020-02-02"));<br/>
/// await page.Clock.PauseAtAsync("2020-02-02");
/// </code>
/// <para>
/// For best results, install the clock before navigating the page and set it to a time
/// slightly before the intended test time. This ensures that all timers run normally
/// during page loading, preventing the page from getting stuck. Once the page has fully
/// loaded, you can safely use <see cref="IClock.PauseAtAsync"/> to pause the clock.
/// </para>
/// </summary>
/// <param name="time">Time to pause at.</param>
Task PauseAtAsync(string time);
/// <summary>
/// <para>
/// Resumes timers. Once this method is called, time resumes flowing, timers are fired
/// as usual.
/// </para>
/// </summary>
Task ResumeAsync();
/// <summary>
/// <para>
/// Makes <c>Date.now</c> and <c>new Date()</c> return fixed fake time at all times,
/// keeps all the timers running.
/// </para>
/// <para>
/// Use this method for simple scenarios where you only need to test with a predefined
/// time. For more advanced scenarios, use <see cref="IClock.InstallAsync"/> instead.
/// Read docs on <a href="https://playwright.dev/dotnet/docs/clock">clock emulation</a>
/// to learn more.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.SetFixedTimeAsync(DateTime.Now);<br/>
/// await page.Clock.SetFixedTimeAsync(new DateTime(2020, 2, 2));<br/>
/// await page.Clock.SetFixedTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">Time to be set.</param>
Task SetFixedTimeAsync(string time);
/// <summary>
/// <para>
/// Makes <c>Date.now</c> and <c>new Date()</c> return fixed fake time at all times,
/// keeps all the timers running.
/// </para>
/// <para>
/// Use this method for simple scenarios where you only need to test with a predefined
/// time. For more advanced scenarios, use <see cref="IClock.InstallAsync"/> instead.
/// Read docs on <a href="https://playwright.dev/dotnet/docs/clock">clock emulation</a>
/// to learn more.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.SetFixedTimeAsync(DateTime.Now);<br/>
/// await page.Clock.SetFixedTimeAsync(new DateTime(2020, 2, 2));<br/>
/// await page.Clock.SetFixedTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">Time to be set.</param>
Task SetFixedTimeAsync(DateTime time);
/// <summary>
/// <para>
/// Sets system time, but does not trigger any timers. Use this to test how the web
/// page reacts to a time shift, for example switching from summer to winter time, or
/// changing time zones.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.SetSystemTimeAsync(DateTime.Now);<br/>
/// await page.Clock.SetSystemTimeAsync(new DateTime(2020, 2, 2));<br/>
/// await page.Clock.SetSystemTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">Time to be set.</param>
Task SetSystemTimeAsync(string time);
/// <summary>
/// <para>
/// Sets system time, but does not trigger any timers. Use this to test how the web
/// page reacts to a time shift, for example switching from summer to winter time, or
/// changing time zones.
/// </para>
/// <para>**Usage**</para>
/// <code>
/// await page.Clock.SetSystemTimeAsync(DateTime.Now);<br/>
/// await page.Clock.SetSystemTimeAsync(new DateTime(2020, 2, 2));<br/>
/// await page.Clock.SetSystemTimeAsync("2020-02-02");
/// </code>
/// </summary>
/// <param name="time">Time to be set.</param>
Task SetSystemTimeAsync(DateTime time);
}
#nullable disable