-
-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Properly dispose images when cache is cleared (#1312)
* fix: Properly dispose images when cache is cleared * Add test for clear and clearCache * Update docs for Images cache
- Loading branch information
Showing
3 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/assets.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class MockImage extends Mock implements Image { | ||
int disposedCount = 0; | ||
|
||
@override | ||
void dispose() { | ||
disposedCount++; | ||
} | ||
} | ||
|
||
void main() { | ||
group('ImagesCache', () { | ||
test('clear', () { | ||
final cache = Images(); | ||
final image = MockImage(); | ||
cache.add('test', image); | ||
expect(image.disposedCount, 0); | ||
cache.clear('test'); | ||
expect(image.disposedCount, 1); | ||
}); | ||
|
||
test('clearCache', () { | ||
final cache = Images(); | ||
final images = List.generate(10, (_) => MockImage()); | ||
for (var i = 0; i < images.length; i++) { | ||
cache.add(i.toString(), images[i]); | ||
} | ||
expect(images.fold<int>(0, (agg, image) => agg + image.disposedCount), 0); | ||
cache.clearCache(); | ||
expect( | ||
images.fold<int>(0, (agg, image) => agg + image.disposedCount), | ||
images.length, | ||
); | ||
}); | ||
}); | ||
} |