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