Debian Codegraph @

obvious/tests/battery.lua

raw symbols SCIP
1 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et:fdm=marker
2
3 local charged_output = { -- {{{
4 ['upower -e'] = [[
5 /org/freedesktop/UPower/devices/line_power_AC
6 /org/freedesktop/UPower/devices/battery_BAT0
7 /org/freedesktop/UPower/devices/DisplayDevice
8 ]],
9
10 ['upower -i /org/freedesktop/UPower/devices/battery_BAT0'] = [[
11 native-path: BAT0
12 vendor: SMP
13 model: DELL TXWRR2C
14 serial: 484
15 power supply: yes
16 updated: Wed 11 Nov 2015 10:59:41 AM CST (73 seconds ago)
17 has history: yes
18 has statistics: yes
19 battery
20 present: yes
21 rechargeable: yes
22 state: fully-charged
23 warning-level: none
24 energy: 58.0216 Wh
25 energy-empty: 0 Wh
26 energy-full: 58.0216 Wh
27 energy-full-design: 58.1555 Wh
28 energy-rate: 0.011158 W
29 voltage: 12.277 V
30 percentage: 100%
31 capacity: 55.5641%
32 technology: lithium-ion
33 icon-name: 'battery-full-charged-symbolic'
34 ]],
35
36 ['acpi -b'] = [[
37 Battery 0: Charged, 100%
38 ]],
39
40 ['acpi'] = [[
41 Battery 0: Charged, 100%
42 ]],
43
44 ['acpitool -b'] = [[
45 Battery #1 : Charged, 100.0%, 5200:00:00
46 ]],
47
48 ['acpitool'] = [[
49 Battery #1 : Charged, 100.0%, 5200:00:00
50 AC adapter : <info not available>
51 Thermal info : <not available>
52 ]],
53
54 ['apm -l -a -m'] = [[
55 100
56 unknown
57 1
58 ]],
59 ['apm'] = [[
60 Battery state: high, 100% remaining
61 A/C adapter state: connected
62 Performance adjustment mode: manual (2531 MHz)
63 ]],
64
65 ['acpiconf -i0'] = [[
66 Design capacity: 50000 mWh
67 Last full capacity: 50000 mWh
68 Technology: secondary (rechargeable)
69 Design voltage: 10000 mV
70 Capacity (warn): 100 mWh
71 Capacity (low): 50 mWh
72 Low/warn granularity: 1 mWh
73 Warn/full granularity: 1 mWh
74 Model number: 1
75 Serial number: 0
76 Type: VBOX
77 OEM info: innotek
78 State: high
79 Remaining capacity: 100%
80 Remaining time: 0:24
81 Present rate: 0 mW
82 Present voltage: 10000 mV
83 ]],
84 } -- }}}
85
86 local discharging_output = { -- {{{
87 ['upower -e'] = charged_output['upower -e'],
88 ['upower -i /org/freedesktop/UPower/devices/battery_BAT0'] = [[
89 native-path: BAT0
90 vendor: SMP
91 model: DELL TXWRR2C
92 serial: 484
93 power supply: yes
94 updated: Wed 11 Nov 2015 11:23:41 AM CST (76 seconds ago)
95 has history: yes
96 has statistics: yes
97 battery
98 present: yes
99 rechargeable: yes
100 state: discharging
101 warning-level: none
102 energy: 26.7346 Wh
103 energy-empty: 0 Wh
104 energy-full: 58.0216 Wh
105 energy-full-design: 58.1555 Wh
106 energy-rate: 45.4465 W
107 voltage: 10.587 V
108 time to empty: 24.3 minutes
109 percentage: 72%
110 capacity: 55.5641%
111 technology: lithium-ion
112 icon-name: 'battery-full-symbolic'
113 History (charge):
114 1447262621 72.000 discharging
115 History (rate):
116 1447262621 45.447 discharging
117 ]],
118 ['acpi -b'] = [[
119 Battery 0: Discharging, 72%, 00:24:33 remaining
120 ]],
121 ['acpi'] = [[
122 Battery 0: Discharging, 72%, 00:24:33 remaining
123 ]],
124 ['acpitool -b'] = [[
125 Battery #1 : Discharging, 72.80%, 00:24:33
126 ]],
127 ['acpitool'] = [[
128 Battery #1 : Discharging, 72.69%, 00:24:33
129 AC adapter : <info not available>
130 Thermal info : <not available>
131 ]],
132 ['apm -l -a -m'] = [[
133 73
134 37
135 0
136 ]],
137 ['apm'] = [[
138 Battery state: high, 72% remaining, 38 minutes life estimate
139 A/C adapter state: not connected
140 Performance adjustment mode: manual (2531 MHz)
141 ]],
142
143 ['acpiconf -i0'] = [[
144 Design capacity: 50000 mWh
145 Last full capacity: 50000 mWh
146 Technology: secondary (rechargeable)
147 Design voltage: 10000 mV
148 Capacity (warn): 100 mWh
149 Capacity (low): 50 mWh
150 Low/warn granularity: 1 mWh
151 Warn/full granularity: 1 mWh
152 Model number: 1
153 Serial number: 0
154 Type: VBOX
155 OEM info: innotek
156 State: discharging
157 Remaining capacity: 72%
158 Remaining time: 0:24
159 Present rate: 44450 mW
160 Present voltage: 10000 mV
161 ]],
162 } -- }}}
163
164 local charging_output = { -- {{{
165 ['upower -e'] = charged_output['upower -e'],
166 ['upower -i /org/freedesktop/UPower/devices/battery_BAT0'] = [[
167 native-path: BAT0
168 vendor: SMP
169 model: DELL TXWRR2C
170 serial: 484
171 power supply: yes
172 updated: Wed 11 Nov 2015 11:29:41 AM CST (88 seconds ago)
173 has history: yes
174 has statistics: yes
175 battery
176 present: yes
177 rechargeable: yes
178 state: charging
179 warning-level: none
180 energy: 22.9408 Wh
181 energy-empty: 0 Wh
182 energy-full: 58.0216 Wh
183 energy-full-design: 58.1555 Wh
184 energy-rate: 21.5126 W
185 voltage: 11.894 V
186 time to full: 0.4 hours
187 percentage: 72%
188 capacity: 55.5641%
189 technology: lithium-ion
190 icon-name: 'battery-full-charging-symbolic'
191 History (rate):
192 1447262981 21.513 charging
193 ]],
194 ['acpi -b'] = [[
195 Battery 0: Charging, 72%, 00:24:33 until charged
196 ]],
197 ['acpi'] = [[
198 Battery 0: Charging, 72%, 00:24:31 until charged
199 ]],
200 ['acpitool -b'] = [[
201 Battery #1 : Charging, 72.96%, 00:24:36
202 ]],
203 ['acpitool'] = [[
204 Battery #1 : Charging, 73.07%, 00:24:36
205 AC adapter : <info not available>
206 Thermal info : <not available>
207 ]],
208 ['apm'] = [[
209 70
210 38
211 1
212 ]],
213 ['acpiconf -i0'] = [[
214 Design capacity: 50000 mWh
215 Last full capacity: 50000 mWh
216 Technology: secondary (rechargeable)
217 Design voltage: 10000 mV
218 Capacity (warn): 100 mWh
219 Capacity (low): 50 mWh
220 Low/warn granularity: 1 mWh
221 Warn/full granularity: 1 mWh
222 Model number: 1
223 Serial number: 0
224 Type: VBOX
225 OEM info: innotek
226 State: charging
227 Remaining capacity: 72%
228 Remaining time: 0:24
229 Present rate: 24100 mW
230 Present voltage: 10000 mV
231
232 ]],
233 } -- }}}
234
235 local charged_output_dual = { -- {{{
236 ['upower -e'] = [[
237 /org/freedesktop/UPower/devices/line_power_AC
238 /org/freedesktop/UPower/devices/battery_BAT0
239 /org/freedesktop/UPower/devices/line_power_AC0
240 /org/freedesktop/UPower/devices/battery_BAT1
241 /org/freedesktop/UPower/devices/DisplayDevice
242 ]],
243 ['upower -i /org/freedesktop/UPower/devices/battery_BAT0'] = [[
244 native-path: BAT0
245 vendor: SMP
246 model: DELL TXWRR2C
247 serial: 484
248 power supply: yes
249 updated: Wed 11 Nov 2015 10:59:41 AM CST (73 seconds ago)
250 has history: yes
251 has statistics: yes
252 battery
253 present: yes
254 rechargeable: yes
255 state: fully-charged
256 warning-level: none
257 energy: 58.0216 Wh
258 energy-empty: 0 Wh
259 energy-full: 58.0216 Wh
260 energy-full-design: 58.1555 Wh
261 energy-rate: 0.011158 W
262 voltage: 12.277 V
263 percentage: 100%
264 capacity: 55.5641%
265 technology: lithium-ion
266 icon-name: 'battery-full-charged-symbolic'
267 ]],
268 ['upower -i /org/freedesktop/UPower/devices/battery_BAT1'] = [[
269 native-path: BAT1
270 vendor: SMP
271 model: DELL TXWRR2C
272 serial: 484
273 power supply: yes
274 updated: Wed 11 Nov 2015 10:59:41 AM CST (73 seconds ago)
275 has history: yes
276 has statistics: yes
277 battery
278 present: yes
279 rechargeable: yes
280 state: fully-charged
281 warning-level: none
282 energy: 58.0216 Wh
283 energy-empty: 0 Wh
284 energy-full: 58.0216 Wh
285 energy-full-design: 58.1555 Wh
286 energy-rate: 0.011158 W
287 voltage: 12.277 V
288 percentage: 100%
289 capacity: 55.5641%
290 technology: lithium-ion
291 icon-name: 'battery-full-charged-symbolic'
292 ]],
293 } -- }}}
294
295 local discharging_output_dual = { -- {{{
296 ['upower -e'] = charged_output_dual['upower -e'],
297 ['upower -i /org/freedesktop/UPower/devices/battery_BAT0'] = [[
298 native-path: BAT0
299 vendor: SMP
300 model: DELL TXWRR2C
301 serial: 484
302 power supply: yes
303 updated: Wed 11 Nov 2015 11:23:41 AM CST (76 seconds ago)
304 has history: yes
305 has statistics: yes
306 battery
307 present: yes
308 rechargeable: yes
309 state: discharging
310 warning-level: none
311 energy: 26.7346 Wh
312 energy-empty: 0 Wh
313 energy-full: 58.0216 Wh
314 energy-full-design: 58.1555 Wh
315 energy-rate: 45.4465 W
316 voltage: 10.587 V
317 time to empty: 24.3 minutes
318 percentage: 72%
319 capacity: 55.5641%
320 technology: lithium-ion
321 icon-name: 'battery-full-symbolic'
322 History (charge):
323 1447262621 72.000 discharging
324 History (rate):
325 1447262621 45.447 discharging
326 ]],
327 ['upower -i /org/freedesktop/UPower/devices/battery_BAT1'] = [[
328 native-path: BAT1
329 vendor: SMP
330 model: DELL TXWRR2C
331 serial: 484
332 power supply: yes
333 updated: Wed 11 Nov 2015 11:23:41 AM CST (76 seconds ago)
334 has history: yes
335 has statistics: yes
336 battery
337 present: yes
338 rechargeable: yes
339 state: discharging
340 warning-level: none
341 energy: 26.7346 Wh
342 energy-empty: 0 Wh
343 energy-full: 58.0216 Wh
344 energy-full-design: 58.1555 Wh
345 energy-rate: 45.4465 W
346 voltage: 10.587 V
347 time to empty: 14.3 minutes
348 percentage: 62%
349 capacity: 55.5641%
350 technology: lithium-ion
351 icon-name: 'battery-full-symbolic'
352 History (charge):
353 1447262621 62.000 discharging
354 History (rate):
355 1447262621 45.447 discharging
356 ]],
357 } -- }}}
358
359 local charging_output_dual = { -- {{{
360 ['upower -e'] = charged_output_dual['upower -e'],
361 ['upower -i /org/freedesktop/UPower/devices/battery_BAT0'] = [[
362 native-path: BAT0
363 vendor: SMP
364 model: DELL TXWRR2C
365 serial: 484
366 power supply: yes
367 updated: Wed 11 Nov 2015 11:29:41 AM CST (88 seconds ago)
368 has history: yes
369 has statistics: yes
370 battery
371 present: yes
372 rechargeable: yes
373 state: charging
374 warning-level: none
375 energy: 22.9408 Wh
376 energy-empty: 0 Wh
377 energy-full: 58.0216 Wh
378 energy-full-design: 58.1555 Wh
379 energy-rate: 21.5126 W
380 voltage: 11.894 V
381 time to full: 0.4 hours
382 percentage: 72%
383 capacity: 55.5641%
384 technology: lithium-ion
385 icon-name: 'battery-full-charging-symbolic'
386 History (rate):
387 1447262981 21.513 charging
388 ]],
389 ['upower -i /org/freedesktop/UPower/devices/battery_BAT1'] = [[
390 native-path: BAT1
391 vendor: SMP
392 model: DELL TXWRR2C
393 serial: 484
394 power supply: yes
395 updated: Wed 11 Nov 2015 11:29:41 AM CST (88 seconds ago)
396 has history: yes
397 has statistics: yes
398 battery
399 present: yes
400 rechargeable: yes
401 state: charging
402 warning-level: none
403 energy: 22.9408 Wh
404 energy-empty: 0 Wh
405 energy-full: 58.0216 Wh
406 energy-full-design: 58.1555 Wh
407 energy-rate: 21.5126 W
408 voltage: 11.894 V
409 time to full: 0.5 hours
410 percentage: 62%
411 capacity: 55.5641%
412 technology: lithium-ion
413 icon-name: 'battery-full-charging-symbolic'
414 History (rate):
415 1447262981 21.513 charging
416 ]],
417 } -- }}}
418
419 -- {{{ mock setup
420 for _, output_table in ipairs { charged_output, discharging_output, charging_output } do
421 for key, output in pairs(output_table) do
422 local trimmed_output = string.match(output, '^%s*(.-)%s*$')
423 output_table[key] = trimmed_output .. '\n'
424 end
425 end
426
427 local mock_output = {
428 charged = charged_output,
429 discharging = discharging_output,
430 charging = charging_output,
431
432 charged_dual = charged_output_dual,
433 discharging_dual = discharging_output_dual,
434 charging_dual = charging_output_dual,
435 }
436
437 local smatch = string.match
438 local itmpfile = io.tmpfile
439 local sformat = string.format
440
441 local ac_state = 'charged'
442
443 local function remove_environment(command, ...)
444 local vars = {}
445 for _, v in ipairs{...} do
446 vars[v] = true
447 end
448
449 local other_vars = ''
450
451 while true do
452 command, count = command:gsub("^(([%w_]+=%S+)%s+)", function(full, env)
453 if not vars[env] then
454 other_vars = other_vars .. full
455 end
456 return ''
457 end)
458
459 if count == 0 then
460 break
461 end
462 end
463
464 return other_vars .. command
465 end
466
467 local function mock_popen(command)
468 command = remove_environment(command, 'LANG=C', 'LC_ALL=C')
469
470 local stderr_redirect_match = smatch(command, '^(.-)%s*2>/dev/null$')
471
472 if stderr_redirect_match then
473 command = stderr_redirect_match
474 end
475
476 local which_match = smatch(command, '^which%s+(%w+)')
477 local output
478
479 if which_match then
480 output = '/usr/bin/' .. which_match
481 else
482 local output_lookup = mock_output[ac_state]
483 output = output_lookup[command]
484 end
485
486 if not output then
487 return nil, 'No output for command ' .. command
488 end
489
490 local fh = assert(itmpfile())
491
492 fh:write(output)
493 assert(fh:seek 'set')
494
495 return fh
496 end
497
498 io.popen = mock_popen
499 -- }}}
500
501 local backends = require 'battery.backends'
502
503 local blacklisted_backends = {
504 ['apm-openbsd'] = true, -- XXX mock uname command
505 apm = true, -- XXX need to find a working version of APM
506 null = true, -- null backend has different state
507 }
508
509 for name, backend_proto in pairs(backends) do
510 if name ~= 'get' and not blacklisted_backends[name] then
511 local backend = backend_proto:configure()
512
513 assert(backend, 'backend ' .. name .. ' should be defined')
514
515 local state = backend:state()
516 local details = backend:details()
517
518 assert(state ~= nil, sformat("backend: %s state should not be nil", name))
519 assert(state.status == 'charged', sformat("backend: %s status should be 'charged', is %s", name, tostring(state.status)))
520 assert(state.charge == 100, sformat("backend: %s charge should be 100, is %d", name, state.charge))
521 assert(type(details) == 'string', sformat("backend: %s details should be a string", name))
522 assert(details ~= '', sformat("backend- %s details should be a non-empty string", name))
523 end
524 end
525
526 ac_state = 'charging'
527
528 for name, backend_proto in pairs(backends) do
529 if name ~= 'get' and not blacklisted_backends[name] then
530 local backend = backend_proto:configure()
531
532 assert(backend, 'backend ' .. name .. ' should be defined')
533
534 local state = backend:state()
535 local details = backend:details()
536
537 assert(state ~= nil, sformat("backend: %s state should not be nil", name))
538 assert(state.status == 'charging', sformat("backend: %s status should be 'charging', is %s", name, tostring(state.status)))
539 assert(state.charge == 72, sformat("backend: %s charge should be 72, is %s", name, tostring(state.charge)))
540 assert(state.time == 24, sformat("backend: %s time should be 24, is %s", name, tostring(state.time)))
541 assert(type(details) == 'string', sformat("backend: %s details should be a string", name))
542 assert(details ~= '', sformat("backend- %s details should be a non-empty string", name))
543 end
544 end
545
546 ac_state = 'discharging'
547
548 for name, backend_proto in pairs(backends) do
549 if name ~= 'get' and not blacklisted_backends[name] then
550 local backend = backend_proto:configure()
551
552 assert(backend, 'backend ' .. name .. ' should be defined')
553
554 local state = backend:state()
555 local details = backend:details()
556
557 assert(state ~= nil, sformat("backend: %s state should not be nil", name))
558 assert(state.status == 'discharging', sformat("backend: %s status should be 'discharging', is %s", name, tostring(state.status)))
559 assert(state.charge == 72, sformat("backend: %s charge should be 72, is %s", name, tostring(state.charge)))
560 assert(state.time == 24, sformat("backend: %s time should be 24, is %s", name, tostring(state.time)))
561 assert(type(details) == 'string', sformat("backend: %s details should be a string", name))
562 assert(details ~= '', sformat("backend- %s details should be a non-empty string", name))
563 end
564 end
565
566 ac_state = 'charged_dual'
567
568 do -- {{{ upower dual output tests (charged)
569 local backend = backends.upower:configure()
570 local name = 'upower'
571
572 assert(backend, 'backend ' .. name .. ' should be defined')
573
574 local state = backend:state()
575 local details = backend:details()
576
577 assert(state ~= nil, sformat("backend: %s state should not be nil", name))
578 assert(state.status == 'charged', sformat("backend: %s status should be 'charged', is %s", name, tostring(state.status)))
579 assert(state.charge == 100, sformat("backend: %s charge should be 100, is %d", name, state.charge))
580 assert(type(details) == 'string', sformat("backend: %s details should be a string", name))
581 assert(details ~= '', sformat("backend- %s details should be a non-empty string", name))
582 end -- }}}
583
584 ac_state = 'charging_dual'
585
586 do -- {{{ upower dual output tests (charging)
587 local backend = backends.upower:configure()
588 local name = 'upower'
589
590 assert(backend, 'backend ' .. name .. ' should be defined')
591
592 local state = backend:state()
593 local details = backend:details()
594
595 assert(state ~= nil, sformat("backend: %s state should not be nil", name))
596 assert(state.status == 'charging', sformat("backend: %s status should be 'charging', is %s", name, tostring(state.status)))
597 assert(state.charge == 72, sformat("backend: %s charge should be 72, is %s", name, tostring(state.charge)))
598 assert(state.time == 24, sformat("backend: %s time should be 24, is %s", name, tostring(state.time)))
599 assert(type(details) == 'string', sformat("backend: %s details should be a string", name))
600 assert(details ~= '', sformat("backend- %s details should be a non-empty string", name))
601 end -- }}}
602
603 ac_state = 'discharging_dual'
604
605 do -- {{{ upower dual output tests (discharging)
606 local backend = backends.upower:configure()
607 local name = 'upower'
608 assert(backend, 'backend ' .. name .. ' should be defined')
609
610 local state1, state2 = backend:state()
611 local details = backend:details()
612
613 assert(state1 ~= nil, sformat("backend: %s 1st state should not be nil", name))
614 assert(state1.status == 'discharging', sformat("backend: %s 1st status should be 'discharging', is %s", name, tostring(state1.status)))
615 assert(state1.charge == 72, sformat("backend: %s 1st charge should be 72, is %s", name, tostring(state1.charge)))
616 assert(state1.time == 24, sformat("backend: %s 1st time should be 24, is %s", name, tostring(state1.time)))
617
618 assert(state2 ~= nil, sformat("backend: %s 2nd state should not be nil", name))
619 assert(state2.status == 'discharging', sformat("backend: %s 2nd status should be 'discharging', is %s", name, tostring(state2.status)))
620 assert(state2.charge == 62, sformat("backend: %s 2nd charge should be 72, is %s", name, tostring(state2.charge)))
621 assert(state2.time == 14, sformat("backend: %s 2nd time should be 24, is %s", name, tostring(state2.time)))
622
623 assert(type(details) == 'string', sformat("backend: %s details should be a string", name))
624 assert(details ~= '', sformat("backend- %s details should be a non-empty string", name))
625 end -- }}}
626
627 -- XXX handle failure
628 -- XXX handle unknown
629 -- XXX handle each backend
630 -- XXX handle backend detection
631 -- XXX handle preferred backend
632 -- XXX apciconf, apm (non-OpenBSD) output
633 -- XXX how to distinguish OpenBSD vs non for output?
634 -- XXX verify apm OpenBSD charged output
635 -- XXX handle lack of command