@@ -3,11 +3,16 @@ package ovh.plrapps.mapcompose.core
3
3
import android.graphics.Bitmap
4
4
import android.graphics.BitmapFactory
5
5
import android.os.Build
6
- import kotlinx.coroutines.*
6
+ import kotlinx.coroutines.CoroutineScope
7
+ import kotlinx.coroutines.cancel
7
8
import kotlinx.coroutines.channels.Channel
8
9
import kotlinx.coroutines.channels.ReceiveChannel
10
+ import kotlinx.coroutines.launch
9
11
import kotlinx.coroutines.test.runTest
10
- import org.junit.Assert.*
12
+ import org.junit.Assert.assertEquals
13
+ import org.junit.Assert.assertNotNull
14
+ import org.junit.Assert.assertTrue
15
+ import org.junit.Assert.fail
11
16
import org.junit.Test
12
17
import org.junit.runner.RunWith
13
18
import org.robolectric.RobolectricTestRunner
@@ -17,7 +22,6 @@ import java.io.FileInputStream
17
22
18
23
/* *
19
24
* Test the [TileCollector.collectTiles] engine. The following assertions are tested:
20
- * * The Bitmap flow should pick a [Bitmap] from the pool if possible
21
25
* * If [TileSpec]s are send to the input channel, corresponding [Tile]s are received from the
22
26
* output channel (from the [TileCollector.collectTiles] point of view).
23
27
* * The [Bitmap] of the [Tile]s produced should be consistent with the output of the flow
@@ -42,7 +46,6 @@ class TileCollectorTest {
42
46
}
43
47
}
44
48
45
- @OptIn(ExperimentalCoroutinesApi ::class )
46
49
@Test
47
50
fun fullTest () = runTest {
48
51
assertNotNull(assetsDir)
@@ -53,73 +56,66 @@ class TileCollectorTest {
53
56
val visibleTileLocationsChannel = Channel <TileSpec >(capacity = Channel .RENDEZVOUS )
54
57
val tilesOutput = Channel <Tile >(capacity = Channel .RENDEZVOUS )
55
58
56
- val pool = BitmapPool (Dispatchers .Default .limitedParallelism(1 ))
57
-
58
59
val tileStreamProvider = TileStreamProvider { _, _, _ -> FileInputStream (imageFile) }
59
60
60
61
val bitmapReference = try {
61
62
val inputStream = FileInputStream (imageFile)
62
- val bitmapLoadingOptions = BitmapFactory .Options ().apply {
63
- inPreferredConfig = Bitmap .Config .RGB_565
64
- }
65
- BitmapFactory .decodeStream(inputStream, null , bitmapLoadingOptions)
63
+ BitmapFactory .decodeStream(inputStream, null , null )
66
64
} catch (e: Exception ) {
67
65
fail()
68
66
error(" Could not decode image" )
69
67
}
70
68
69
+
70
+ val layers = listOf (
71
+ Layer (" default" , tileStreamProvider)
72
+ )
73
+
74
+ /* Start collecting tiles */
75
+ val tileCollector = TileCollector (1 , optimizeForLowEndDevices = false , tileSize)
76
+ val tileCollectorJob = launch {
77
+ tileCollector.collectTiles(visibleTileLocationsChannel, tilesOutput, layers)
78
+ }
79
+
71
80
fun CoroutineScope.consumeTiles (tileChannel : ReceiveChannel <Tile >) = launch {
81
+ var receivedTiles = 0
72
82
for (tile in tileChannel) {
73
83
println (" received tile ${tile.zoom} -${tile.row} -${tile.col} " )
74
- val bitmap = tile.bitmap
75
84
assertTrue(tile.bitmap?.sameAs(bitmapReference) ? : false )
85
+ receivedTiles + = 1
76
86
77
- /* Add bitmap to the pool only if they are from level 0 */
78
- if (tile.zoom == 0 ) {
79
- if (bitmap != null ) {
80
- pool.put(bitmap )
81
- }
87
+ if (tile.zoom == 6 && tile.row == 6 && tile.col == 6 ) {
88
+ println ( " received poison pill " )
89
+ assertEquals( 7 , receivedTiles)
90
+ cancel( )
91
+ tileCollectorJob.cancel()
82
92
}
83
93
}
84
94
}
85
95
86
- val layers = listOf (
87
- Layer (" default" , tileStreamProvider)
88
- )
89
-
90
96
/* Start consuming tiles */
91
- val tileConsumeJob = launch {
92
- consumeTiles(tilesOutput)
93
- }
94
-
95
- /* Start collecting tiles */
96
- val tileCollector = TileCollector (1 , BitmapConfiguration (Bitmap .Config .RGB_565 , 2 ), tileSize)
97
- val tileCollectorJob = launch {
98
- tileCollector.collectTiles(visibleTileLocationsChannel, tilesOutput, layers, pool)
99
- }
97
+ consumeTiles(tilesOutput)
100
98
101
99
launch {
102
100
val locations1 = listOf (
103
- TileSpec (0 , 0 , 0 ),
104
- TileSpec (0 , 1 , 1 ),
105
- TileSpec (0 , 2 , 1 )
101
+ TileSpec (0 , 0 , 0 ),
102
+ TileSpec (0 , 1 , 1 ),
103
+ TileSpec (0 , 2 , 1 )
106
104
)
107
105
for (spec in locations1) {
108
106
visibleTileLocationsChannel.send(spec)
109
107
}
110
108
111
109
val locations2 = listOf (
112
- TileSpec (1 , 0 , 0 ),
113
- TileSpec (1 , 1 , 1 ),
114
- TileSpec (1 , 2 , 1 )
110
+ TileSpec (1 , 0 , 0 ),
111
+ TileSpec (1 , 1 , 1 ),
112
+ TileSpec (1 , 2 , 1 ),
113
+ TileSpec (6 , 6 , 6 ), // poison pill
115
114
)
116
- /* Bitmaps inside the pool should be used */
115
+
117
116
for (spec in locations2) {
118
117
visibleTileLocationsChannel.send(spec)
119
118
}
120
-
121
- tileCollectorJob.cancel()
122
- tileConsumeJob.cancel()
123
119
}
124
120
Unit
125
121
}
0 commit comments