-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
clock.go
111 lines (92 loc) · 2.26 KB
/
clock.go
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
package playwright
import (
"errors"
"time"
)
type clockImpl struct {
browserCtx *browserContextImpl
}
func newClock(bCtx *browserContextImpl) Clock {
return &clockImpl{
browserCtx: bCtx,
}
}
func (c *clockImpl) FastForward(ticks interface{}) error {
params, err := parseTicks(ticks)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockFastForward", params)
return err
}
func (c *clockImpl) Install(options ...ClockInstallOptions) (err error) {
params := map[string]any{}
if len(options) == 1 {
if options[0].Time != nil {
params, err = parseTime(options[0].Time)
if err != nil {
return err
}
}
}
_, err = c.browserCtx.channel.Send("clockInstall", params)
return err
}
func (c *clockImpl) PauseAt(time interface{}) error {
params, err := parseTime(time)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockPauseAt", params)
return err
}
func (c *clockImpl) Resume() error {
_, err := c.browserCtx.channel.Send("clockResume")
return err
}
func (c *clockImpl) RunFor(ticks interface{}) error {
params, err := parseTicks(ticks)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockRunFor", params)
return err
}
func (c *clockImpl) SetFixedTime(time interface{}) error {
params, err := parseTime(time)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockSetFixedTime", params)
return err
}
func (c *clockImpl) SetSystemTime(time interface{}) error {
params, err := parseTime(time)
if err != nil {
return err
}
_, err = c.browserCtx.channel.Send("clockSetSystemTime", params)
return err
}
func parseTime(t interface{}) (map[string]any, error) {
switch v := t.(type) {
case int, int64:
return map[string]any{"timeNumber": v}, nil
case string:
return map[string]any{"timeString": v}, nil
case time.Time:
return map[string]any{"timeNumber": v.UnixMilli()}, nil
default:
return nil, errors.New("time should be one of: int, int64, string, time.Time")
}
}
func parseTicks(ticks interface{}) (map[string]any, error) {
switch v := ticks.(type) {
case int, int64:
return map[string]any{"ticksNumber": v}, nil
case string:
return map[string]any{"ticksString": v}, nil
default:
return nil, errors.New("ticks should be one of: int, int64, string")
}
}