Skip to content

Commit 82478f5

Browse files
committed
chore: add test update
Signed-off-by: mykh-hailo <kristianderonta0205@gmail.com>
1 parent 2dd0acb commit 82478f5

2 files changed

Lines changed: 114 additions & 39 deletions

File tree

app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import android.content.ContentUris;
2222
import android.content.ContentValues;
2323
import android.content.Context;
24-
import android.content.Intent;
2524
import android.media.MediaScannerConnection;
2625
import android.content.OperationApplicationException;
2726
import android.database.Cursor;
2827
import android.net.Uri;
29-
import android.os.Build;
3028
import android.os.RemoteException;
3129
import android.provider.MediaStore;
3230
import android.text.TextUtils;
@@ -2016,49 +2014,28 @@ public List<OCShare> getSharesWithForAFile(String filePath, String accountName)
20162014
}
20172015

20182016
public static void triggerMediaScan(String path) {
2019-
triggerMediaScan(path, null);
2017+
triggerMediaScan(MainApp.getAppContext(), path, null);
20202018
}
20212019

20222020
public static void triggerMediaScan(String path, OCFile file) {
2021+
triggerMediaScan(MainApp.getAppContext(), path, file);
2022+
}
2023+
2024+
public static void triggerMediaScan(Context context, String path, OCFile file) {
20232025
if (path != null && !TextUtils.isEmpty(path)) {
2024-
ContentValues values = new ContentValues();
2025-
ContentResolver contentResolver = MainApp.getAppContext().getContentResolver();
2026-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
2027-
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
2028-
if (file != null) {
2029-
values.put(MediaStore.Images.Media.MIME_TYPE, file.getMimeType());
2030-
values.put(MediaStore.Images.Media.TITLE, file.getFileName());
2031-
values.put(MediaStore.Images.Media.DISPLAY_NAME, file.getFileName());
2032-
}
2033-
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
2034-
values.put(MediaStore.Images.Media.RELATIVE_PATH, path);
2035-
values.put(MediaStore.Images.Media.IS_PENDING, 0);
2036-
try {
2037-
contentResolver.insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY),
2038-
values);
2039-
} catch (IllegalArgumentException e) {
2040-
Log_OC.e("MediaScanner", "Adding image to media scanner failed: " + e);
2026+
String mimeType = file != null ? file.getMimeType() : null;
2027+
MediaScannerConnection.scanFile(
2028+
context,
2029+
new String[]{path},
2030+
mimeType != null ? new String[]{mimeType} : null,
2031+
(scannedPath, scannedUri) -> {
2032+
if (scannedUri != null) {
2033+
Log_OC.d(TAG, "Media scan completed for " + scannedPath);
2034+
} else {
2035+
Log_OC.w(TAG, "Media scan failed for " + scannedPath);
20412036
}
2042-
} else {
2043-
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
2044-
intent.setData(Uri.fromFile(new File(path)));
2045-
MainApp.getAppContext().sendBroadcast(intent);
20462037
}
2047-
} else {
2048-
String mimeType = file != null ? file.getMimeType() : null;
2049-
MediaScannerConnection.scanFile(
2050-
MainApp.getAppContext(),
2051-
new String[]{path},
2052-
mimeType != null ? new String[]{mimeType} : null,
2053-
(scannedPath, scannedUri) -> {
2054-
if (scannedUri != null) {
2055-
Log_OC.d(TAG, "Media scan completed for " + scannedPath);
2056-
} else {
2057-
Log_OC.w(TAG, "Media scan failed for " + scannedPath);
2058-
}
2059-
}
2060-
);
2061-
}
2038+
);
20622039
}
20632040
}
20642041

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
package com.owncloud.android.datamodel
7+
8+
import android.content.Context
9+
import android.media.MediaScannerConnection
10+
import android.text.TextUtils
11+
import io.mockk.every
12+
import io.mockk.mockk
13+
import io.mockk.mockkStatic
14+
import io.mockk.slot
15+
import io.mockk.unmockkAll
16+
import io.mockk.verify
17+
import org.junit.After
18+
import org.junit.Assert.assertEquals
19+
import org.junit.Before
20+
import org.junit.Test
21+
22+
class FileDataStorageManagerTriggerMediaScanTest {
23+
24+
private lateinit var mockContext: Context
25+
26+
@Before
27+
fun setUp() {
28+
mockContext = mockk(relaxed = true)
29+
mockkStatic(TextUtils::class)
30+
every { TextUtils.isEmpty(any()) } answers { arg<CharSequence?>(0)?.toString().isNullOrEmpty() }
31+
mockkStatic(MediaScannerConnection::class)
32+
every { MediaScannerConnection.scanFile(any(), any(), any(), any()) } returns Unit
33+
}
34+
35+
@After
36+
fun tearDown() {
37+
unmockkAll()
38+
}
39+
40+
@Test
41+
fun triggerMediaScan_withValidPath_callsMediaScannerConnection() {
42+
val path = "/storage/emulated/0/DCIM/photo.jpg"
43+
44+
FileDataStorageManager.triggerMediaScan(mockContext, path, null)
45+
46+
val pathsSlot = slot<Array<String>>()
47+
verify(exactly = 1) {
48+
MediaScannerConnection.scanFile(
49+
mockContext,
50+
capture(pathsSlot),
51+
null,
52+
any()
53+
)
54+
}
55+
assertEquals(path, pathsSlot.captured.single())
56+
}
57+
58+
@Test
59+
fun triggerMediaScan_withOCFile_passesMimeType() {
60+
val path = "/storage/emulated/0/DCIM/photo.jpg"
61+
val file = mockk<OCFile>(relaxed = true) {
62+
every { mimeType } returns "image/jpeg"
63+
}
64+
65+
FileDataStorageManager.triggerMediaScan(mockContext, path, file)
66+
67+
val pathsSlot = slot<Array<String>>()
68+
val mimeTypesSlot = slot<Array<String>>()
69+
verify(exactly = 1) {
70+
MediaScannerConnection.scanFile(
71+
mockContext,
72+
capture(pathsSlot),
73+
capture(mimeTypesSlot),
74+
any()
75+
)
76+
}
77+
assertEquals(path, pathsSlot.captured.single())
78+
assertEquals("image/jpeg", mimeTypesSlot.captured.single())
79+
}
80+
81+
@Test
82+
fun triggerMediaScan_withEmptyPath_doesNotCallMediaScanner() {
83+
FileDataStorageManager.triggerMediaScan(mockContext, "", null)
84+
85+
verify(exactly = 0) {
86+
MediaScannerConnection.scanFile(any(), any(), any(), any())
87+
}
88+
}
89+
90+
@Test
91+
fun triggerMediaScan_withNullPath_doesNotCallMediaScanner() {
92+
FileDataStorageManager.triggerMediaScan(mockContext, null, null)
93+
94+
verify(exactly = 0) {
95+
MediaScannerConnection.scanFile(any(), any(), any(), any())
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)