Path: blob/trunk/rb/spec/unit/selenium/webdriver/common/action_builder_spec.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require File.expand_path('../spec_helper', __dir__)2021module Selenium22module WebDriver23describe ActionBuilder do24let(:keyboard) { Interactions.key('key') }25let(:mouse) { Interactions.pointer(:mouse, name: 'mouse') }26let(:bridge) { instance_double(Remote::Bridge).as_null_object }27let(:builder) { described_class.new(bridge, devices: [mouse, keyboard]) }28let(:async_builder) { described_class.new(bridge, devices: [mouse, keyboard], async: true) }2930describe '#initialize' do31it 'does not create input devices when not provided' do32action_builder = described_class.new(bridge)33expect(action_builder.devices).to be_empty34end3536it 'accepts mouse and keyboard with devices keyword' do37action_builder = described_class.new(bridge, devices: [mouse, keyboard])38expect(action_builder.devices).to eq([mouse, keyboard])39end4041it 'accepts multiple devices with devices keyword' do42none = Interactions.none('none')43touch = Interactions.pointer(:touch, name: 'touch')44action_builder = described_class.new(bridge, devices: [mouse, keyboard, none, touch])4546expect(action_builder.devices).to eq([mouse, keyboard, none, touch])47end4849it 'accepts duration' do50action_builder = described_class.new(bridge, duration: 2200)51expect(action_builder.default_move_duration).to eq(2.2)52end5354it 'raises a TypeError if a non InputDevice is passed into devices' do55expect {56described_class.new(bridge, devices: [mouse, keyboard, 'banana'])57}.to raise_error(TypeError)58end59end6061describe '#devices' do62it 'returns Array of devices' do63expect(builder.devices).to include(a_kind_of(Interactions::KeyInput),64a_kind_of(Interactions::PointerInput))65end66end6768describe '#add_pointer_input' do69let(:device) { Interactions.pointer :mouse }7071it 'creates pointer and adds to devices' do72device = builder.add_pointer_input(:touch, 'name')7374expect(builder.devices).to include(device)75end7677it 'adds pauses to match other devices when sync' do78builder.key_down('d', device: 'key')79builder.key_up('d', device: 'key')8081allow(Interactions).to receive(:pointer).and_return(device)82allow(device).to receive(:name)83allow(device).to receive(:create_pause)8485builder.add_pointer_input(:touch, 'name')86expect(device).to have_received(:create_pause).twice87end8889it 'does not add pauses to match other devices when async' do90async_builder.key_down('d', device: 'key')91async_builder.key_up('d', device: 'key')9293allow(Interactions).to receive(:pointer).and_return(device)94allow(device).to receive(:name)95allow(device).to receive(:create_pause)9697async_builder.add_pointer_input(:touch, 'name')98expect(device).not_to have_received(:create_pause)99end100end101102describe '#add_key_input' do103let(:device) { Interactions.key }104105it 'creates keyboard and adds to devices' do106device = builder.add_key_input('name')107108expect(builder.devices).to include(device)109end110111it 'adds pauses to match other devices' do112builder.key_down('d', device: 'key')113builder.key_up('d', device: 'key')114115allow(Interactions).to receive(:key).and_return(device)116allow(device).to receive(:create_pause)117118builder.add_key_input('name')119expect(device).to have_received(:create_pause).twice120end121end122123describe '#device' do124it 'gets device by name' do125expect(builder.device(name: 'mouse')).to eq(mouse)126end127128it 'raises ArgumentError when name not found' do129expect { builder.device(name: 'none', type: Interactions::NONE) }.to raise_error(ArgumentError)130end131132it 'gets device by type' do133expect(builder.device(type: Interactions::POINTER)).to eq(mouse)134end135136it 'returns nil when type not found' do137expect(builder.device(type: Interactions::NONE)).to be_nil138end139end140141describe '#pointer_inputs' do142it 'returns only pointer inputs' do143touch_input = builder.add_pointer_input(:touch, 'touch')144pen_input = builder.add_pointer_input(:pen, 'pen')145builder.add_key_input('key2')146147expect(builder.pointer_inputs).to eq([mouse, touch_input, pen_input])148end149end150151describe '#key_inputs' do152it 'returns only key inputs' do153builder.add_pointer_input(:touch, 'touch')154builder.add_pointer_input(:pen, 'pen')155key_input = builder.add_key_input('key2')156157expect(builder.key_inputs).to eq([keyboard, key_input])158end159end160161describe '#pause' do162it 'creates pause with default duration' do163allow(mouse).to receive :create_pause164165builder.pause(device: mouse)166167expect(mouse).to have_received(:create_pause).with(0)168end169170it 'creates pause with provided duration' do171allow(mouse).to receive :create_pause172173builder.pause(device: mouse, duration: 5)174175expect(mouse).to have_received(:create_pause).with(5)176end177end178179describe '#pauses' do180it 'adds 2 pauses to a pointer device by default' do181allow(mouse).to receive :create_pause182183builder.pauses184185expect(mouse).to have_received(:create_pause).with(0).exactly(2).times186end187188it 'adds multiple pause commands' do189allow(mouse).to receive :create_pause190191builder.pauses(device: mouse, number: 3)192193expect(mouse).to have_received(:create_pause).with(0).exactly(3).times194end195end196197describe '#perform' do198it 'encodes each device' do199allow(mouse).to receive(:encode)200allow(keyboard).to receive(:encode)201202builder.perform203204expect(keyboard).to have_received(:encode)205expect(mouse).to have_received(:encode)206end207208it 'clears all actions' do209allow(builder).to receive(:clear_all_actions)210211builder.perform212213expect(builder).to have_received(:clear_all_actions)214end215216it 'sends non-nil encoded actions to bridge' do217allow(mouse).to receive(:encode).and_return(nil)218allow(keyboard).to receive(:encode).and_return(keyboard: 'encoded')219allow(bridge).to receive(:send_actions)220221builder.perform222expect(bridge).to have_received(:send_actions).with([{keyboard: 'encoded'}])223end224end225226describe '#clear_all_actions' do227it 'sends clear_actions to each devices' do228allow(mouse).to receive(:clear_actions)229allow(keyboard).to receive(:clear_actions)230231builder.clear_all_actions232233expect(mouse).to have_received(:clear_actions)234expect(keyboard).to have_received(:clear_actions)235end236end237238describe '#release_actions' do239it 'sends release actions command to bridge' do240allow(bridge).to receive(:release_actions)241242builder.release_actions243244expect(bridge).to have_received(:release_actions)245end246end247248describe 'tick' do249it 'adds pauses to non-active devices when synchronous' do250touch = builder.add_pointer_input(:touch, 'touch')251allow(mouse).to receive(:create_pause)252allow(touch).to receive(:create_pause)253allow(keyboard).to receive(:create_pause)254255builder.pointer_down(:left, device: 'mouse')256257expect(mouse).not_to have_received(:create_pause)258expect(touch).to have_received(:create_pause)259expect(keyboard).to have_received(:create_pause)260end261262it 'does not create pauses for any devices when asynchronous' do263touch = builder.add_pointer_input(:touch, 'touch')264allow(mouse).to receive(:create_pause)265allow(touch).to receive(:create_pause)266allow(keyboard).to receive(:create_pause)267268async_builder.pointer_down(:left, device: 'mouse')269270expect(mouse).not_to have_received(:create_pause)271expect(touch).not_to have_received(:create_pause)272expect(keyboard).not_to have_received(:create_pause)273end274end275end # ActionBuilder276end # WebDriver277end # Selenium278279280