|
| 1 | +/* |
| 2 | + * Copyright (C) 2013 Square, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.squareup.picasso3.compose |
| 17 | + |
| 18 | +import android.graphics.Bitmap |
| 19 | +import android.graphics.Bitmap.Config.ARGB_8888 |
| 20 | +import androidx.compose.foundation.Canvas |
| 21 | +import androidx.compose.foundation.layout.fillMaxSize |
| 22 | +import androidx.compose.foundation.layout.requiredSize |
| 23 | +import androidx.compose.runtime.CompositionLocalProvider |
| 24 | +import androidx.compose.runtime.getValue |
| 25 | +import androidx.compose.runtime.mutableStateOf |
| 26 | +import androidx.compose.runtime.setValue |
| 27 | +import androidx.compose.ui.Modifier |
| 28 | +import androidx.compose.ui.layout.onSizeChanged |
| 29 | +import androidx.compose.ui.platform.LocalDensity |
| 30 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 31 | +import androidx.compose.ui.unit.Density |
| 32 | +import androidx.compose.ui.unit.IntSize |
| 33 | +import androidx.compose.ui.unit.dp |
| 34 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 35 | +import androidx.test.platform.app.InstrumentationRegistry |
| 36 | +import com.google.common.truth.Truth.assertThat |
| 37 | +import com.squareup.picasso3.Picasso |
| 38 | +import com.squareup.picasso3.Picasso.LoadedFrom |
| 39 | +import com.squareup.picasso3.Request |
| 40 | +import com.squareup.picasso3.RequestHandler |
| 41 | +import org.junit.Rule |
| 42 | +import org.junit.Test |
| 43 | +import org.junit.runner.RunWith |
| 44 | +import kotlinx.coroutines.Dispatchers |
| 45 | + |
| 46 | +@RunWith(AndroidJUnit4::class) |
| 47 | +class PicassoPainterTest { |
| 48 | + |
| 49 | + @get:Rule |
| 50 | + val rule = createComposeRule() |
| 51 | + |
| 52 | + @Test |
| 53 | + fun firstFrameConsumesStateFromLayout() { |
| 54 | + lateinit var lastRequest: Request |
| 55 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 56 | + |
| 57 | + val picasso = Picasso.Builder(context) |
| 58 | + .callFactory { throw RuntimeException() } |
| 59 | + .dispatchers(Dispatchers.Unconfined, Dispatchers.Unconfined) |
| 60 | + .addRequestHandler(object : RequestHandler() { |
| 61 | + override fun canHandleRequest(data: Request): Boolean = true |
| 62 | + override fun load(picasso: Picasso, request: Request, callback: Callback) { |
| 63 | + lastRequest = request |
| 64 | + callback.onSuccess(Result.Bitmap(Bitmap.createBitmap(1, 1, ARGB_8888), LoadedFrom.MEMORY)) |
| 65 | + } |
| 66 | + }) |
| 67 | + .build() |
| 68 | + var size: IntSize by mutableStateOf(IntSize.Zero) |
| 69 | + var drawn = false |
| 70 | + |
| 71 | + rule.setContent { |
| 72 | + CompositionLocalProvider(LocalDensity provides Density(1f)) { |
| 73 | + val painter = picasso.rememberPainter { |
| 74 | + it.load("http://example.com/") |
| 75 | + // Headers are not part of a cache key, using a stable key to break cache |
| 76 | + .stableKey("http://example.com/$size") |
| 77 | + .addHeader("width", size.width.toString()) |
| 78 | + .addHeader("height", size.height.toString()) |
| 79 | + } |
| 80 | + Canvas( |
| 81 | + Modifier |
| 82 | + .requiredSize(9.dp) |
| 83 | + .onSizeChanged { size = it } |
| 84 | + ) { |
| 85 | + val canvasSize = this.size |
| 86 | + |
| 87 | + with(painter) { |
| 88 | + draw(canvasSize) |
| 89 | + } |
| 90 | + drawn = true |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + rule.waitUntil { drawn } |
| 96 | + |
| 97 | + // Draw triggers request was made with size. |
| 98 | + assertThat(lastRequest.headers?.toMultimap()).containsAtLeastEntriesIn( |
| 99 | + mapOf( |
| 100 | + "width" to listOf("9"), |
| 101 | + "height" to listOf("9") |
| 102 | + ) |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun redrawDoesNotReexecuteUnchangedRequest() { |
| 108 | + var requestCount = 0 |
| 109 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 110 | + val picasso = Picasso.Builder(context) |
| 111 | + .callFactory { throw RuntimeException() } |
| 112 | + .dispatchers(Dispatchers.Unconfined, Dispatchers.Unconfined) |
| 113 | + .addRequestHandler(object : RequestHandler() { |
| 114 | + override fun canHandleRequest(data: Request): Boolean = true |
| 115 | + override fun load(picasso: Picasso, request: Request, callback: Callback) { |
| 116 | + requestCount++ |
| 117 | + callback.onSuccess(Result.Bitmap(Bitmap.createBitmap(1, 1, ARGB_8888), LoadedFrom.MEMORY)) |
| 118 | + } |
| 119 | + }) |
| 120 | + .build() |
| 121 | + |
| 122 | + var drawInvalidator by mutableStateOf(0) |
| 123 | + var drawCount = 0 |
| 124 | + rule.setContent { |
| 125 | + CompositionLocalProvider(LocalDensity provides Density(1f)) { |
| 126 | + val painter = picasso.rememberPainter { |
| 127 | + it.load("http://example.com/") |
| 128 | + } |
| 129 | + Canvas(Modifier.fillMaxSize()) { |
| 130 | + drawCount++ |
| 131 | + drawInvalidator = 1 |
| 132 | + |
| 133 | + val canvasSize = this.size |
| 134 | + with(painter) { |
| 135 | + draw(canvasSize) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + rule.waitUntil { drawCount == 2 } |
| 142 | + assertThat(requestCount).isEqualTo(1) |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun newRequestLoaded_whenRequestDependenciesChangedAfterFirstFrame() { |
| 147 | + var lastRequest: Request? = null |
| 148 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 149 | + val picasso = Picasso.Builder(context) |
| 150 | + .callFactory { throw RuntimeException() } |
| 151 | + .dispatchers(Dispatchers.Unconfined, Dispatchers.Unconfined) |
| 152 | + .addRequestHandler(object : RequestHandler() { |
| 153 | + override fun canHandleRequest(data: Request): Boolean = true |
| 154 | + override fun load(picasso: Picasso, request: Request, callback: Callback) { |
| 155 | + lastRequest = request |
| 156 | + callback.onSuccess(Result.Bitmap(Bitmap.createBitmap(1, 1, ARGB_8888), LoadedFrom.MEMORY)) |
| 157 | + } |
| 158 | + }) |
| 159 | + .build() |
| 160 | + var testHeader by mutableStateOf("one") |
| 161 | + |
| 162 | + rule.setContent { |
| 163 | + CompositionLocalProvider(LocalDensity provides Density(1f)) { |
| 164 | + val painter = picasso.rememberPainter { |
| 165 | + it.load("http://example.com/") |
| 166 | + // Headers are not part of a cache key, using a stable key to break cache |
| 167 | + .stableKey("http://example.com/$testHeader") |
| 168 | + .addHeader("testHeader", testHeader) |
| 169 | + } |
| 170 | + Canvas(Modifier.fillMaxSize()) { |
| 171 | + val canvasSize = this.size |
| 172 | + |
| 173 | + with(painter) { |
| 174 | + draw(canvasSize) |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + rule.waitUntil { lastRequest != null } |
| 181 | + assertThat(lastRequest!!.headers?.get("testHeader")).isEqualTo("one") |
| 182 | + |
| 183 | + var currentRequest = lastRequest |
| 184 | + testHeader = "two" |
| 185 | + |
| 186 | + // On API 21 runOnIdle runs before the composition recomposes :-( |
| 187 | + // Waiting until the request updates, then asserting |
| 188 | + rule.waitUntil { currentRequest != lastRequest } |
| 189 | + assertThat(lastRequest!!.headers?.get("testHeader")).isEqualTo("two") |
| 190 | + |
| 191 | + currentRequest = lastRequest |
| 192 | + testHeader = "three" |
| 193 | + |
| 194 | + rule.waitUntil { currentRequest != lastRequest } |
| 195 | + assertThat(lastRequest!!.headers?.get("testHeader")).isEqualTo("three") |
| 196 | + } |
| 197 | +} |
0 commit comments