diff --git a/features/session/termination.feature b/features/session/termination.feature index 135b056..5676eec 100644 --- a/features/session/termination.feature +++ b/features/session/termination.feature @@ -1,6 +1,13 @@ Feature: program termination - Scenario: terminates when requested to quit + Background: Given uhwm is running + + Scenario: terminates on quit request When I tell uhwm to quit Then uhwm must terminate successfully + + Scenario: logs about termination + When I tell uhwm to quit + Then uhwm must terminate successfully + And the output must match /terminat/i diff --git a/lib/uh/wm/manager.rb b/lib/uh/wm/manager.rb index 9943a8d..6f8c232 100644 --- a/lib/uh/wm/manager.rb +++ b/lib/uh/wm/manager.rb @@ -22,6 +22,11 @@ module Uh @events.emit :connected, args: @display end + def disconnect + @display.close + @events.emit :disconnected + end + def grab_key keysym, mod = nil mod_mask = KEY_MODIFIERS[@modifier] mod_mask |= KEY_MODIFIERS[mod] if mod diff --git a/lib/uh/wm/runner.rb b/lib/uh/wm/runner.rb index 73113b8..1afd7a0 100644 --- a/lib/uh/wm/runner.rb +++ b/lib/uh/wm/runner.rb @@ -8,6 +8,7 @@ module Uh runner.register_event_hooks runner.connect_manager runner.run_until { runner.stopped? } + runner.terminate end end @@ -57,6 +58,11 @@ module Uh manager.handle_pending_events until block.call end + def terminate + @env.log "Terminating..." + manager.disconnect + end + private @@ -71,6 +77,7 @@ module Uh @events.on :connected do |display| @env.log "Connected to X server on `#{display}'" end + @events.on(:disconnected) { @env.log "Disconnected from X server" } end def register_layout_hooks diff --git a/spec/uh/wm/manager_spec.rb b/spec/uh/wm/manager_spec.rb index 3f6474f..1ece020 100644 --- a/spec/uh/wm/manager_spec.rb +++ b/spec/uh/wm/manager_spec.rb @@ -1,6 +1,7 @@ module Uh module WM RSpec.describe Manager do + let(:block) { proc { } } let(:events) { Dispatcher.new } let(:modifier) { :mod1 } let(:display) { Display.new } @@ -11,8 +12,6 @@ module Uh end describe '#connect', :xvfb do - let(:block) { proc { } } - it 'opens the display' do expect(manager.display).to receive(:open).and_call_original manager.connect @@ -45,6 +44,20 @@ module Uh end end + describe '#disconnect' do + it 'closes the display' do + expect(display).to receive :close + manager.disconnect + end + + it 'emits :disconnected event' do + allow(display).to receive :close + events.on :disconnected, &block + expect(block).to receive :call + manager.disconnect + end + end + describe '#grab_key' do it 'grabs given key on the display' do expect(manager.display) diff --git a/spec/uh/wm/runner_spec.rb b/spec/uh/wm/runner_spec.rb index 2d605c1..fe65c11 100644 --- a/spec/uh/wm/runner_spec.rb +++ b/spec/uh/wm/runner_spec.rb @@ -129,6 +129,19 @@ module Uh runner.run_until &block end end + + describe '#terminate' do + it 'tells the manager to disconnect' do + expect(runner.manager).to receive :disconnect + runner.terminate + end + + it 'logs about program termination' do + allow(runner.manager).to receive :disconnect + expect(env).to receive(:log).with /terminat/i + runner.terminate + end + end end end end