Repository navigation
Fix local/remote image file comparison #17826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daniele-verducci
wants to merge
3
commits into
master
Choose a base branch
from
fix/internal-105127
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
app/src/androidTest/java/com/nextcloud/extensions/RemoteFileExtensionsImageTest.kt
This file contains hidden or 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,131 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Your Name <your@email.com> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| package com.nextcloud.extensions | ||
|
|
||
| import android.graphics.Bitmap | ||
| import com.nextcloud.utils.extensions.areImageDimensionsSame | ||
| import com.owncloud.android.lib.resources.files.model.ImageDimension | ||
| import com.owncloud.android.lib.resources.files.model.RemoteFile | ||
| import org.junit.After | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import java.io.File | ||
| import java.io.OutputStream | ||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import kotlin.io.path.exists | ||
|
|
||
| class RemoteFileExtensionsImageTest { | ||
|
|
||
| private lateinit var tempDir: Path | ||
|
|
||
| @Test | ||
| fun remoteFileAreImageDimensionsSame_checkForAllConditions() { | ||
| val remoteFile = RemoteFile().apply { | ||
| mimeType = "image/jpeg" | ||
| imageDimension = ImageDimension(IMAGE_WIDTH.toFloat(), IMAGE_HEIGHT.toFloat()) | ||
| } | ||
|
|
||
| // local not an image | ||
| assertTrue( | ||
| "comparing with non-image", | ||
| remoteFile.areImageDimensionsSame(getPath(TEXT_FILENAME).toString()) | ||
| ) | ||
|
|
||
| // same dimensions | ||
| assertTrue( | ||
| "comparing with same-size image", | ||
| remoteFile.areImageDimensionsSame(getPath(IMAGE_FILENAME).toString()) | ||
| ) | ||
|
|
||
| // different dimensions | ||
| val biggerRemoteImage = RemoteFile().apply { | ||
| mimeType = "image/jpeg" | ||
| imageDimension = ImageDimension((IMAGE_WIDTH * 2).toFloat(), IMAGE_HEIGHT.toFloat()) | ||
| } | ||
| assertFalse( | ||
| "comparing with different-size image", | ||
| biggerRemoteImage.areImageDimensionsSame(getPath(IMAGE_FILENAME).toString()) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun remoteFileAreImageDimensionsSame_checkForEdgeCases() { | ||
| val remoteFileWithDimensions = RemoteFile().apply { | ||
| mimeType = "image/jpeg" | ||
| imageDimension = ImageDimension(IMAGE_WIDTH.toFloat(), IMAGE_HEIGHT.toFloat()) | ||
| } | ||
| val remoteFileWithoutDimensions = RemoteFile().apply { | ||
| mimeType = "image/jpeg" | ||
| imageDimension = null | ||
| } | ||
|
|
||
| // local file with null dimensions | ||
| assertTrue( | ||
| "comparing null-dimensions local file with valid remote file", | ||
| remoteFileWithoutDimensions.areImageDimensionsSame(getPath(TEXT_FILENAME).toString()) | ||
| ) | ||
|
|
||
| // remote file with null dimensions | ||
| assertTrue( | ||
| "comparing with null-dimensions remote file", | ||
| remoteFileWithoutDimensions.areImageDimensionsSame(getPath(IMAGE_FILENAME).toString()) | ||
| ) | ||
|
|
||
| // both files with null dimensions | ||
| assertTrue( | ||
| "comparing null-dimensions local file with null-dimensions remote file", | ||
| remoteFileWithoutDimensions.areImageDimensionsSame(getPath(TEXT_FILENAME).toString()) | ||
| ) | ||
| } | ||
|
|
||
| @After | ||
| fun cleanup() { | ||
| val tf = File(getPath(TEXT_FILENAME).toUri()) | ||
| val imgf = File(getPath(IMAGE_FILENAME).toUri()) | ||
| tf.delete() | ||
| imgf.delete() | ||
| } | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| tempDir = Files.createTempDirectory(TMP_DIR) | ||
| assertTrue("Temp directory should exist", tempDir.exists()) | ||
|
|
||
| createImageFile() | ||
| createTextFile() | ||
| } | ||
|
|
||
| private fun getPath(filename: String): Path = tempDir.resolve("test_$filename") | ||
|
|
||
| private fun createImageFile() { | ||
| val bitmap = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT, Bitmap.Config.ARGB_8888) | ||
| val imagePath = getPath(IMAGE_FILENAME) | ||
|
|
||
| Files.newOutputStream(imagePath).use { out: OutputStream -> | ||
| bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out) | ||
| } | ||
|
|
||
| assertTrue(imagePath.exists()) | ||
| } | ||
|
|
||
| private fun createTextFile() { | ||
| val f = File(getPath(TEXT_FILENAME).toUri()) | ||
| f.writeText("Lorem ipsum") | ||
| } | ||
|
|
||
| companion object { | ||
| val TMP_DIR = "remoteFileExtensionsTest" | ||
| val TEXT_FILENAME = "text.txt" | ||
| val IMAGE_FILENAME = "image.jpg" | ||
|
|
||
| val IMAGE_WIDTH = 123 | ||
| val IMAGE_HEIGHT = 45 | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we truly need tests for this? Could adding tests lead to test bloat?