Skip to content

Commit

Permalink
fix(battery): disable segment when no batteries
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu authored and JanDeDobbeleer committed Apr 22, 2021
1 parent 3b7b854 commit 44ac09d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
6 changes: 6 additions & 0 deletions src/segment_battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ const (

func (b *batt) enabled() bool {
batteries, err := b.env.getBatteryInfo()

if !b.enabledWhileError(err) {
return false
}

// case on computer without batteries(no error, empty array)
if err == nil && len(batteries) == 0 {
return false
}

b.Battery = &battery.Battery{}
for _, bt := range batteries {
b.Battery.Current += bt.Current
Expand Down
44 changes: 15 additions & 29 deletions src/segment_battery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const (
func TestBatterySegmentSingle(t *testing.T) {
cases := []struct {
Case string
BatteryState battery.State
BatteryLevel float64
Batteries []*battery.Battery
ExpectedString string
ExpectedEnabled bool
ExpectedColor string
Expand All @@ -27,69 +26,63 @@ func TestBatterySegmentSingle(t *testing.T) {
Error error
DisableCharging bool
}{
{Case: "80% charging", BatteryState: battery.Charging, BatteryLevel: 80, ExpectedString: "charging 80", ExpectedEnabled: true},
{Case: "battery full", BatteryState: battery.Full, BatteryLevel: 100, ExpectedString: "charged 100", ExpectedEnabled: true},
{Case: "70% discharging", BatteryState: battery.Discharging, BatteryLevel: 70, ExpectedString: "going down 70", ExpectedEnabled: true},
{Case: "80% charging", Batteries: []*battery.Battery{{Full: 100, State: battery.Charging, Current: 80}}, ExpectedString: "charging 80", ExpectedEnabled: true},
{Case: "battery full", Batteries: []*battery.Battery{{Full: 100, State: battery.Full, Current: 100}}, ExpectedString: "charged 100", ExpectedEnabled: true},
{Case: "70% discharging", Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}}, ExpectedString: "going down 70", ExpectedEnabled: true},
{
Case: "discharging background color",
BatteryState: battery.Discharging,
BatteryLevel: 70,
Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}},
ExpectedString: "going down 70",
ExpectedEnabled: true,
ColorBackground: true,
ExpectedColor: dischargingColor,
},
{
Case: "charging background color",
BatteryState: battery.Charging,
BatteryLevel: 70,
Batteries: []*battery.Battery{{Full: 100, State: battery.Charging, Current: 70}},
ExpectedString: "charging 70",
ExpectedEnabled: true,
ColorBackground: true,
ExpectedColor: chargingColor,
},
{
Case: "charged background color",
BatteryState: battery.Full,
BatteryLevel: 70,
Batteries: []*battery.Battery{{Full: 100, State: battery.Full, Current: 70}},
ExpectedString: "charged 70",
ExpectedEnabled: true,
ColorBackground: true,
ExpectedColor: chargedColor,
},
{
Case: "discharging foreground color",
BatteryState: battery.Discharging,
BatteryLevel: 70,
Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}},
ExpectedString: "going down 70",
ExpectedEnabled: true,
ExpectedColor: dischargingColor,
},
{
Case: "charging foreground color",
BatteryState: battery.Charging,
BatteryLevel: 70,
Batteries: []*battery.Battery{{Full: 100, State: battery.Charging, Current: 70}},
ExpectedString: "charging 70",
ExpectedEnabled: true,
ExpectedColor: chargingColor,
},
{
Case: "charged foreground color",
BatteryState: battery.Full,
BatteryLevel: 70,
Batteries: []*battery.Battery{{Full: 100, State: battery.Full, Current: 70}},
ExpectedString: "charged 70",
ExpectedEnabled: true,
ExpectedColor: chargedColor,
},
{Case: "battery error", DisplayError: true, Error: errors.New("oh snap"), ExpectedString: "oh snap", ExpectedEnabled: true},
{Case: "battery error disabled", Error: errors.New("oh snap")},
{Case: "no batteries", DisplayError: true, Error: &noBatteryError{}},
{Case: "display charging disabled: charging", BatteryState: battery.Charging, DisableCharging: true},
{Case: "display charging disabled: charged", BatteryState: battery.Full, DisableCharging: true},
{Case: "no batteries without error"},
{Case: "display charging disabled: charging", Batteries: []*battery.Battery{{Full: 100, State: battery.Charging}}, DisableCharging: true},
{Case: "display charging disabled: charged", Batteries: []*battery.Battery{{Full: 100, State: battery.Full}}, DisableCharging: true},
{
Case: "display charging disabled: discharging",
BatteryState: battery.Discharging,
BatteryLevel: 70,
Batteries: []*battery.Battery{{Full: 100, State: battery.Discharging, Current: 70}},
ExpectedString: "going down 70",
ExpectedEnabled: true,
DisableCharging: true,
Expand All @@ -98,13 +91,6 @@ func TestBatterySegmentSingle(t *testing.T) {

for _, tc := range cases {
env := &MockedEnvironment{}
batteries := []*battery.Battery{
{
Full: 100,
State: tc.BatteryState,
Current: tc.BatteryLevel,
},
}
props := &properties{
background: "#111111",
foreground: "#ffffff",
Expand All @@ -122,7 +108,7 @@ func TestBatterySegmentSingle(t *testing.T) {
if tc.DisableCharging {
props.values[DisplayCharging] = false
}
env.On("getBatteryInfo", nil).Return(batteries, tc.Error)
env.On("getBatteryInfo", nil).Return(tc.Batteries, tc.Error)
b := &batt{
props: props,
env: env,
Expand Down

0 comments on commit 44ac09d

Please sign in to comment.