Skip to content

Commit f71c7f6

Browse files
committed
Adapt tile collector test
1 parent 4a93411 commit f71c7f6

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

Readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ There's an example in the demo app.
7575

7676
Add this to your module's build.gradle
7777
```groovy
78-
implementation 'ovh.plrapps:mapcompose:2.16.1'
78+
implementation 'ovh.plrapps:mapcompose:2.16.2'
7979
```
8080

8181
Starting with v.2.4.1, the library is using the

mapcompose/src/test/java/ovh/plrapps/mapcompose/core/TileCollectorTest.kt

+35-39
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ package ovh.plrapps.mapcompose.core
33
import android.graphics.Bitmap
44
import android.graphics.BitmapFactory
55
import android.os.Build
6-
import kotlinx.coroutines.*
6+
import kotlinx.coroutines.CoroutineScope
7+
import kotlinx.coroutines.cancel
78
import kotlinx.coroutines.channels.Channel
89
import kotlinx.coroutines.channels.ReceiveChannel
10+
import kotlinx.coroutines.launch
911
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
1116
import org.junit.Test
1217
import org.junit.runner.RunWith
1318
import org.robolectric.RobolectricTestRunner
@@ -17,7 +22,6 @@ import java.io.FileInputStream
1722

1823
/**
1924
* Test the [TileCollector.collectTiles] engine. The following assertions are tested:
20-
* * The Bitmap flow should pick a [Bitmap] from the pool if possible
2125
* * If [TileSpec]s are send to the input channel, corresponding [Tile]s are received from the
2226
* output channel (from the [TileCollector.collectTiles] point of view).
2327
* * The [Bitmap] of the [Tile]s produced should be consistent with the output of the flow
@@ -42,7 +46,6 @@ class TileCollectorTest {
4246
}
4347
}
4448

45-
@OptIn(ExperimentalCoroutinesApi::class)
4649
@Test
4750
fun fullTest() = runTest {
4851
assertNotNull(assetsDir)
@@ -53,73 +56,66 @@ class TileCollectorTest {
5356
val visibleTileLocationsChannel = Channel<TileSpec>(capacity = Channel.RENDEZVOUS)
5457
val tilesOutput = Channel<Tile>(capacity = Channel.RENDEZVOUS)
5558

56-
val pool = BitmapPool(Dispatchers.Default.limitedParallelism(1))
57-
5859
val tileStreamProvider = TileStreamProvider { _, _, _ -> FileInputStream(imageFile) }
5960

6061
val bitmapReference = try {
6162
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)
6664
} catch (e: Exception) {
6765
fail()
6866
error("Could not decode image")
6967
}
7068

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+
7180
fun CoroutineScope.consumeTiles(tileChannel: ReceiveChannel<Tile>) = launch {
81+
var receivedTiles = 0
7282
for (tile in tileChannel) {
7383
println("received tile ${tile.zoom}-${tile.row}-${tile.col}")
74-
val bitmap = tile.bitmap
7584
assertTrue(tile.bitmap?.sameAs(bitmapReference) ?: false)
85+
receivedTiles += 1
7686

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()
8292
}
8393
}
8494
}
8595

86-
val layers = listOf(
87-
Layer("default", tileStreamProvider)
88-
)
89-
9096
/* 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)
10098

10199
launch {
102100
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)
106104
)
107105
for (spec in locations1) {
108106
visibleTileLocationsChannel.send(spec)
109107
}
110108

111109
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
115114
)
116-
/* Bitmaps inside the pool should be used */
115+
117116
for (spec in locations2) {
118117
visibleTileLocationsChannel.send(spec)
119118
}
120-
121-
tileCollectorJob.cancel()
122-
tileConsumeJob.cancel()
123119
}
124120
Unit
125121
}

0 commit comments

Comments
 (0)