rmonitor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,259 @@
1
+ require 'rmonitor/helpers/xrandr_read_helpers'
2
+
3
+ describe RMonitor::XRandRReadHelpers do
4
+ include RMonitor::XRandRReadHelpers
5
+
6
+ describe :split_blocks do
7
+ it "should split each block" do
8
+ device_data = <<-X.strip_heredoc
9
+ Screen 0: minimum 320 x 200, current 3840 x 1080, maximum 8192 x 8192
10
+ LVDS1 connected (normal left inverted right x axis y axis)
11
+ 1280x800 60.0 +
12
+ 1024x768 60.0
13
+ 800x600 60.3 56.2
14
+ 640x480 59.9
15
+ VGA1 disconnected (normal left inverted right x axis y axis)
16
+ X
17
+
18
+ split_blocks(device_data).should == [<<-X1.strip_heredoc, <<-X2.strip_heredoc, <<-X3.strip_heredoc]
19
+ Screen 0: minimum 320 x 200, current 3840 x 1080, maximum 8192 x 8192
20
+ X1
21
+ LVDS1 connected (normal left inverted right x axis y axis)
22
+ 1280x800 60.0 +
23
+ 1024x768 60.0
24
+ 800x600 60.3 56.2
25
+ 640x480 59.9
26
+ X2
27
+ VGA1 disconnected (normal left inverted right x axis y axis)
28
+ X3
29
+ end
30
+ end
31
+
32
+ describe :collect_devices do
33
+ it "should filter out blocks not representing a device" do
34
+ blocks = [<<-X1.strip_heredoc, <<-X2.strip_heredoc, <<-X3.strip_heredoc]
35
+ Screen 0: minimum 320 x 200, current 3840 x 1080, maximum 8192 x 8192
36
+ X1
37
+ LVDS1 connected (normal left inverted right x axis y axis)
38
+ 1280x800 60.0 +
39
+ 1024x768 60.0
40
+ 800x600 60.3 56.2
41
+ 640x480 59.9
42
+ X2
43
+ VGA1 disconnected (normal left inverted right x axis y axis)
44
+ X3
45
+
46
+ collect_devices(blocks) == [<<-X1.strip_heredoc, <<-X2.strip_heredoc]
47
+ LVDS1 connected (normal left inverted right x axis y axis)
48
+ 1280x800 60.0 +
49
+ 1024x768 60.0
50
+ 800x600 60.3 56.2
51
+ 640x480 59.9
52
+ X1
53
+ VGA1 disconnected (normal left inverted right x axis y axis)
54
+ X2
55
+ end
56
+ end
57
+
58
+ describe :extract_name do
59
+ it "should extract the name from a device block" do
60
+ block = <<-X.strip_heredoc
61
+ LVDS1 connected (normal left inverted right x axis y axis)
62
+ 1280x800 60.0 +
63
+ 1024x768 60.0
64
+ 800x600 60.3 56.2
65
+ 640x480 59.9
66
+ X
67
+
68
+ extract_name(block).should == "LVDS1"
69
+ end
70
+ end
71
+
72
+ describe :extract_pos do
73
+ context "with an disabled device" do
74
+ it "should return nil" do
75
+ block = <<-X.strip_heredoc
76
+ VGA1 disconnected (normal left inverted right x axis y axis)
77
+ X
78
+
79
+ extract_pos(block).should == nil
80
+ end
81
+ end
82
+
83
+ context "with an enabled device" do
84
+ it "should return the position" do
85
+ block = <<-X.strip_heredoc
86
+ HDMI2 connected 1920x1080+1920+0 (normal left inverted right x axis y axis) 477mm x 268mm
87
+ 1920x1080 60.0*+
88
+ 1280x1024 75.0 60.0
89
+ 1152x864 75.0
90
+ 1024x768 75.1 60.0
91
+ X
92
+
93
+ extract_pos(block).should == "1920x0"
94
+ end
95
+ end
96
+
97
+ context "with an enabled and disconncted device" do
98
+ it "should return the position" do
99
+ block = <<-X.strip_heredoc
100
+ HDMI2 disconnected 1920x1080+1920+0 (normal left inverted right x axis y axis) 477mm x 268mm
101
+ X
102
+
103
+ extract_pos(block).should == "1920x0"
104
+ end
105
+ end
106
+ end
107
+
108
+ describe :extract_enabled do
109
+ it "should return false if the device is disabled" do
110
+ block = <<-X.strip_heredoc
111
+ LVDS1 connected (normal left inverted right x axis y axis)
112
+ 1280x800 60.0 +
113
+ 1024x768 60.0
114
+ 800x600 60.3 56.2
115
+ 640x480 59.9
116
+ X
117
+
118
+ extract_enabled(block).should be_false
119
+ end
120
+
121
+ it "should return true if the device is enabled" do
122
+ block = <<-X.strip_heredoc
123
+ HDMI1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 477mm x 268mm
124
+ 1920x1080 60.0*+
125
+ 1280x1024 75.0 60.0
126
+ 1152x864 75.0
127
+ 1024x768 75.1 60.0
128
+ X
129
+
130
+ extract_enabled(block).should be_true
131
+ end
132
+ end
133
+
134
+ describe :extract_connected do
135
+ it "should return false if the device is disconnected" do
136
+ block = <<-X.strip_heredoc
137
+ VGA1 disconnected (normal left inverted right x axis y axis)
138
+ X
139
+
140
+ extract_connected(block).should be_false
141
+ end
142
+
143
+ it "should return true if the device is connected" do
144
+ block = <<-X.strip_heredoc
145
+ LVDS1 connected (normal left inverted right x axis y axis)
146
+ 1280x800 60.0 +
147
+ 1024x768 60.0
148
+ 800x600 60.3 56.2
149
+ 640x480 59.9
150
+ X
151
+
152
+ extract_connected(block).should be_true
153
+ end
154
+ end
155
+
156
+ describe :extract_configuration do
157
+ context "with no current configuration" do
158
+ it "should return nil" do
159
+ block = <<-X.strip_heredoc
160
+ LVDS1 connected (normal left inverted right x axis y axis)
161
+ 1280x800 60.0 +
162
+ 1024x768 60.0
163
+ 800x600 60.3
164
+ 640x480 59.9
165
+ X
166
+
167
+ extract_configuration(block).should == nil
168
+ end
169
+ end
170
+
171
+ context "with a current configuration" do
172
+ it "should return the current configuration" do
173
+ block = <<-X.strip_heredoc
174
+ HDMI1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 477mm x 268mm
175
+ 1920x1080 60.0*+
176
+ 1280x1024 75.0 60.0
177
+ 1152x864 75.0
178
+ 1024x768 75.1 60.0
179
+ X
180
+
181
+ extract_configuration(block).should == { :mode => "1920x1080",
182
+ :rate => "60.0" }
183
+ end
184
+ end
185
+
186
+ context "with a current configuration containing a rate with more than one decimal" do
187
+ it "should return the current configuration" do
188
+ block = <<-X.strip_heredoc
189
+ HDMI1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 477mm x 268mm
190
+ 1920x1080 60.05*+
191
+ 1280x1024 75.0 60.0
192
+ 1152x864 75.0
193
+ 1024x768 75.1 60.0
194
+ X
195
+
196
+ extract_configuration(block).should == { :mode => "1920x1080",
197
+ :rate => "60.05" }
198
+ end
199
+ end
200
+ end
201
+
202
+ describe :extract_configurations do
203
+ context "with no configurations" do
204
+ it "should return an empty array" do
205
+ block = <<-X.strip_heredoc
206
+ VGA1 disconnected (normal left inverted right x axis y axis)
207
+ X
208
+
209
+ extract_configurations(block).should == []
210
+ end
211
+ end
212
+
213
+ context "with multiple modes" do
214
+ it "should return all possible configurations" do
215
+ block = <<-X.strip_heredoc
216
+ LVDS1 connected (normal left inverted right x axis y axis)
217
+ 1280x800 60.0 +
218
+ 1024x768 60.0
219
+ 800x600 60.3
220
+ 640x480 59.9
221
+ X
222
+
223
+ extract_configurations(block).should == [{ :mode => '1280x800', :rate => '60.0' },
224
+ { :mode => '1024x768', :rate => '60.0' },
225
+ { :mode => '800x600', :rate => '60.3' },
226
+ { :mode => '640x480', :rate => '59.9' }]
227
+ end
228
+ end
229
+
230
+ context "with multiple rates per mode" do
231
+ it "should return all possible configurations" do
232
+ block = <<-X.strip_heredoc
233
+ LVDS1 connected (normal left inverted right x axis y axis)
234
+ 1280x800 60.0 +
235
+ 1024x768 60.0
236
+ 800x600 60.3 56.2
237
+ 640x480 59.9
238
+ X
239
+
240
+ extract_configurations(block).should == [{ :mode => '1280x800', :rate => '60.0' },
241
+ { :mode => '1024x768', :rate => '60.0' },
242
+ { :mode => '800x600', :rate => '60.3' },
243
+ { :mode => '800x600', :rate => '56.2' },
244
+ { :mode => '640x480', :rate => '59.9' }]
245
+ end
246
+ end
247
+
248
+ context "with rates containing more than one decimal" do
249
+ it "should return all possible configurations" do
250
+ block = <<-X.strip_heredoc
251
+ LVDS1 connected (normal left inverted right x axis y axis)
252
+ 1280x800 60.05
253
+ X
254
+
255
+ extract_configurations(block).should == [{ :mode => '1280x800', :rate => '60.05' }]
256
+ end
257
+ end
258
+ end
259
+ end
@@ -0,0 +1,158 @@
1
+ require 'rmonitor/helpers/xrandr_write_helpers'
2
+
3
+ describe RMonitor::XRandRWriteHelpers do
4
+ include RMonitor::XRandRWriteHelpers
5
+
6
+ describe :turn_off do
7
+ it "should return an appropriate XRandR directive for turning off a device" do
8
+ turn_off("HDMI1").should == "--output HDMI1 --off"
9
+ end
10
+ end
11
+
12
+ describe :turn_on do
13
+ it "should return a XRandR directive containing mode and rate when nothing else is specified" do
14
+ options = {
15
+ :mode => "1920x1080",
16
+ :rate => "60.0",
17
+ }
18
+
19
+ turn_on("HDMI1", options).should ==
20
+ "--output HDMI1 --mode 1920x1080 --rate 60.0"
21
+ end
22
+
23
+ it "should return a XRandR directive containing --pos when specified" do
24
+ options = {
25
+ :mode => "1920x1080",
26
+ :rate => "60.0",
27
+ :pos => "1920x0",
28
+ }
29
+
30
+ turn_on("HDMI1", options).should ==
31
+ "--output HDMI1 --mode 1920x1080 --rate 60.0 --pos 1920x0"
32
+ end
33
+
34
+ it "should return a XRandR directive containing --left-of when specified" do
35
+ options = {
36
+ :mode => "1920x1080",
37
+ :rate => "60.0",
38
+ :left_of => "HDMI1",
39
+ }
40
+
41
+ turn_on("HDMI2", options).should ==
42
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --left-of HDMI1"
43
+ end
44
+
45
+ it "should return a XRandR directive containing --right-of when specified" do
46
+ options = {
47
+ :mode => "1920x1080",
48
+ :rate => "60.0",
49
+ :right_of => "HDMI1",
50
+ }
51
+
52
+ turn_on("HDMI2", options).should ==
53
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --right-of HDMI1"
54
+ end
55
+
56
+ it "should return a XRandR directive containing --above when specified" do
57
+ options = {
58
+ :mode => "1920x1080",
59
+ :rate => "60.0",
60
+ :above => "HDMI1",
61
+ }
62
+
63
+ turn_on("HDMI2", options).should ==
64
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --above HDMI1"
65
+ end
66
+
67
+ it "should return a XRandR directive containing --below when specified" do
68
+ options = {
69
+ :mode => "1920x1080",
70
+ :rate => "60.0",
71
+ :below => "HDMI1",
72
+ }
73
+
74
+ turn_on("HDMI2", options).should ==
75
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --below HDMI1"
76
+ end
77
+
78
+ it "should return a XRandR directive containing --same-as when specified" do
79
+ options = {
80
+ :mode => "1920x1080",
81
+ :rate => "60.0",
82
+ :same_as => "LVDS1",
83
+ }
84
+
85
+ turn_on("HDMI2", options).should ==
86
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --same-as LVDS1"
87
+ end
88
+
89
+ it "should return a XRandR directive containing only one option specifying placement when given multiple" do
90
+ options = {
91
+ :mode => "1920x1080",
92
+ :rate => "60.0",
93
+ :pos => "1920x0",
94
+ :left_of => "HDMI1",
95
+ :right_of => "HDMI1",
96
+ :above => "HDMI1",
97
+ :same_as => "LVDS1",
98
+ :below => "HDMI1",
99
+ }
100
+
101
+ turn_on("HDMI2", options).should ==
102
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --pos 1920x0"
103
+ end
104
+
105
+ it "should return a XRandR directive containing --primary when specified" do
106
+ options = {
107
+ :mode => "1920x1080",
108
+ :rate => "60.0",
109
+ :primary => true,
110
+ }
111
+
112
+ turn_on("HDMI2", options).should ==
113
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --primary"
114
+ end
115
+
116
+ it "should return a XRandR directive containing --rotate when specified" do
117
+ options = {
118
+ :mode => "1920x1080",
119
+ :rate => "60.0",
120
+ :rotate => "normal",
121
+ }
122
+
123
+ turn_on("HDMI2", options).should ==
124
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --rotate normal"
125
+ end
126
+
127
+ it "should raise an exception if a wrong --rotate directive is specified" do
128
+ options = {
129
+ :mode => "1920x1080",
130
+ :rate => "60.0",
131
+ :rotate => "this does not exist",
132
+ }
133
+
134
+ lambda { turn_on("HDMI2", options) }.should raise_error
135
+ end
136
+
137
+ it "should return a XRandR directive containing --reflect when specified" do
138
+ options = {
139
+ :mode => "1920x1080",
140
+ :rate => "60.0",
141
+ :reflect => "normal",
142
+ }
143
+
144
+ turn_on("HDMI2", options).should ==
145
+ "--output HDMI2 --mode 1920x1080 --rate 60.0 --reflect normal"
146
+ end
147
+
148
+ it "should raise an exception if a wrong --reflect directive is specified" do
149
+ options = {
150
+ :mode => "1920x1080",
151
+ :rate => "60.0",
152
+ :reflect => "this does not exist",
153
+ }
154
+
155
+ lambda { turn_on("HDMI2", options) }.should raise_error
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,3 @@
1
+ def parse_device(device_data)
2
+ RMonitor::Devices.parse(device_data).first
3
+ end
@@ -0,0 +1,3 @@
1
+ def configuration(&block)
2
+ block
3
+ end
@@ -0,0 +1,25 @@
1
+ class String
2
+ # Strips indentation in heredocs.
3
+ #
4
+ # For example in
5
+ #
6
+ # if options[:usage]
7
+ # puts <<-USAGE.strip_heredoc
8
+ # This command does such and such.
9
+ #
10
+ # Supported options are:
11
+ # -h This message
12
+ # ...
13
+ # USAGE
14
+ # end
15
+ #
16
+ # the user would see the usage message aligned against the left margin.
17
+ #
18
+ # Technically, it looks for the least indented line in the whole string, and removes
19
+ # that amount of leading whitespace.
20
+ def strip_heredoc
21
+ indent = scan(/^[ \t]*(?=\S)/).min
22
+ indent = (indent and indent.size) || 0
23
+ gsub(/^[ \t]{#{indent}}/, '')
24
+ end
25
+ end