-
Notifications
You must be signed in to change notification settings - Fork 43
/
event_dispatcher_test.rb
executable file
·97 lines (72 loc) · 2.14 KB
/
event_dispatcher_test.rb
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
#! /usr/bin/env rspec
require_relative "test_helper"
require "ui/event_dispatcher"
class DispatcherTestDialog
include Yast::UIShortcuts
include UI::EventDispatcher
Yast.import "UI"
def ok_handler
finish_dialog(true)
end
def again_handler
@again_handler_called = true
end
def again_handler_called?
!!@again_handler_called
end
end
class DispatcherUserInputTestDialog
include Yast::UIShortcuts
include UI::EventDispatcher
Yast.import "UI"
def user_input
Yast::UI.TimeoutUserInput(1000)
end
end
class DispatcherEventHandlerTestDialog
include Yast::UIShortcuts
include UI::EventDispatcher
Yast.import "UI"
def event_handler(_input)
finish_dialog(:always_cancel)
end
end
describe UI::EventDispatcher do
subject { DispatcherTestDialog.new }
def mock_ui_events(*events)
allow(Yast::UI).to receive(:UserInput).and_return(*events)
end
describe "#event_loop" do
it "dispatch call for widget with id 'i' to i_handler method" do
mock_ui_events(:again, :cancel)
subject.event_loop
expect(subject.again_handler_called?).to eq(true)
end
it "returns value of first handler which call finish_dialog" do
mock_ui_events(:ok, :again, :cancel)
expect(subject.event_loop).to eq(true)
expect(subject.again_handler_called?).to eq(false)
end
it "raise exception if handler is not defined" do
mock_ui_events(:unknown)
expect { subject.event_loop }.to raise_error(RuntimeError)
end
it "uses user_input to get input" do
dialog = DispatcherUserInputTestDialog.new
expect(Yast::UI).to receive(:TimeoutUserInput).with(1000).and_return(:cancel)
expect(Yast::UI).to_not receive(:UserInput)
dialog.event_loop
end
it "uses custom event_handler to manage events" do
mock_ui_events(:ok)
dialog = DispatcherEventHandlerTestDialog.new
expect(dialog.event_loop).to eq(:always_cancel)
end
end
describe "#cancel_handler" do
it "provides default action for cancel operation leading to exit of dialog with nil" do
mock_ui_events(:cancel)
expect(subject.event_loop).to eq(nil)
end
end
end