|
| 1 | +import 'package:flutter/foundation.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:immich_mobile/domain/models/exif.model.dart'; |
| 4 | +import 'package:immich_mobile/domain/services/asset.service.dart'; |
| 5 | +import 'package:mocktail/mocktail.dart'; |
| 6 | + |
| 7 | +import '../../infrastructure/repository.mock.dart'; |
| 8 | +import '../../test_utils.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + late AssetService sut; |
| 12 | + late MockRemoteAssetRepository mockRemoteAssetRepository; |
| 13 | + late MockDriftLocalAssetRepository mockLocalAssetRepository; |
| 14 | + |
| 15 | + setUp(() { |
| 16 | + mockRemoteAssetRepository = MockRemoteAssetRepository(); |
| 17 | + mockLocalAssetRepository = MockDriftLocalAssetRepository(); |
| 18 | + sut = AssetService( |
| 19 | + remoteAssetRepository: mockRemoteAssetRepository, |
| 20 | + localAssetRepository: mockLocalAssetRepository, |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + group('getAspectRatio', () { |
| 25 | + test('flips dimensions on Android for 90° and 270° orientations', () async { |
| 26 | + debugDefaultTargetPlatformOverride = TargetPlatform.android; |
| 27 | + addTearDown(() => debugDefaultTargetPlatformOverride = null); |
| 28 | + |
| 29 | + for (final orientation in [90, 270]) { |
| 30 | + final localAsset = TestUtils.createLocalAsset( |
| 31 | + id: 'local-$orientation', |
| 32 | + width: 1920, |
| 33 | + height: 1080, |
| 34 | + orientation: orientation, |
| 35 | + ); |
| 36 | + |
| 37 | + final result = await sut.getAspectRatio(localAsset); |
| 38 | + |
| 39 | + expect(result, 1080 / 1920, reason: 'Orientation $orientation should flip on Android'); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + test('does not flip dimensions on iOS regardless of orientation', () async { |
| 44 | + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; |
| 45 | + addTearDown(() => debugDefaultTargetPlatformOverride = null); |
| 46 | + |
| 47 | + for (final orientation in [0, 90, 270]) { |
| 48 | + final localAsset = TestUtils.createLocalAsset( |
| 49 | + id: 'local-$orientation', |
| 50 | + width: 1920, |
| 51 | + height: 1080, |
| 52 | + orientation: orientation, |
| 53 | + ); |
| 54 | + |
| 55 | + final result = await sut.getAspectRatio(localAsset); |
| 56 | + |
| 57 | + expect(result, 1920 / 1080, reason: 'iOS should never flip dimensions'); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + test('fetches dimensions from remote repository when missing from asset', () async { |
| 62 | + final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: null, height: null); |
| 63 | + |
| 64 | + final exif = const ExifInfo(orientation: '1'); |
| 65 | + |
| 66 | + final fetchedAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: 1920, height: 1080); |
| 67 | + |
| 68 | + when(() => mockRemoteAssetRepository.getExif('remote-1')).thenAnswer((_) async => exif); |
| 69 | + when(() => mockRemoteAssetRepository.get('remote-1')).thenAnswer((_) async => fetchedAsset); |
| 70 | + |
| 71 | + final result = await sut.getAspectRatio(remoteAsset); |
| 72 | + |
| 73 | + expect(result, 1920 / 1080); |
| 74 | + verify(() => mockRemoteAssetRepository.get('remote-1')).called(1); |
| 75 | + }); |
| 76 | + |
| 77 | + test('fetches dimensions from local repository when missing from local asset', () async { |
| 78 | + final localAsset = TestUtils.createLocalAsset(id: 'local-1', width: null, height: null, orientation: 0); |
| 79 | + |
| 80 | + final fetchedAsset = TestUtils.createLocalAsset(id: 'local-1', width: 1920, height: 1080, orientation: 0); |
| 81 | + |
| 82 | + when(() => mockLocalAssetRepository.get('local-1')).thenAnswer((_) async => fetchedAsset); |
| 83 | + |
| 84 | + final result = await sut.getAspectRatio(localAsset); |
| 85 | + |
| 86 | + expect(result, 1920 / 1080); |
| 87 | + verify(() => mockLocalAssetRepository.get('local-1')).called(1); |
| 88 | + }); |
| 89 | + |
| 90 | + test('returns 1.0 when dimensions are still unavailable after fetching', () async { |
| 91 | + final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: null, height: null); |
| 92 | + |
| 93 | + final exif = const ExifInfo(orientation: '1'); |
| 94 | + |
| 95 | + when(() => mockRemoteAssetRepository.getExif('remote-1')).thenAnswer((_) async => exif); |
| 96 | + when(() => mockRemoteAssetRepository.get('remote-1')).thenAnswer((_) async => null); |
| 97 | + |
| 98 | + final result = await sut.getAspectRatio(remoteAsset); |
| 99 | + |
| 100 | + expect(result, 1.0); |
| 101 | + }); |
| 102 | + |
| 103 | + test('returns 1.0 when height is zero', () async { |
| 104 | + final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: 1920, height: 0); |
| 105 | + |
| 106 | + final exif = const ExifInfo(orientation: '1'); |
| 107 | + |
| 108 | + when(() => mockRemoteAssetRepository.getExif('remote-1')).thenAnswer((_) async => exif); |
| 109 | + |
| 110 | + final result = await sut.getAspectRatio(remoteAsset); |
| 111 | + |
| 112 | + expect(result, 1.0); |
| 113 | + }); |
| 114 | + |
| 115 | + test('handles local asset with remoteId and uses exif from remote', () async { |
| 116 | + final localAsset = TestUtils.createLocalAsset( |
| 117 | + id: 'local-1', |
| 118 | + remoteId: 'remote-1', |
| 119 | + width: 1920, |
| 120 | + height: 1080, |
| 121 | + orientation: 0, |
| 122 | + ); |
| 123 | + |
| 124 | + final exif = const ExifInfo(orientation: '6'); |
| 125 | + |
| 126 | + when(() => mockRemoteAssetRepository.getExif('remote-1')).thenAnswer((_) async => exif); |
| 127 | + |
| 128 | + final result = await sut.getAspectRatio(localAsset); |
| 129 | + |
| 130 | + expect(result, 1080 / 1920); |
| 131 | + }); |
| 132 | + |
| 133 | + test('handles various flipped EXIF orientations correctly', () async { |
| 134 | + final flippedOrientations = ['5', '6', '7', '8', '90', '-90']; |
| 135 | + |
| 136 | + for (final orientation in flippedOrientations) { |
| 137 | + final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-$orientation', width: 1920, height: 1080); |
| 138 | + |
| 139 | + final exif = ExifInfo(orientation: orientation); |
| 140 | + |
| 141 | + when(() => mockRemoteAssetRepository.getExif('remote-$orientation')).thenAnswer((_) async => exif); |
| 142 | + |
| 143 | + final result = await sut.getAspectRatio(remoteAsset); |
| 144 | + |
| 145 | + expect(result, 1080 / 1920, reason: 'Orientation $orientation should flip dimensions'); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + test('handles various non-flipped EXIF orientations correctly', () async { |
| 150 | + final nonFlippedOrientations = ['1', '2', '3', '4']; |
| 151 | + |
| 152 | + for (final orientation in nonFlippedOrientations) { |
| 153 | + final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-$orientation', width: 1920, height: 1080); |
| 154 | + |
| 155 | + final exif = ExifInfo(orientation: orientation); |
| 156 | + |
| 157 | + when(() => mockRemoteAssetRepository.getExif('remote-$orientation')).thenAnswer((_) async => exif); |
| 158 | + |
| 159 | + final result = await sut.getAspectRatio(remoteAsset); |
| 160 | + |
| 161 | + expect(result, 1920 / 1080, reason: 'Orientation $orientation should NOT flip dimensions'); |
| 162 | + } |
| 163 | + }); |
| 164 | + }); |
| 165 | +} |
0 commit comments