rbindkeys 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ # -*- coding:utf-8; mode:ruby; -*-
2
+
3
+ require 'rbindkeys'
4
+
5
+ include Rbindkeys
6
+
7
+ describe BindTree do
8
+ context "normal" do
9
+ describe 'BindTree#bind' do
10
+ before do
11
+ @bt = BindTree.new
12
+ end
13
+
14
+ context 'with valid input/output' do
15
+ it "should construct expected tree" do
16
+ input = [1,2]
17
+ @bt.bind input, [2,3]
18
+ input.should == [1,2] # should not destroyable
19
+ @bt.bind [1,0], [2,3]
20
+ @bt.bind [2], [2,4]
21
+ @bt.tree[1][2].payload.output.should == [2,3]
22
+ @bt.tree[1][0].payload.output.should == [2,3]
23
+ @bt.tree[2].payload.output.should == [2,4]
24
+ end
25
+ end
26
+ context 'with duplicate node input' do
27
+ it "should raise DuplicateNodeError" do
28
+ @bt.bind [1,2], [2,3]
29
+ begin
30
+ @bt.bind [1,2], [2,4]
31
+ violated "should raise"
32
+ rescue => e
33
+ e.class.should == BindTree::DuplicateNodeError
34
+ end
35
+ end
36
+ it "should raise DuplicateNodeError" do
37
+ @bt.bind [1,2], [2,3]
38
+ begin
39
+ @bt.bind [1,2,6], [2,4]
40
+ violated "should raise"
41
+ rescue => e
42
+ e.class.should == BindTree::DuplicateNodeError
43
+ end
44
+ end
45
+ end
46
+ end # describe
47
+ end # context
48
+ end # describe
@@ -0,0 +1,74 @@
1
+ # -*- coding:utf-8; mode:ruby; -*-
2
+
3
+ require 'rbindkeys'
4
+
5
+ include Rbindkeys
6
+
7
+ describe BindTree do
8
+ context "BindTree constracted" do
9
+ before do
10
+ @bt = BindTree.new
11
+ @bt.bind [1,2,3], [4,5,6]
12
+ @bt.bind [1,2,0], [6]
13
+ @bt.bind [1,3], [5,6]
14
+ @bt.bind [1,10,0], [55]
15
+ @bt.bind [2], [2]
16
+ @bt.bind [[4],[5]], [6]
17
+ end
18
+
19
+ describe "BindTree#resolve_for_pressed_event" do
20
+ before do
21
+ @ev = Revdev::InputEvent.new nil, Revdev::EV_KEY, 0, 1
22
+ end
23
+ context "with unsorted pressed keys" do
24
+ it "should rase" do
25
+ begin
26
+ @bt.resolve_for_pressed_event(@ev, [2, 1])
27
+ violated "should raise"
28
+ rescue => e
29
+ end
30
+ end
31
+ end
32
+ context "with binded pressed keys" do
33
+ it "should return Arrays and pressing_binds added" do
34
+ @ev.code = 0
35
+ @bt.resolve_for_pressed_event(@ev, [1,2]).output.should == [6]
36
+ expected_pressing_binds = [1, 2, @ev.code]
37
+ @bt.active_key_binds[0].input.should == expected_pressing_binds
38
+
39
+ @ev.code = 2
40
+ @bt.resolve_for_pressed_event(@ev, []).output.should == [2]
41
+ expected_pressing_binds = [@ev.code]
42
+ @bt.active_key_binds[1].input.should == expected_pressing_binds
43
+ end
44
+ end
45
+ context "with no binded pressed keys" do
46
+ it "should return nil" do
47
+ @ev.code = 5
48
+ @bt.resolve_for_pressed_event(@ev, []).should == @bt.default_value
49
+ @bt.resolve_for_pressed_event(@ev, [1]).should == @bt.default_value
50
+ @ev.code = 1
51
+ @bt.resolve_for_pressed_event(@ev, []).should == @bt.default_value
52
+ end
53
+ end
54
+ context "with pressed keys as super set of binded keys" do
55
+ it "should return Arrays" do
56
+ @ev.code = 0
57
+ @bt.resolve_for_pressed_event(@ev, [1,2,4,5]).output.should == [6]
58
+ @bt.resolve_for_pressed_event(@ev, [1,4,5,10]).output.should == [55]
59
+ end
60
+ end
61
+ context "with 2 stroke keybind" do
62
+ it "should return Arrays" do
63
+ @ev.code = 4
64
+ tree = @bt.resolve_for_pressed_event(@ev, [])
65
+ tree.class.should == BindTree
66
+ p tree.tree
67
+
68
+ @ev.code = 5
69
+ tree.resolve_for_pressed_event(@ev, []).output.should == [6]
70
+ end
71
+ end
72
+ end
73
+ end # context
74
+ end # describe
@@ -0,0 +1,63 @@
1
+ # -*- coding:utf-8; mode:ruby; -*-
2
+
3
+ require 'rbindkeys'
4
+
5
+ include Rbindkeys
6
+
7
+ describe BindTree do
8
+ describe '#resolve_for_released_event' do
9
+ before do
10
+ @bt = BindTree.new
11
+ @kb = []
12
+ @kb << KeyBind.new([1,2,3], [4,5,6])
13
+ @kb << KeyBind.new([1,2,0], [6])
14
+ @kb.each do |kb|
15
+ @bt.bind kb.input, kb.output
16
+ end
17
+ end
18
+ context "no pressed binds" do
19
+ before do
20
+ @ev = Revdev::InputEvent.new nil, Revdev::EV_KEY, 0, 0
21
+ end
22
+ it "'s pressed_binds should have empty" do
23
+ @bt.active_key_binds.should be_empty
24
+ @bt.resolve_for_pressed_event(@ev, []).should == @bt.default_value
25
+ @bt.active_key_binds.should be_empty
26
+ end
27
+ end
28
+ context "a pressed bind" do
29
+ before do
30
+ ev = Revdev::InputEvent.new nil, Revdev::EV_KEY, 0, 1
31
+ @bt.resolve_for_pressed_event ev, [1,2,4]
32
+ @ev = Revdev::InputEvent.new nil, Revdev::EV_KEY, 0, 0
33
+ end
34
+ it "'s pressed_binds should empty" do
35
+ @bt.active_key_binds.size.should == 1
36
+ @bt.resolve_for_released_event(@ev, [])[0].output.should == [6]
37
+ @bt.active_key_binds.should be_empty
38
+ end
39
+ end
40
+ context "two pressed binds" do
41
+ before do
42
+ ev = Revdev::InputEvent.new nil, Revdev::EV_KEY, 0, 1
43
+ @bt.resolve_for_pressed_event ev, [1,2,4]
44
+ ev = Revdev::InputEvent.new nil, Revdev::EV_KEY, 3, 1
45
+ @bt.resolve_for_pressed_event ev, [1,2,4]
46
+ @ev = Revdev::InputEvent.new nil, Revdev::EV_KEY, 0, 0
47
+ end
48
+ it "'s pressed_binds should decrease" do
49
+ @bt.active_key_binds.length.should == 2
50
+ @bt.resolve_for_released_event(@ev, [])
51
+ @bt.active_key_binds.length.should == 1
52
+ @ev.code = 3
53
+ @bt.resolve_for_released_event(@ev, [])
54
+ @bt.active_key_binds.should be_empty
55
+ end
56
+ it "'s pressed_binds should empty" do
57
+ @ev.code = 1
58
+ @bt.resolve_for_released_event(@ev, []).length.should == 2
59
+ @bt.active_key_binds.should be_empty
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,81 @@
1
+ # -*- coding:utf-8; mode:ruby; -*-
2
+
3
+ require 'rbindkeys'
4
+ require 'revdev'
5
+
6
+ include Rbindkeys
7
+
8
+ describe CLI do
9
+ describe '#.main' do
10
+ context ', when ARGV is empty,' do
11
+ before do
12
+ @stdout = StringIO.new
13
+ # stub(STDOUT){@stdout}
14
+ ARGV = []
15
+ end
16
+ it 'shoud exit with code 1' do
17
+ expect { CLI::main }.to raise_error do |e|
18
+ e.should be_a SystemExit
19
+ e.status.should == 1
20
+ end
21
+ end
22
+ end
23
+ context ', when ARGV have an argument,' do
24
+ before do
25
+ ARGV = ['foo']
26
+ @observer = mock Observer
27
+ Observer.should_receive(:new){@observer}
28
+ @observer.should_receive(:start){nil}
29
+ end
30
+ it 'should call Observer#new#start' do
31
+ config = CLI::config
32
+ CLI::main
33
+ CLI::config.should == config
34
+ end
35
+ end
36
+ context ', when ARGV have an invalid option (--config),' do
37
+ before do
38
+ ARGV = ['--config']
39
+ end
40
+ it 'should exit with code 1' do
41
+ expect { CLI::main }.to raise_error do |e|
42
+ e.should be_a SystemExit
43
+ e.status.should == 1
44
+ end
45
+ end
46
+ end
47
+ context ', when ARGV have an option (--config) and an event device,' do
48
+ before do
49
+ @config = 'a_config_file'
50
+ ARGV = ['--config', @config,'foodev']
51
+ @observer = mock Observer
52
+ Observer.should_receive(:new){@observer}
53
+ @observer.should_receive(:start){nil}
54
+ end
55
+ it 'should call Observer#new#start ' do
56
+ CLI::main
57
+ CLI::config.should == @config
58
+ end
59
+ end
60
+ context ', when ARGV have an option (--evdev-list),' do
61
+ before do
62
+ ARGV = ['--evdev-list']
63
+ @evdev = mock Revdev::EventDevice
64
+ @id = mock Object
65
+ @evdev.stub(:device_name){"foo"}
66
+ @evdev.stub(:device_id){@id}
67
+ @id.stub(:hr_bustype){'bar'}
68
+ Revdev::EventDevice.stub(:new){@evdev}
69
+ Dir.should_receive(:glob).with(CLI::EVDEVS).
70
+ and_return(['/dev/input/event4','/dev/input/event2',
71
+ '/dev/input/event13'])
72
+ # @stdout = StringIO.new
73
+ # $stdout = @stdout
74
+ end
75
+ it 'should pring device info' do
76
+ CLI::main
77
+ # @stdout.string.should match(%r!^/dev/input/event2!)
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,57 @@
1
+ # -*- coding:utf-8; mode:ruby; -*-
2
+
3
+ require 'rbindkeys'
4
+ require 'revdev'
5
+
6
+ include Rbindkeys
7
+
8
+ describe DeviceOperator do
9
+ before do
10
+ @dev = mock Device
11
+ @vdev = mock VirtualDevice
12
+ @operator = DeviceOperator.new @dev, @vdev
13
+ end
14
+
15
+ describe "#send_key" do
16
+ context "with a code and a press value" do
17
+ before do
18
+ @code = 10
19
+ @value = 1
20
+ end
21
+ it "should call #send_event" do
22
+ @operator.should_receive(:send_event).with(Revdev::EV_KEY,@code,@value)
23
+ @operator.send_key @code, @value
24
+ end
25
+ end
26
+ end
27
+
28
+ context "#send_event" do
29
+ context "with normal values" do
30
+ before do
31
+ @type = Revdev::EV_KEY
32
+ @code = 10
33
+ @value = 1
34
+ end
35
+ it "should send a #write_input_event to @vdev" do
36
+ @vdev.should_receive(:write_input_event) do |ie|
37
+ ie.code.should == @code
38
+ 10
39
+ end
40
+ @operator.send_event @type, @code, @value
41
+ end
42
+ end
43
+ context "with an input event" do
44
+ before do
45
+ @ie = Revdev::InputEvent.new nil, Revdev::EV_KEY, 10, 1
46
+ end
47
+ it "should send a #write_input_event to @vdev" do
48
+ @vdev.should_receive(:write_input_event) do |ie|
49
+ ie.should == @ie
50
+ 10
51
+ end
52
+ @operator.send_event @ie
53
+ end
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,57 @@
1
+ # -*- coding:utf-8; mode:ruby; -*-
2
+
3
+ require 'rbindkeys'
4
+
5
+ include Rbindkeys
6
+
7
+ describe FixResolver do
8
+
9
+ describe ".instance" do
10
+ context "with different value" do
11
+ it "should return different instances" do
12
+ f = FixResolver.instance :foo
13
+ g = FixResolver.instance :bar
14
+ f.equal?(g).should be_false
15
+ end
16
+ it "should return different instances" do
17
+ f = FixResolver.instance :foo
18
+ g = FixResolver.instance :foo
19
+ f.equal?(g).should be_true
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '.new' do
25
+ context 'with any arg' do
26
+ it 'should raise an exception' do
27
+ lambda{FixResolver.new}.should raise_error
28
+ lambda{FixResolver.new :foo}.should raise_error
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#bind' do
34
+ before do
35
+ @resolver = FixResolver.instance :foo
36
+ end
37
+ context 'with any args' do
38
+ it 'should raise an exception' do
39
+ lambda{@resolver.bind 1, 2}.should raise_error
40
+ lambda{@resolver.bind [], []}.should raise_error
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#resolve' do
46
+ before do
47
+ @resolver = FixResolver.instance :foo
48
+ end
49
+ context 'with any args' do
50
+ it 'should return :foo' do
51
+ @resolver.resolve(1, []).should == :foo
52
+ @resolver.resolve(3, [1,2]).should == :foo
53
+ end
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,201 @@
1
+ # -*- coding:utf-8; mode:ruby; -*-
2
+
3
+ require 'rbindkeys'
4
+ require 'fileutils'
5
+ require 'revdev'
6
+
7
+ include Rbindkeys
8
+
9
+ $tmp_dir = 'tmp'
10
+
11
+ describe KeyEventHandler do
12
+ before :all do
13
+ FileUtils.mkdir $tmp_dir
14
+ end
15
+
16
+ after :all do
17
+ FileUtils.rm_r $tmp_dir
18
+ end
19
+
20
+ before do
21
+ @ope = mock DeviceOperator
22
+ end
23
+
24
+ describe "bind keys methods" do
25
+
26
+ before do
27
+ @defval = :hoge
28
+ @bind_set = []
29
+ @res = mock Rbindkeys::BindResolver
30
+
31
+ # define stubs
32
+ @res.stub(:bind) do |i, o|
33
+ @bind_set << [i, o]
34
+ end
35
+ @res.stub(:resolve) do |input, pressed_key_set|
36
+ if input == 10
37
+ BindResolver.new
38
+ else
39
+ @defval
40
+ end
41
+ end
42
+ @res.stub(:just_resolve) do |input, pressed_key_set|
43
+ if input == 10
44
+ BindResolver.new
45
+ else
46
+ nil
47
+ end
48
+ end
49
+ @res.stub(:kind_of?) do |klass|
50
+ klass == Rbindkeys::BindResolver
51
+ end
52
+ Rbindkeys::BindResolver.stub!(:new).and_return(@res)
53
+
54
+ @handler = KeyEventHandler.new @ope
55
+ end
56
+
57
+ describe KeyEventHandler, "#pre_bind_key" do
58
+ context "with a bind" do
59
+ it "map the bind to @pre_bind_resolver" do
60
+ @handler.pre_bind_key 1, 0
61
+ @handler.pre_bind_resolver[1].should == 0
62
+ end
63
+ end
64
+ context "with duplicated binds" do
65
+ it "should raise a DuplicatedNodeError" do
66
+ @handler.pre_bind_key 1, 0
67
+ lambda{ @handler.pre_bind_key(1, 2) }.should raise_error(DuplicateNodeError)
68
+ end
69
+ end
70
+ end
71
+
72
+ describe KeyEventHandler, "#bind_key" do
73
+ context "with two Fixnum" do
74
+ it "construct @bind_set" do
75
+ @handler.bind_key 0, 1
76
+ @bind_set.should == [[[0],[1]]]
77
+ end
78
+ end
79
+ context "with two Arrays" do
80
+ it "construct @bind_set" do
81
+ @handler.bind_key [0, 1], [2, 3]
82
+ @bind_set.should == [[[0, 1], [2, 3]]]
83
+ end
84
+ end
85
+ context "with an Array and a KeyResolver" do
86
+ it "construct @bind_set" do
87
+ @handler.bind_key [0,1], @res
88
+ @bind_set.should == [[[0,1], @res]]
89
+ end
90
+ end
91
+ context "with an Array and a block" do
92
+ it "construct @bind_set" do
93
+ @handler.bind_key [0,1] do
94
+ p 'foo'
95
+ end
96
+ @bind_set.first[1].class.should == Proc
97
+ end
98
+ end
99
+ context "with mix classes" do
100
+ it "construct @bind_set" do
101
+ @handler.bind_key 1, [2, 3]
102
+ @handler.bind_key [2, 3], 4
103
+ @bind_set.should == [[[1], [2, 3]], [[2, 3], [4]]]
104
+ end
105
+ end
106
+ context "with invalid args" do
107
+ it "raise some error" do
108
+ lambda{@handler.bind_key [1], [[[2]]]}.should raise_error
109
+ end
110
+ end
111
+ end
112
+
113
+ describe KeyEventHandler, "#bind_prefix_key" do
114
+ context "with a new prefix key" do
115
+ it "construct @bind_set" do
116
+ @handler.bind_prefix_key [0,1] do
117
+ @handler.bind_key 2, 3
118
+ end
119
+ @bind_set.length.should == 2
120
+ @bind_set.include?([[2],[3]]).should be_true
121
+ end
122
+ end
123
+ context "with a existing prefix key" do
124
+ it "should construct @bind_set" do
125
+ @handler.bind_prefix_key [0,10] do
126
+ @handler.bind_key 2, 3
127
+ end
128
+ @bind_set.length.should == 1
129
+ @bind_set.include?([[2],[3]]).should be_true
130
+ end
131
+ end
132
+ end
133
+
134
+ describe KeyEventHandler, "#window" do
135
+ context 'with invalid arg' do
136
+ it 'should raise ArgumentError' do
137
+ lambda { @handler.window(nil, "foo") }.should raise_error
138
+ lambda { @handler.window(nil, :class => "bar") }.should raise_error
139
+ end
140
+ end
141
+ context 'with nil and a regex' do
142
+ it 'should return the BindResolver and added it to @window_bind_resolver_map' do
143
+ size = @handler.window_bind_resolver_map.size
144
+ res = @handler.window(nil, /foo/)
145
+ res.should be_a BindResolver
146
+ (@handler.window_bind_resolver_map.size - size).should == 1
147
+ p '--------------'
148
+ Hash[*@handler.window_bind_resolver_map.flatten].value?(res).should be_true
149
+ end
150
+ end
151
+ context 'with nil and a Hash having :class key' do
152
+ it 'should return the BindResolver and added it to @window_bind_resolver_map' do
153
+ size = @handler.window_bind_resolver_map.size
154
+ res = @handler.window(nil, :class => /foo/)
155
+ res.should be_a BindResolver
156
+ (@handler.window_bind_resolver_map.size - size).should == 1
157
+ Hash[*@handler.window_bind_resolver_map.flatten].value?(res).should be_true
158
+ end
159
+ end
160
+ context 'with a BindResolver and a regex' do
161
+ before do
162
+ @arg_resolver = mock BindResolver
163
+ @arg_resolver.stub(:kind_of?).and_return(false)
164
+ @arg_resolver.stub(:kind_of?).with(BindResolver).and_return(true)
165
+ end
166
+ it 'should return the BindResolver and added it to @window_bind_resolver_map' do
167
+ size = @handler.window_bind_resolver_map.size
168
+ res = @handler.window(@arg_resolver, /foo/)
169
+ res.should be_a BindResolver
170
+ (@handler.window_bind_resolver_map.size - size).should == 1
171
+ Hash[*@handler.window_bind_resolver_map.flatten].value?(res).should be_true
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "KeyEventHandler#load_config" do
177
+ before :all do
178
+ @config = File.join $tmp_dir, 'config'
179
+ open @config, 'w' do |f|
180
+ f.write <<-EOF
181
+ pre_bind_key KEY_CAPSLOCK, KEY_LEFTCTRL
182
+ bind_key [KEY_LEFTCTRL,KEY_F], KEY_RIGHT
183
+ bind_key [KEY_LEFTCTRL,KEY_W], [KEY_LEFTCTRL,KEY_X]
184
+ bind_prefix_key [KEY_LEFTCTRL,KEY_X] do
185
+ bind_key KEY_K, [KEY_LEFTCTRL, KEY_W]
186
+ end
187
+ EOF
188
+ end
189
+ end
190
+ it "construct @pre_bind_key_set and @bind_key_set" do
191
+ @handler.load_config @config
192
+ @handler.pre_bind_resolver.size.should == 1
193
+ @bind_set.length.should == 4
194
+ @bind_set[0][1].should == [Revdev::KEY_RIGHT]
195
+ @bind_set[1][1].should == [Revdev::KEY_LEFTCTRL, Revdev::KEY_X]
196
+ @bind_set[2][1].should == @res
197
+ @bind_set[3][1].should == [Revdev::KEY_LEFTCTRL, Revdev::KEY_W]
198
+ end
199
+ end
200
+ end
201
+ end