|
| 1 | +import 'package:flutter_device_apps_platform_interface/flutter_device_apps_app_change_event.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('AppChangeType', () { |
| 6 | + test('has all expected values', () { |
| 7 | + expect(AppChangeType.values, hasLength(3)); |
| 8 | + expect(AppChangeType.values, contains(AppChangeType.installed)); |
| 9 | + expect(AppChangeType.values, contains(AppChangeType.removed)); |
| 10 | + expect(AppChangeType.values, contains(AppChangeType.updated)); |
| 11 | + }); |
| 12 | + |
| 13 | + test('enum names match expected strings', () { |
| 14 | + expect(AppChangeType.installed.name, 'installed'); |
| 15 | + expect(AppChangeType.removed.name, 'removed'); |
| 16 | + expect(AppChangeType.updated.name, 'updated'); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + group('AppChangeEvent', () { |
| 21 | + group('constructor', () { |
| 22 | + test('creates instance with all null values', () { |
| 23 | + const event = AppChangeEvent(); |
| 24 | + |
| 25 | + expect(event.packageName, isNull); |
| 26 | + expect(event.type, isNull); |
| 27 | + expect(event.isReplacing, isNull); |
| 28 | + }); |
| 29 | + |
| 30 | + test('creates instance with all values', () { |
| 31 | + const event = AppChangeEvent( |
| 32 | + packageName: 'com.example.app', |
| 33 | + type: AppChangeType.installed, |
| 34 | + isReplacing: false, |
| 35 | + ); |
| 36 | + |
| 37 | + expect(event.packageName, 'com.example.app'); |
| 38 | + expect(event.type, AppChangeType.installed); |
| 39 | + expect(event.isReplacing, false); |
| 40 | + }); |
| 41 | + |
| 42 | + test('creates instance with partial values', () { |
| 43 | + const event = AppChangeEvent( |
| 44 | + packageName: 'com.example.app', |
| 45 | + ); |
| 46 | + |
| 47 | + expect(event.packageName, 'com.example.app'); |
| 48 | + expect(event.type, isNull); |
| 49 | + expect(event.isReplacing, isNull); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + group('fromMap', () { |
| 54 | + test('parses empty map', () { |
| 55 | + final event = AppChangeEvent.fromMap({}); |
| 56 | + |
| 57 | + expect(event.packageName, isNull); |
| 58 | + expect(event.type, isNull); |
| 59 | + expect(event.isReplacing, isNull); |
| 60 | + }); |
| 61 | + |
| 62 | + test('parses packageName correctly', () { |
| 63 | + final event = AppChangeEvent.fromMap({ |
| 64 | + 'packageName': 'com.test.app', |
| 65 | + }); |
| 66 | + |
| 67 | + expect(event.packageName, 'com.test.app'); |
| 68 | + }); |
| 69 | + |
| 70 | + test('parses type "installed" correctly', () { |
| 71 | + final event = AppChangeEvent.fromMap({ |
| 72 | + 'type': 'installed', |
| 73 | + }); |
| 74 | + |
| 75 | + expect(event.type, AppChangeType.installed); |
| 76 | + }); |
| 77 | + |
| 78 | + test('parses type "removed" correctly', () { |
| 79 | + final event = AppChangeEvent.fromMap({ |
| 80 | + 'type': 'removed', |
| 81 | + }); |
| 82 | + |
| 83 | + expect(event.type, AppChangeType.removed); |
| 84 | + }); |
| 85 | + |
| 86 | + test('parses type "updated" correctly', () { |
| 87 | + final event = AppChangeEvent.fromMap({ |
| 88 | + 'type': 'updated', |
| 89 | + }); |
| 90 | + |
| 91 | + expect(event.type, AppChangeType.updated); |
| 92 | + }); |
| 93 | + |
| 94 | + test('returns null type for invalid type string', () { |
| 95 | + final event = AppChangeEvent.fromMap({ |
| 96 | + 'type': 'unknown', |
| 97 | + }); |
| 98 | + |
| 99 | + expect(event.type, isNull); |
| 100 | + }); |
| 101 | + |
| 102 | + test('returns null type for empty type string', () { |
| 103 | + final event = AppChangeEvent.fromMap({ |
| 104 | + 'type': '', |
| 105 | + }); |
| 106 | + |
| 107 | + expect(event.type, isNull); |
| 108 | + }); |
| 109 | + |
| 110 | + test('parses isReplacing from bool value', () { |
| 111 | + final eventTrue = AppChangeEvent.fromMap({ |
| 112 | + 'isReplacing': true, |
| 113 | + }); |
| 114 | + final eventFalse = AppChangeEvent.fromMap({ |
| 115 | + 'isReplacing': false, |
| 116 | + }); |
| 117 | + |
| 118 | + expect(eventTrue.isReplacing, true); |
| 119 | + expect(eventFalse.isReplacing, false); |
| 120 | + }); |
| 121 | + |
| 122 | + test('parses isReplacing from string value', () { |
| 123 | + final eventTrue = AppChangeEvent.fromMap({ |
| 124 | + 'isReplacing': 'true', |
| 125 | + }); |
| 126 | + final eventFalse = AppChangeEvent.fromMap({ |
| 127 | + 'isReplacing': 'false', |
| 128 | + }); |
| 129 | + |
| 130 | + expect(eventTrue.isReplacing, true); |
| 131 | + expect(eventFalse.isReplacing, false); |
| 132 | + }); |
| 133 | + |
| 134 | + test('returns null isReplacing for invalid string', () { |
| 135 | + final event = AppChangeEvent.fromMap({ |
| 136 | + 'isReplacing': 'yes', |
| 137 | + }); |
| 138 | + |
| 139 | + expect(event.isReplacing, isNull); |
| 140 | + }); |
| 141 | + |
| 142 | + test('parses complete map with all fields', () { |
| 143 | + final event = AppChangeEvent.fromMap({ |
| 144 | + 'packageName': 'com.example.fullapp', |
| 145 | + 'type': 'updated', |
| 146 | + 'isReplacing': true, |
| 147 | + }); |
| 148 | + |
| 149 | + expect(event.packageName, 'com.example.fullapp'); |
| 150 | + expect(event.type, AppChangeType.updated); |
| 151 | + expect(event.isReplacing, true); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + group('toMap', () { |
| 156 | + test('converts empty event to map', () { |
| 157 | + const event = AppChangeEvent(); |
| 158 | + final Map<String, Object?> map = event.toMap(); |
| 159 | + |
| 160 | + expect(map['packageName'], isNull); |
| 161 | + expect(map['type'], isNull); |
| 162 | + expect(map['isReplacing'], isNull); |
| 163 | + }); |
| 164 | + |
| 165 | + test('converts full event to map', () { |
| 166 | + const event = AppChangeEvent( |
| 167 | + packageName: 'com.example.app', |
| 168 | + type: AppChangeType.installed, |
| 169 | + isReplacing: false, |
| 170 | + ); |
| 171 | + final Map<String, Object?> map = event.toMap(); |
| 172 | + |
| 173 | + expect(map['packageName'], 'com.example.app'); |
| 174 | + expect(map['type'], 'installed'); |
| 175 | + expect(map['isReplacing'], false); |
| 176 | + }); |
| 177 | + |
| 178 | + test('converts removed type correctly', () { |
| 179 | + const event = AppChangeEvent(type: AppChangeType.removed); |
| 180 | + final Map<String, Object?> map = event.toMap(); |
| 181 | + |
| 182 | + expect(map['type'], 'removed'); |
| 183 | + }); |
| 184 | + |
| 185 | + test('converts updated type correctly', () { |
| 186 | + const event = AppChangeEvent(type: AppChangeType.updated); |
| 187 | + final Map<String, Object?> map = event.toMap(); |
| 188 | + |
| 189 | + expect(map['type'], 'updated'); |
| 190 | + }); |
| 191 | + |
| 192 | + test('converts partial event to map', () { |
| 193 | + const event = AppChangeEvent( |
| 194 | + packageName: 'com.partial.app', |
| 195 | + isReplacing: true, |
| 196 | + ); |
| 197 | + final Map<String, Object?> map = event.toMap(); |
| 198 | + |
| 199 | + expect(map['packageName'], 'com.partial.app'); |
| 200 | + expect(map['type'], isNull); |
| 201 | + expect(map['isReplacing'], true); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + group('round-trip (fromMap -> toMap)', () { |
| 206 | + test('installed event survives round-trip', () { |
| 207 | + final originalMap = <String, Object?>{ |
| 208 | + 'packageName': 'com.roundtrip.installed', |
| 209 | + 'type': 'installed', |
| 210 | + 'isReplacing': false, |
| 211 | + }; |
| 212 | + |
| 213 | + final event = AppChangeEvent.fromMap(originalMap); |
| 214 | + final Map<String, Object?> resultMap = event.toMap(); |
| 215 | + |
| 216 | + expect(resultMap['packageName'], originalMap['packageName']); |
| 217 | + expect(resultMap['type'], originalMap['type']); |
| 218 | + expect(resultMap['isReplacing'], originalMap['isReplacing']); |
| 219 | + }); |
| 220 | + |
| 221 | + test('removed event survives round-trip', () { |
| 222 | + final originalMap = <String, Object?>{ |
| 223 | + 'packageName': 'com.roundtrip.removed', |
| 224 | + 'type': 'removed', |
| 225 | + 'isReplacing': false, |
| 226 | + }; |
| 227 | + |
| 228 | + final event = AppChangeEvent.fromMap(originalMap); |
| 229 | + final Map<String, Object?> resultMap = event.toMap(); |
| 230 | + |
| 231 | + expect(resultMap['packageName'], originalMap['packageName']); |
| 232 | + expect(resultMap['type'], originalMap['type']); |
| 233 | + expect(resultMap['isReplacing'], originalMap['isReplacing']); |
| 234 | + }); |
| 235 | + |
| 236 | + test('updated event with replacing survives round-trip', () { |
| 237 | + final originalMap = <String, Object?>{ |
| 238 | + 'packageName': 'com.roundtrip.updated', |
| 239 | + 'type': 'updated', |
| 240 | + 'isReplacing': true, |
| 241 | + }; |
| 242 | + |
| 243 | + final event = AppChangeEvent.fromMap(originalMap); |
| 244 | + final Map<String, Object?> resultMap = event.toMap(); |
| 245 | + |
| 246 | + expect(resultMap['packageName'], originalMap['packageName']); |
| 247 | + expect(resultMap['type'], originalMap['type']); |
| 248 | + expect(resultMap['isReplacing'], originalMap['isReplacing']); |
| 249 | + }); |
| 250 | + |
| 251 | + test('empty event survives round-trip', () { |
| 252 | + final originalMap = <String, Object?>{}; |
| 253 | + |
| 254 | + final event = AppChangeEvent.fromMap(originalMap); |
| 255 | + final Map<String, Object?> resultMap = event.toMap(); |
| 256 | + |
| 257 | + expect(resultMap['packageName'], isNull); |
| 258 | + expect(resultMap['type'], isNull); |
| 259 | + expect(resultMap['isReplacing'], isNull); |
| 260 | + }); |
| 261 | + }); |
| 262 | + }); |
| 263 | +} |
0 commit comments