Skip to content

Commit 6f2b9a4

Browse files
committed
[rb] revamp Actions unit tests
1 parent 52f9967 commit 6f2b9a4

9 files changed

Lines changed: 739 additions & 840 deletions

File tree

rb/spec/unit/selenium/webdriver/common/action_builder_spec.rb

Lines changed: 152 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -22,145 +22,214 @@
2222
module Selenium
2323
module WebDriver
2424
describe ActionBuilder do
25-
let(:keyboard) do
26-
instance_double(Interactions::KeyInput,
27-
actions: [1, 2, 3],
28-
name: 'keyboard',
29-
type: Interactions::KEY)
30-
end
31-
let(:mouse) do
32-
instance_double(Interactions::PointerInput,
33-
actions: [1, 2, 3],
34-
name: 'mouse',
35-
type: Interactions::POINTER)
36-
end
25+
let(:keyboard) { Interactions.key('key') }
26+
let(:mouse) { Interactions.pointer(:mouse, name: 'mouse') }
3727
let(:bridge) { instance_double('Bridge').as_null_object }
3828
let(:builder) { ActionBuilder.new(bridge, mouse, keyboard) }
3929
let(:async_builder) { ActionBuilder.new(bridge, mouse, keyboard, true) }
4030

41-
context 'when adding an input device' do
42-
let(:pointer_input) { Interactions.pointer(:touch, name: 'touch') }
31+
describe '#devices' do
32+
it 'returns Array of devices' do
33+
expect(builder.devices).to include(a_kind_of(Interactions::KeyInput),
34+
a_kind_of(Interactions::PointerInput))
35+
end
36+
end
37+
38+
describe '#add_pointer_input' do
39+
let(:device) { instance_double Interactions::PointerInput, actions: [] }
40+
41+
it 'creates pointer and adds to devices' do
42+
builder
43+
allow(Interactions).to receive(:pointer).and_return(device)
44+
builder.add_pointer_input(:touch, 'name')
4345

44-
it 'should be able to add an input device' do
45-
expect { async_builder.send('add_input', pointer_input) }.to change { async_builder.devices.length }.by(1)
46+
expect(builder.devices).to include(device)
47+
expect(Interactions).to have_received(:pointer).with(:touch, name: 'name')
4648
end
4749

48-
it 'should add pauses to match the device with the most actions when synchronous' do
49-
expect(builder).to receive(:pauses).with(pointer_input, 3)
50+
it 'adds pauses to match other devices when sync' do
51+
builder.key_down('d', device: 'key')
52+
builder.key_up('d', device: 'key')
5053

51-
builder.send('add_input', pointer_input)
54+
allow(Interactions).to receive(:pointer).and_return(device)
55+
allow(device).to receive(:name)
56+
allow(device).to receive(:create_pause)
57+
58+
builder.add_pointer_input(:touch, 'name')
59+
expect(device).to have_received(:create_pause).twice
5260
end
53-
end # when adding an input device
5461

55-
context 'when adding a pointer input' do
56-
it 'should add a PointerInput' do
57-
allow(Interactions::PointerInput).to receive(:new).with(:touch, name: 'touch').and_return(:device)
58-
expect(builder).to receive(:add_input).with(:device)
62+
it 'does not add pauses to match other devices when async' do
63+
async_builder.key_down('d', device: 'key')
64+
async_builder.key_up('d', device: 'key')
65+
66+
allow(Interactions).to receive(:pointer).and_return(device)
67+
allow(device).to receive(:name)
68+
allow(device).to receive(:create_pause)
5969

60-
expect(builder.add_pointer_input(:touch, 'touch')).to eq(:device)
61-
expect(Interactions::PointerInput).to have_received(:new).with(:touch, name: 'touch')
70+
async_builder.add_pointer_input(:touch, 'name')
71+
expect(device).not_to have_received(:create_pause)
6272
end
73+
end
6374

64-
it 'should not assign the pointer input as primary if not primary' do
65-
expect(builder).to receive(:add_input)
66-
expect(builder).not_to receive(:set_primary_pointer)
75+
describe '#add_key_input' do
76+
let(:device) { instance_double Interactions::KeyInput, actions: [] }
6777

68-
builder.add_pointer_input(:touch, 'touch')
78+
it 'creates keyboard and adds to devices' do
79+
builder
80+
allow(Interactions).to receive(:key).and_return(device)
81+
82+
builder.add_key_input('name')
83+
84+
expect(builder.devices).to include(device)
85+
expect(Interactions).to have_received(:key).with('name')
6986
end
70-
end # when adding a pointer input
7187

72-
it 'should add a key input' do
73-
allow(Interactions::KeyInput).to receive(:new).with('keyboard').and_return(:device)
74-
expect(builder).to receive(:add_input).with(:device)
88+
it 'adds pauses to match other devices' do
89+
builder.key_down('d', device: 'key')
90+
builder.key_up('d', device: 'key')
7591

76-
expect(builder.add_key_input('keyboard')).to eq(:device)
77-
expect(Interactions::KeyInput).to have_received(:new).with('keyboard')
92+
allow(Interactions).to receive(:key).and_return(device)
93+
allow(device).to receive(:create_pause)
94+
95+
builder.add_key_input('name')
96+
expect(device).to have_received(:create_pause).twice
97+
end
7898
end
7999

80-
it 'should get a device by name' do
81-
expect(builder.get_device('mouse')).to eq(mouse)
100+
describe '#get_device' do
101+
it 'gets device by name' do
102+
expect(builder.get_device('mouse')).to eq(mouse)
103+
expect(builder.get_device('key')).to eq(keyboard)
104+
end
82105
end
83106

84-
it 'should return only pointer inputs' do
85-
expect(builder.pointer_inputs).to eq([mouse])
107+
describe '#pointer_inputs' do
108+
it 'returns only pointer inputs' do
109+
touch_input = builder.add_pointer_input(:touch, 'touch')
110+
pen_input = builder.add_pointer_input(:pen, 'pen')
111+
builder.add_key_input('key2')
112+
113+
expect(builder.pointer_inputs).to eq([mouse, touch_input, pen_input])
114+
end
86115
end
87116

88-
it 'should return the key inputs' do
89-
expect(builder.key_inputs).to eq([keyboard])
117+
describe '#key_inputs' do
118+
it 'returns only key inputs' do
119+
builder.add_pointer_input(:touch, 'touch')
120+
builder.add_pointer_input(:pen, 'pen')
121+
key_input = builder.add_key_input('key2')
122+
123+
expect(builder.key_inputs).to eq([keyboard, key_input])
124+
end
90125
end
91126

92-
it 'should create a pause for the given device' do
93-
duration = 5
94-
expect(mouse).to receive(:create_pause).with(duration)
127+
describe '#pause' do
128+
it 'creates pause with default duration' do
129+
allow(mouse).to receive :create_pause
130+
131+
builder.pause(mouse)
132+
133+
expect(mouse).to have_received(:create_pause).with(nil)
134+
end
135+
136+
it 'creates pause with provided duration' do
137+
allow(mouse).to receive :create_pause
138+
139+
builder.pause(mouse, 5)
95140

96-
builder.pause(mouse, 5)
141+
expect(mouse).to have_received(:create_pause).with(5)
142+
end
97143
end
98144

99-
it 'should create multiple pauses for the given device' do
100-
duration = 5
101-
number = 3
102-
expect(mouse).to receive(:create_pause).with(duration).exactly(number).times
145+
describe '#pauses' do
146+
it 'adds multiple pause commands' do
147+
allow(mouse).to receive :create_pause
103148

104-
builder.pauses(mouse, number, duration)
149+
builder.pauses(mouse, 3)
150+
151+
expect(mouse).to have_received(:create_pause).with(nil).exactly(3).times
152+
end
105153
end
106154

107-
context 'when performing actions' do
108-
it 'should encode each device' do
109-
expect(mouse).to receive(:encode)
110-
expect(keyboard).to receive(:encode)
111-
allow(builder).to receive(:clear_all_actions)
155+
describe '#perform' do
156+
it 'encodes each device' do
157+
allow(mouse).to receive(:encode)
158+
allow(keyboard).to receive(:encode)
112159

113160
builder.perform
161+
162+
expect(keyboard).to have_received(:encode)
163+
expect(mouse).to have_received(:encode)
114164
end
115165

116-
it 'should call bridge#send_actions with encoded and compacted devices' do
117-
allow(mouse).to receive(:encode).and_return(nil)
118-
allow(keyboard).to receive(:encode).and_return('not_nil')
119-
expect(bridge).to receive(:send_actions).with(['not_nil'])
166+
it 'clears all actions' do
120167
allow(builder).to receive(:clear_all_actions)
121168

122169
builder.perform
170+
171+
expect(builder).to have_received(:clear_all_actions)
123172
end
124173

125-
it 'should clear all actions' do
126-
allow(mouse).to receive(:encode)
127-
allow(keyboard).to receive(:encode)
128-
expect(builder).to receive(:clear_all_actions)
174+
it 'sends non-nil encoded actions to bridge' do
175+
allow(mouse).to receive(:encode).and_return(nil)
176+
allow(keyboard).to receive(:encode).and_return(keyboard: 'encoded')
177+
allow(bridge).to receive(:send_actions)
129178

130179
builder.perform
180+
expect(bridge).to have_received(:send_actions).with([{keyboard: 'encoded'}])
131181
end
132-
end # when performing actions
182+
end
133183

134-
it 'should clear all actions from devices' do
135-
expect(mouse).to receive(:clear_actions)
136-
expect(keyboard).to receive(:clear_actions)
184+
describe '#clear_all_actions' do
185+
it 'sends clear_actions to each devices' do
186+
allow(mouse).to receive(:clear_actions)
187+
allow(keyboard).to receive(:clear_actions)
137188

138-
builder.clear_all_actions
189+
builder.clear_all_actions
190+
191+
expect(mouse).to have_received(:clear_actions)
192+
expect(keyboard).to have_received(:clear_actions)
193+
end
139194
end
140195

141-
it 'should release actions' do
142-
expect(bridge).to receive(:release_actions)
196+
describe '#release_actions' do
197+
it 'sends release actions command to bridge' do
198+
allow(bridge).to receive(:release_actions)
199+
200+
builder.release_actions
143201

144-
builder.release_actions
202+
expect(bridge).to have_received(:release_actions)
203+
end
145204
end
146205

147-
context 'when adding a tick' do
148-
it 'should not create pauses for any devices when asynchronous' do
149-
expect(mouse).not_to receive(:create_pause)
150-
expect(keyboard).not_to receive(:create_pause)
206+
describe 'tick' do
207+
it 'adds pauses to non-active devices when synchronous' do
208+
touch = builder.add_pointer_input(:touch, 'touch')
209+
allow(mouse).to receive(:create_pause)
210+
allow(touch).to receive(:create_pause)
211+
allow(keyboard).to receive(:create_pause)
212+
213+
builder.pointer_down(:left, device: 'mouse')
151214

152-
async_builder.send('tick', mouse)
215+
expect(mouse).not_to have_received(:create_pause)
216+
expect(touch).to have_received(:create_pause)
217+
expect(keyboard).to have_received(:create_pause)
153218
end
154219

155-
it 'should create pauses for devices not passed when synchronous' do
220+
it 'does not create pauses for any devices when asynchronous' do
156221
touch = builder.add_pointer_input(:touch, 'touch')
157-
expect(touch).to receive(:create_pause)
158-
expect(keyboard).not_to receive(:create_pause)
159-
expect(mouse).not_to receive(:create_pause)
222+
allow(mouse).to receive(:create_pause)
223+
allow(touch).to receive(:create_pause)
224+
allow(keyboard).to receive(:create_pause)
160225

161-
builder.send('tick', mouse, keyboard)
226+
async_builder.pointer_down(:left, device: 'mouse')
227+
228+
expect(mouse).not_to have_received(:create_pause)
229+
expect(touch).not_to have_received(:create_pause)
230+
expect(keyboard).not_to have_received(:create_pause)
162231
end
163-
end # when adding a tick
232+
end
164233
end # ActionBuilder
165234
end # WebDriver
166235
end # Selenium

0 commit comments

Comments
 (0)