github.com/wtfutil/wtf@v0.43.0/modules/power/managed_device_test.go (about) 1 package power 2 3 import ( 4 "reflect" 5 "testing" 6 7 "gotest.tools/assert" 8 ) 9 10 func Test_Refresh(t *testing.T) { 11 // ioreg -c AppleDeviceManagementHIDEventService -r -l 12 data := ` 13 +-o AppleDeviceManagementHIDEventService <class AppleDeviceManagementHIDEventService, id 0x100000892, registered, matched, active, busy 0 (0 ms), retain 8> 14 { 15 "LowBatteryNotificationPercentage" = 2 16 "PrimaryUsagePage" = 65333 17 "BatteryFaultNotificationType" = "TPBatteryFault" 18 "HasBattery" = Yes 19 "VendorID" = 76 20 "VersionNumber" = 0 21 "Built-In" = No 22 "DeviceAddress" = "3c-9b" 23 "WakeReason" = "Button (0x03)" 24 "Product" = "Magic Trackpad 2" 25 "SerialNumber" = "3c-9b" 26 "Transport" = "Bluetooth" 27 "BatteryLowNotificationType" = "TPLowBattery" 28 "ProductID" = 613 29 "DeviceUsagePairs" = ({"DeviceUsagePage"=65333,"DeviceUsage"=11},{"DeviceUsagePage"=65333,"DeviceUsage"=20}) 30 "IOPersonalityPublisher" = "com.apple.driver.AppleTopCaseHIDEventDriver" 31 "BatteryPercent" = 81 32 "MTFW Version" = 944 33 "BD_ADDR" = <3ca6f6cccc9b> 34 "BatteryStatusNotificationType" = "BatteryStatusChanged" 35 "CriticallyLowBatteryNotificationPercentage" = 1 36 "ReportInterval" = 11250 37 "RadioFW Version" = 272 38 "VendorIDSource" = 1 39 "STFW Version" = 2144 40 "CFBundleIdentifier" = "com.apple.driver.AppleTopCaseHIDEventDriver" 41 "IOProviderClass" = "IOHIDInterface" 42 "LocationID" = 1642556667 43 "BluetoothDevice" = Yes 44 "IOClass" = "AppleDeviceManagementHIDEventService" 45 "HIDServiceSupport" = No 46 "CFBundleIdentifierKernel" = "com.apple.driver.AppleTopCaseHIDEventDriver" 47 "ProductIDArray" = (613) 48 "BatteryStatusFlags" = 0 49 "ColorID" = 33 50 "IOMatchCategory" = "IODefaultMatchCategory" 51 "CountryCode" = 0 52 "IOProbeScore" = 7175 53 "PrimaryUsage" = 11 54 "IOGeneralInterest" = "IOCommand is not serializable" 55 "BTFW Version" = 272 56 } 57 58 59 +-o AppleDeviceManagementHIDEventService <class AppleDeviceManagementHIDEventService, id 0x10000091b, registered, matched, active, busy 0 (0 ms), retain 8> 60 { 61 "LowBatteryNotificationPercentage" = 2 62 "PrimaryUsagePage" = 65666 63 "BatteryFaultNotificationType" = "KBBatteryFault" 64 "HasBattery" = Yes 65 "VendorID" = 76 66 "TrustedAccessoryFW Version" = 5666 67 "Built-In" = No 68 "DeviceAddress" = "ac-c5" 69 "VersionNumber" = 0 70 "WakeReason" = "Host (0x01)" 71 "Product" = "Magic Keyboard with Touch ID" 72 "SerialNumber" = "ac-c5" 73 "Transport" = "Bluetooth" 74 "BatteryLowNotificationType" = "KB2LowBattery" 75 "ProductID" = 666 76 "DeviceUsagePairs" = ({"DeviceUsagePage"=65666,"DeviceUsage"=11},{"DeviceUsagePage"=65666,"DeviceUsage"=20}) 77 "IOPersonalityPublisher" = "com.apple.driver.AppleTopCaseDriverV2" 78 "BatteryPercent" = 93 79 "BD_ADDR" = <ac49dbbbbbc5> 80 "BatteryStatusNotificationType" = "BatteryStatusChanged" 81 "CriticallyLowBatteryNotificationPercentage" = 1 82 "ReportInterval" = 11250 83 "RadioFW Version" = 328 84 "VendorIDSource" = 1 85 "STFW Version" = 1024 86 "CFBundleIdentifier" = "com.apple.driver.AppleTopCaseHIDEventDriver" 87 "IOProviderClass" = "IOHIDInterface" 88 "LocationID" = 1642556667 89 "BluetoothDevice" = Yes 90 "IOClass" = "AppleDeviceManagementHIDEventService" 91 "HIDServiceSupport" = No 92 "CFBundleIdentifierKernel" = "com.apple.driver.AppleTopCaseHIDEventDriver" 93 "ProductIDArray" = (666) 94 "BatteryStatusFlags" = 0 95 "ColorID" = 32 96 "IOMatchCategory" = "IODefaultMatchCategory" 97 "CountryCode" = 2 98 "IOProbeScore" = 7175 99 "PrimaryUsage" = 11 100 "IOGeneralInterest" = "IOCommand is not serializable" 101 "BTFW Version" = 328 102 } 103 ` 104 105 manDevices := NewManagedDevices() 106 manDevices.Devices = manDevices.parse(data) 107 108 assert.Equal(t, 2, len(manDevices.Devices)) 109 110 first := manDevices.Devices[0] 111 assert.Equal(t, "Magic Trackpad 2", first.Product()) 112 assert.Equal(t, int64(81), first.BatteryPercent()) 113 assert.Equal(t, true, first.BluetoothDevice()) 114 assert.Equal(t, true, first.HasBattery()) 115 } 116 117 func Test_Add(t *testing.T) { 118 tests := []struct { 119 name string 120 src string 121 expected map[string]string 122 }{ 123 { 124 name: "with empty string", 125 src: "", 126 expected: map[string]string{}, 127 }, 128 { 129 name: "with no delimiter match", 130 src: "catsdogs", 131 expected: map[string]string{}, 132 }, 133 { 134 name: "with valid src", 135 src: "cats=dogs", 136 expected: map[string]string{ 137 "cats": "dogs", 138 }, 139 }, 140 { 141 name: "with valid multiline src", 142 src: "cats=dogs\nx=y", 143 expected: map[string]string{ 144 "cats": "dogs", 145 "x": "y", 146 }, 147 }, 148 } 149 150 for _, tt := range tests { 151 t.Run(tt.name, func(t *testing.T) { 152 manDev := NewManagedDevice() 153 manDev.Add(tt.src) 154 155 if !reflect.DeepEqual(tt.expected, manDev.Attributes) { 156 t.Errorf("\nexpected %v\n got %v", tt.expected, manDev.Attributes) 157 } 158 }) 159 } 160 } 161 162 func Test_Attributes(t *testing.T) { 163 tests := []struct { 164 name string 165 data string 166 }{ 167 { 168 name: "with valid attributes", 169 data: ` 170 "LowBatteryNotificationPercentage" = 2 171 "PrimaryUsagePage" = 65666 172 "BatteryFaultNotificationType" = "KBBatteryFault" 173 "HasBattery" = Yes 174 "VendorID" = 76 175 "TrustedAccessoryFW Version" = 5666 176 "Built-In" = No 177 "DeviceAddress" = "ac-c5" 178 "VersionNumber" = 0 179 "WakeReason" = "Host (0x01)" 180 "Product" = "Magic Keyboard with Touch ID" 181 "SerialNumber" = "ac-c5" 182 "Transport" = "Bluetooth" 183 "BatteryLowNotificationType" = "KB2LowBattery" 184 "ProductID" = 666 185 "DeviceUsagePairs" = ({"DeviceUsagePage"=65666,"DeviceUsage"=11},{"DeviceUsagePage"=65666,"DeviceUsage"=20}) 186 "IOPersonalityPublisher" = "com.apple.driver.AppleTopCaseDriverV2" 187 "BatteryPercent" = 93 188 "BD_ADDR" = <ac49dbbbbbc5> 189 "BatteryStatusNotificationType" = "BatteryStatusChanged" 190 "CriticallyLowBatteryNotificationPercentage" = 1 191 "ReportInterval" = 11250 192 "RadioFW Version" = 328 193 "VendorIDSource" = 1 194 "STFW Version" = 1024 195 "CFBundleIdentifier" = "com.apple.driver.AppleTopCaseHIDEventDriver" 196 "IOProviderClass" = "IOHIDInterface" 197 "LocationID" = 1642556667 198 "BluetoothDevice" = Yes 199 "IOClass" = "AppleDeviceManagementHIDEventService" 200 "HIDServiceSupport" = No 201 "CFBundleIdentifierKernel" = "com.apple.driver.AppleTopCaseHIDEventDriver" 202 "ProductIDArray" = (666) 203 "BatteryStatusFlags" = 0 204 "ColorID" = 32 205 "IOMatchCategory" = "IODefaultMatchCategory" 206 "CountryCode" = 2 207 "IOProbeScore" = 7175 208 "PrimaryUsage" = 11 209 "IOGeneralInterest" = "IOCommand is not serializable" 210 "BTFW Version" = 328 211 `, 212 }, 213 } 214 215 for _, tt := range tests { 216 t.Run(tt.name, func(t *testing.T) { 217 manDev := NewManagedDevice() 218 manDev.Add(tt.data) 219 220 assert.Equal(t, manDev.BatteryPercent(), int64(93)) 221 assert.Equal(t, manDev.BluetoothDevice(), true) 222 assert.Equal(t, manDev.BuiltIn(), false) 223 assert.Equal(t, manDev.HasBattery(), true) 224 assert.Equal(t, manDev.Product(), "Magic Keyboard with Touch ID") 225 }) 226 } 227 } 228 229 func Test_BatteryPercent(t *testing.T) { 230 tests := []struct { 231 name string 232 percent string 233 expected int64 234 }{ 235 { 236 name: "with empty percent", 237 percent: "", 238 expected: -1, 239 }, 240 { 241 name: "with invalid percent", 242 percent: "3a3", 243 expected: -1, 244 }, 245 { 246 name: "with negative percent", 247 percent: "-23", 248 expected: -23, 249 }, 250 { 251 name: "with valid percent", 252 percent: "23", 253 expected: 23, 254 }, 255 } 256 257 for _, tt := range tests { 258 t.Run(tt.name, func(t *testing.T) { 259 manDev := NewManagedDevice() 260 manDev.Attributes["BatteryPercent"] = tt.percent 261 262 actual := manDev.BatteryPercent() 263 assert.Equal(t, tt.expected, actual) 264 }) 265 } 266 }