|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:flutter_device_apps_platform_interface/flutter_device_apps_platform_interface.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group('AppInfo', () { |
| 8 | + group('constructor', () { |
| 9 | + test('creates instance with all null values', () { |
| 10 | + const appInfo = AppInfo(); |
| 11 | + |
| 12 | + expect(appInfo.packageName, isNull); |
| 13 | + expect(appInfo.appName, isNull); |
| 14 | + expect(appInfo.versionName, isNull); |
| 15 | + expect(appInfo.versionCode, isNull); |
| 16 | + expect(appInfo.firstInstallTime, isNull); |
| 17 | + expect(appInfo.lastUpdateTime, isNull); |
| 18 | + expect(appInfo.isSystem, isNull); |
| 19 | + expect(appInfo.iconBytes, isNull); |
| 20 | + expect(appInfo.category, isNull); |
| 21 | + expect(appInfo.targetSdkVersion, isNull); |
| 22 | + expect(appInfo.minSdkVersion, isNull); |
| 23 | + expect(appInfo.enabled, isNull); |
| 24 | + expect(appInfo.processName, isNull); |
| 25 | + expect(appInfo.installLocation, isNull); |
| 26 | + }); |
| 27 | + |
| 28 | + test('creates instance with all values', () { |
| 29 | + final iconBytes = Uint8List.fromList([1, 2, 3]); |
| 30 | + final firstInstallTime = DateTime(2024, 1, 15); |
| 31 | + final lastUpdateTime = DateTime(2024, 6, 20); |
| 32 | + |
| 33 | + final appInfo = AppInfo( |
| 34 | + packageName: 'com.example.app', |
| 35 | + appName: 'Example App', |
| 36 | + versionName: '1.0.0', |
| 37 | + versionCode: 10, |
| 38 | + firstInstallTime: firstInstallTime, |
| 39 | + lastUpdateTime: lastUpdateTime, |
| 40 | + isSystem: false, |
| 41 | + iconBytes: iconBytes, |
| 42 | + category: 3, |
| 43 | + targetSdkVersion: 33, |
| 44 | + minSdkVersion: 21, |
| 45 | + enabled: true, |
| 46 | + processName: 'com.example.app', |
| 47 | + installLocation: 0, |
| 48 | + ); |
| 49 | + |
| 50 | + expect(appInfo.packageName, 'com.example.app'); |
| 51 | + expect(appInfo.appName, 'Example App'); |
| 52 | + expect(appInfo.versionName, '1.0.0'); |
| 53 | + expect(appInfo.versionCode, 10); |
| 54 | + expect(appInfo.firstInstallTime, firstInstallTime); |
| 55 | + expect(appInfo.lastUpdateTime, lastUpdateTime); |
| 56 | + expect(appInfo.isSystem, false); |
| 57 | + expect(appInfo.iconBytes, iconBytes); |
| 58 | + expect(appInfo.category, 3); |
| 59 | + expect(appInfo.targetSdkVersion, 33); |
| 60 | + expect(appInfo.minSdkVersion, 21); |
| 61 | + expect(appInfo.enabled, true); |
| 62 | + expect(appInfo.processName, 'com.example.app'); |
| 63 | + expect(appInfo.installLocation, 0); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + group('fromMap', () { |
| 68 | + test('parses empty map', () { |
| 69 | + final appInfo = AppInfo.fromMap({}); |
| 70 | + |
| 71 | + expect(appInfo.packageName, isNull); |
| 72 | + expect(appInfo.appName, isNull); |
| 73 | + expect(appInfo.versionName, isNull); |
| 74 | + expect(appInfo.versionCode, isNull); |
| 75 | + expect(appInfo.firstInstallTime, isNull); |
| 76 | + expect(appInfo.lastUpdateTime, isNull); |
| 77 | + expect(appInfo.isSystem, isNull); |
| 78 | + expect(appInfo.iconBytes, isNull); |
| 79 | + expect(appInfo.category, isNull); |
| 80 | + expect(appInfo.targetSdkVersion, isNull); |
| 81 | + expect(appInfo.minSdkVersion, isNull); |
| 82 | + expect(appInfo.enabled, isNull); |
| 83 | + expect(appInfo.processName, isNull); |
| 84 | + expect(appInfo.installLocation, isNull); |
| 85 | + }); |
| 86 | + |
| 87 | + test('parses all string fields correctly', () { |
| 88 | + final map = <String, Object?>{ |
| 89 | + 'packageName': 'com.example.app', |
| 90 | + 'appName': 'Example App', |
| 91 | + 'versionName': '2.1.0', |
| 92 | + 'processName': 'com.example.process', |
| 93 | + }; |
| 94 | + |
| 95 | + final appInfo = AppInfo.fromMap(map); |
| 96 | + |
| 97 | + expect(appInfo.packageName, 'com.example.app'); |
| 98 | + expect(appInfo.appName, 'Example App'); |
| 99 | + expect(appInfo.versionName, '2.1.0'); |
| 100 | + expect(appInfo.processName, 'com.example.process'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('parses integer fields from int values', () { |
| 104 | + final map = <String, Object?>{ |
| 105 | + 'versionCode': 42, |
| 106 | + 'category': 5, |
| 107 | + 'targetSdkVersion': 34, |
| 108 | + 'minSdkVersion': 23, |
| 109 | + 'installLocation': 1, |
| 110 | + }; |
| 111 | + |
| 112 | + final appInfo = AppInfo.fromMap(map); |
| 113 | + |
| 114 | + expect(appInfo.versionCode, 42); |
| 115 | + expect(appInfo.category, 5); |
| 116 | + expect(appInfo.targetSdkVersion, 34); |
| 117 | + expect(appInfo.minSdkVersion, 23); |
| 118 | + expect(appInfo.installLocation, 1); |
| 119 | + }); |
| 120 | + |
| 121 | + test('parses integer fields from string values', () { |
| 122 | + final map = <String, Object?>{ |
| 123 | + 'versionCode': '42', |
| 124 | + 'category': '5', |
| 125 | + 'targetSdkVersion': '34', |
| 126 | + 'minSdkVersion': '23', |
| 127 | + 'installLocation': '1', |
| 128 | + }; |
| 129 | + |
| 130 | + final appInfo = AppInfo.fromMap(map); |
| 131 | + |
| 132 | + expect(appInfo.versionCode, 42); |
| 133 | + expect(appInfo.category, 5); |
| 134 | + expect(appInfo.targetSdkVersion, 34); |
| 135 | + expect(appInfo.minSdkVersion, 23); |
| 136 | + expect(appInfo.installLocation, 1); |
| 137 | + }); |
| 138 | + |
| 139 | + test('handles invalid integer strings gracefully', () { |
| 140 | + final map = <String, Object?>{ |
| 141 | + 'versionCode': 'invalid', |
| 142 | + 'category': 'not_a_number', |
| 143 | + 'targetSdkVersion': '', |
| 144 | + }; |
| 145 | + |
| 146 | + final appInfo = AppInfo.fromMap(map); |
| 147 | + |
| 148 | + expect(appInfo.versionCode, isNull); |
| 149 | + expect(appInfo.category, isNull); |
| 150 | + expect(appInfo.targetSdkVersion, isNull); |
| 151 | + }); |
| 152 | + |
| 153 | + test('parses boolean fields from bool values', () { |
| 154 | + final map = <String, Object?>{ |
| 155 | + 'isSystem': true, |
| 156 | + 'enabled': false, |
| 157 | + }; |
| 158 | + |
| 159 | + final appInfo = AppInfo.fromMap(map); |
| 160 | + |
| 161 | + expect(appInfo.isSystem, true); |
| 162 | + expect(appInfo.enabled, false); |
| 163 | + }); |
| 164 | + |
| 165 | + test('parses boolean fields from string values', () { |
| 166 | + final map = <String, Object?>{ |
| 167 | + 'isSystem': 'true', |
| 168 | + 'enabled': 'false', |
| 169 | + }; |
| 170 | + |
| 171 | + final appInfo = AppInfo.fromMap(map); |
| 172 | + |
| 173 | + expect(appInfo.isSystem, true); |
| 174 | + expect(appInfo.enabled, false); |
| 175 | + }); |
| 176 | + |
| 177 | + test('handles invalid boolean strings gracefully', () { |
| 178 | + final map = <String, Object?>{ |
| 179 | + 'isSystem': 'yes', |
| 180 | + 'enabled': 'no', |
| 181 | + }; |
| 182 | + |
| 183 | + final appInfo = AppInfo.fromMap(map); |
| 184 | + |
| 185 | + expect(appInfo.isSystem, isNull); |
| 186 | + expect(appInfo.enabled, isNull); |
| 187 | + }); |
| 188 | + |
| 189 | + test('parses DateTime fields from milliseconds', () { |
| 190 | + final int firstInstallMs = DateTime(2024, 1, 15).millisecondsSinceEpoch; |
| 191 | + final int lastUpdateMs = DateTime(2024, 6, 20).millisecondsSinceEpoch; |
| 192 | + |
| 193 | + final map = <String, Object?>{ |
| 194 | + 'firstInstallTime': firstInstallMs, |
| 195 | + 'lastUpdateTime': lastUpdateMs, |
| 196 | + }; |
| 197 | + |
| 198 | + final appInfo = AppInfo.fromMap(map); |
| 199 | + |
| 200 | + expect(appInfo.firstInstallTime, DateTime(2024, 1, 15)); |
| 201 | + expect(appInfo.lastUpdateTime, DateTime(2024, 6, 20)); |
| 202 | + }); |
| 203 | + |
| 204 | + test('parses DateTime fields from string milliseconds', () { |
| 205 | + final int firstInstallMs = DateTime(2024, 3, 10).millisecondsSinceEpoch; |
| 206 | + final int lastUpdateMs = DateTime(2024, 8, 5).millisecondsSinceEpoch; |
| 207 | + |
| 208 | + final map = <String, Object?>{ |
| 209 | + 'firstInstallTime': firstInstallMs.toString(), |
| 210 | + 'lastUpdateTime': lastUpdateMs.toString(), |
| 211 | + }; |
| 212 | + |
| 213 | + final appInfo = AppInfo.fromMap(map); |
| 214 | + |
| 215 | + expect(appInfo.firstInstallTime, DateTime(2024, 3, 10)); |
| 216 | + expect(appInfo.lastUpdateTime, DateTime(2024, 8, 5)); |
| 217 | + }); |
| 218 | + |
| 219 | + test('handles invalid DateTime strings gracefully', () { |
| 220 | + final map = <String, Object?>{ |
| 221 | + 'firstInstallTime': 'not_a_timestamp', |
| 222 | + 'lastUpdateTime': '', |
| 223 | + }; |
| 224 | + |
| 225 | + final appInfo = AppInfo.fromMap(map); |
| 226 | + |
| 227 | + expect(appInfo.firstInstallTime, isNull); |
| 228 | + expect(appInfo.lastUpdateTime, isNull); |
| 229 | + }); |
| 230 | + |
| 231 | + test('parses iconBytes from List<int>', () { |
| 232 | + final iconData = <int>[255, 216, 255, 224, 0, 16]; |
| 233 | + final map = <String, Object?>{ |
| 234 | + 'iconBytes': iconData, |
| 235 | + }; |
| 236 | + |
| 237 | + final appInfo = AppInfo.fromMap(map); |
| 238 | + |
| 239 | + expect(appInfo.iconBytes, isA<Uint8List>()); |
| 240 | + expect(appInfo.iconBytes, [255, 216, 255, 224, 0, 16]); |
| 241 | + }); |
| 242 | + |
| 243 | + test('handles non-List iconBytes gracefully', () { |
| 244 | + final map = <String, Object?>{ |
| 245 | + 'iconBytes': 'not_a_list', |
| 246 | + }; |
| 247 | + |
| 248 | + final appInfo = AppInfo.fromMap(map); |
| 249 | + |
| 250 | + expect(appInfo.iconBytes, isNull); |
| 251 | + }); |
| 252 | + |
| 253 | + test('handles null iconBytes gracefully', () { |
| 254 | + final map = <String, Object?>{ |
| 255 | + 'iconBytes': null, |
| 256 | + }; |
| 257 | + |
| 258 | + final appInfo = AppInfo.fromMap(map); |
| 259 | + |
| 260 | + expect(appInfo.iconBytes, isNull); |
| 261 | + }); |
| 262 | + |
| 263 | + test('parses complete map with all fields', () { |
| 264 | + final int firstInstallMs = DateTime(2024).millisecondsSinceEpoch; |
| 265 | + final int lastUpdateMs = DateTime(2024, 12, 31).millisecondsSinceEpoch; |
| 266 | + final iconData = <int>[1, 2, 3, 4, 5]; |
| 267 | + |
| 268 | + final map = <String, Object?>{ |
| 269 | + 'packageName': 'com.test.fullapp', |
| 270 | + 'appName': 'Full Test App', |
| 271 | + 'versionName': '3.2.1', |
| 272 | + 'versionCode': 321, |
| 273 | + 'firstInstallTime': firstInstallMs, |
| 274 | + 'lastUpdateTime': lastUpdateMs, |
| 275 | + 'isSystem': false, |
| 276 | + 'iconBytes': iconData, |
| 277 | + 'category': 7, |
| 278 | + 'targetSdkVersion': 35, |
| 279 | + 'minSdkVersion': 24, |
| 280 | + 'enabled': true, |
| 281 | + 'processName': 'com.test.fullapp.process', |
| 282 | + 'installLocation': 2, |
| 283 | + }; |
| 284 | + |
| 285 | + final appInfo = AppInfo.fromMap(map); |
| 286 | + |
| 287 | + expect(appInfo.packageName, 'com.test.fullapp'); |
| 288 | + expect(appInfo.appName, 'Full Test App'); |
| 289 | + expect(appInfo.versionName, '3.2.1'); |
| 290 | + expect(appInfo.versionCode, 321); |
| 291 | + expect(appInfo.firstInstallTime, DateTime(2024, 1, 1)); |
| 292 | + expect(appInfo.lastUpdateTime, DateTime(2024, 12, 31)); |
| 293 | + expect(appInfo.isSystem, false); |
| 294 | + expect(appInfo.iconBytes, isA<Uint8List>()); |
| 295 | + expect(appInfo.iconBytes!.length, 5); |
| 296 | + expect(appInfo.category, 7); |
| 297 | + expect(appInfo.targetSdkVersion, 35); |
| 298 | + expect(appInfo.minSdkVersion, 24); |
| 299 | + expect(appInfo.enabled, true); |
| 300 | + expect(appInfo.processName, 'com.test.fullapp.process'); |
| 301 | + expect(appInfo.installLocation, 2); |
| 302 | + }); |
| 303 | + |
| 304 | + test('handles mixed valid and invalid values', () { |
| 305 | + final map = <String, Object?>{ |
| 306 | + 'packageName': 'com.example.mixed', |
| 307 | + 'versionCode': 'invalid', |
| 308 | + 'isSystem': 'true', |
| 309 | + 'enabled': 'maybe', |
| 310 | + 'category': 10, |
| 311 | + }; |
| 312 | + |
| 313 | + final appInfo = AppInfo.fromMap(map); |
| 314 | + |
| 315 | + expect(appInfo.packageName, 'com.example.mixed'); |
| 316 | + expect(appInfo.versionCode, isNull); |
| 317 | + expect(appInfo.isSystem, true); |
| 318 | + expect(appInfo.enabled, isNull); |
| 319 | + expect(appInfo.category, 10); |
| 320 | + }); |
| 321 | + }); |
| 322 | + }); |
| 323 | +} |
0 commit comments