Merge pull request #1939 from nextcloud/dependabot/gradle/org.mockito-mockito-core-4.5.1

Bump mockito-core from 3.12.4 to 4.5.1
This commit is contained in:
Andy Scherzinger 2022-05-27 19:43:09 +02:00 committed by GitHub
commit fcf20e3362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 103 deletions

View File

@ -152,7 +152,6 @@ ext {
okhttpVersion = "4.9.3" okhttpVersion = "4.9.3"
materialDialogsVersion = "3.3.0" materialDialogsVersion = "3.3.0"
parcelerVersion = "1.1.13" parcelerVersion = "1.1.13"
powermockVersion = "2.0.9"
retrofit2Version = "2.9.0" retrofit2Version = "2.9.0"
workVersion = "2.7.1" workVersion = "2.7.1"
markwonVersion = "4.6.2" markwonVersion = "4.6.2"
@ -294,10 +293,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.core:core-ktx:1.7.0'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.12.4' testImplementation 'org.mockito:mockito-core:4.5.1'
testImplementation "org.powermock:powermock-core:${powermockVersion}"
testImplementation "org.powermock:powermock-module-junit4:${powermockVersion}"
testImplementation "org.powermock:powermock-api-mockito2:${powermockVersion}"
androidTestImplementation "androidx.test:core:1.4.0" androidTestImplementation "androidx.test:core:1.4.0"

View File

@ -20,22 +20,33 @@
package com.nextcloud.talk.utils package com.nextcloud.talk.utils
import android.annotation.SuppressLint
import android.app.NotificationManager import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.media.AudioManager import android.media.AudioManager
import android.os.Build import android.os.Build
import android.os.Vibrator import android.os.Vibrator
import androidx.annotation.VisibleForTesting
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
object DoNotDisturbUtils { object DoNotDisturbUtils {
fun shouldPlaySound(): Boolean {
val context = NextcloudTalkApplication.sharedApplication?.applicationContext private var buildVersion = Build.VERSION.SDK_INT
@VisibleForTesting
fun setTestingBuildVersion(version: Int) {
buildVersion = version
}
@SuppressLint("NewApi")
@JvmOverloads
fun shouldPlaySound(context: Context? = NextcloudTalkApplication.sharedApplication?.applicationContext): Boolean {
val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
var shouldPlaySound = true var shouldPlaySound = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (buildVersion >= Build.VERSION_CODES.M) {
if (notificationManager.currentInterruptionFilter != NotificationManager.INTERRUPTION_FILTER_ALL) { if (notificationManager.currentInterruptionFilter != NotificationManager.INTERRUPTION_FILTER_ALL) {
shouldPlaySound = false shouldPlaySound = false
} }
@ -50,16 +61,18 @@ object DoNotDisturbUtils {
return shouldPlaySound return shouldPlaySound
} }
fun hasVibrator(): Boolean { private fun hasVibrator(context: Context?): Boolean {
val context = NextcloudTalkApplication.sharedApplication?.applicationContext
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
return vibrator.hasVibrator() return vibrator.hasVibrator()
} }
fun shouldVibrate(vibrate: Boolean): Boolean { @JvmOverloads
fun shouldVibrate(
context: Context? = NextcloudTalkApplication.sharedApplication?.applicationContext,
vibrate: Boolean
): Boolean {
if (hasVibrator()) { if (hasVibrator(context)) {
val context = NextcloudTalkApplication.sharedApplication?.applicationContext
val audioManager = context?.getSystemService(Context.AUDIO_SERVICE) as AudioManager val audioManager = context?.getSystemService(Context.AUDIO_SERVICE) as AudioManager
return if (vibrate) { return if (vibrate) {

View File

@ -17,35 +17,31 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.nextcloud.talk.utils
package com.nextcloud.talk.utils; import android.content.Context
import com.nextcloud.talk.R
import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.utils.database.user.UserUtils
import android.content.Context; object ShareUtils {
import android.text.TextUtils; fun getStringForIntent(
context: Context?,
import com.nextcloud.talk.R; password: String?,
import com.nextcloud.talk.models.database.UserEntity; userUtils: UserUtils?,
import com.nextcloud.talk.models.json.conversations.Conversation; conversation: Conversation?
import com.nextcloud.talk.utils.database.user.UserUtils; ): String {
val userEntity = userUtils?.currentUser
import androidx.annotation.Nullable; var shareString = ""
public class ShareUtils {
public static String getStringForIntent(Context context, @Nullable String password, UserUtils userUtils, Conversation
conversation) {
UserEntity userEntity = userUtils.getCurrentUser();
String shareString = "";
if (userEntity != null && context != null) { if (userEntity != null && context != null) {
shareString = String.format(context.getResources().getString(R.string.nc_share_text), shareString = String.format(
userEntity.getBaseUrl(), conversation.getToken()); context.resources.getString(R.string.nc_share_text),
userEntity.baseUrl, conversation?.token
if (!TextUtils.isEmpty(password)) { )
shareString += String.format(context.getResources().getString(R.string.nc_share_text_pass), password); if (!password.isNullOrEmpty()) {
shareString += String.format(context.resources.getString(R.string.nc_share_text_pass), password)
} }
} }
return shareString
return shareString;
} }
} }

View File

@ -26,36 +26,20 @@ import android.media.AudioManager;
import android.os.Build; import android.os.Build;
import android.os.Vibrator; import android.os.Vibrator;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest(NextcloudTalkApplication.class)
public class DoNotDisturbUtilsTest { public class DoNotDisturbUtilsTest {
@Mock @Mock
private Context context; private Context context;
@Mock
private NextcloudTalkApplication application;
@Mock @Mock
private NotificationManager notificationManager; private NotificationManager notificationManager;
@ -68,46 +52,30 @@ public class DoNotDisturbUtilsTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.openMocks(this); MockitoAnnotations.openMocks(this);
mockStatic(NextcloudTalkApplication.Companion.class);
NextcloudTalkApplication.Companion companionMock = PowerMockito.mock(NextcloudTalkApplication.Companion.class);
Whitebox.setInternalState(NextcloudTalkApplication.class,"Companion",companionMock);
PowerMockito.when(NextcloudTalkApplication.Companion.getSharedApplication()).thenReturn(application);
when(application.getApplicationContext()).thenReturn(context);
when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager); when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager);
when(context.getSystemService(Context.AUDIO_SERVICE)).thenReturn(audioManager); when(context.getSystemService(Context.AUDIO_SERVICE)).thenReturn(audioManager);
when(context.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(vibrator); when(context.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(vibrator);
} }
private static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
@Test @Test
public void shouldPlaySound_givenAndroidMAndInterruptionFilterNone_assertReturnsFalse() public void shouldPlaySound_givenAndroidMAndInterruptionFilterNone_assertReturnsFalse() {
throws Exception { DoNotDisturbUtils.INSTANCE.setTestingBuildVersion(Build.VERSION_CODES.M);
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.M);
when(notificationManager.getCurrentInterruptionFilter()).thenReturn(NotificationManager.INTERRUPTION_FILTER_NONE); when(notificationManager.getCurrentInterruptionFilter()).thenReturn(NotificationManager.INTERRUPTION_FILTER_NONE);
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL); when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
assertFalse("shouldPlaySound incorrectly returned true", assertFalse("shouldPlaySound incorrectly returned true",
DoNotDisturbUtils.INSTANCE.shouldPlaySound()); DoNotDisturbUtils.INSTANCE.shouldPlaySound(context));
} }
@Test @Test
public void shouldPlaySound_givenRingerModeNotNormal_assertReturnsFalse() throws Exception { public void shouldPlaySound_givenRingerModeNotNormal_assertReturnsFalse() throws Exception {
setFinalStatic(Build.VERSION.class.getField("SDK_INT"), Build.VERSION_CODES.LOLLIPOP); DoNotDisturbUtils.INSTANCE.setTestingBuildVersion(Build.VERSION_CODES.LOLLIPOP);
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
assertFalse("shouldPlaySound incorrectly returned true", assertFalse("shouldPlaySound incorrectly returned true",
DoNotDisturbUtils.INSTANCE.shouldPlaySound()); DoNotDisturbUtils.INSTANCE.shouldPlaySound(context));
} }
@Test @Test
@ -115,7 +83,7 @@ public class DoNotDisturbUtilsTest {
when(vibrator.hasVibrator()).thenReturn(false); when(vibrator.hasVibrator()).thenReturn(false);
assertFalse("shouldVibrate returned true despite no vibrator", assertFalse("shouldVibrate returned true despite no vibrator",
DoNotDisturbUtils.INSTANCE.shouldVibrate(true)); DoNotDisturbUtils.INSTANCE.shouldVibrate(context, true));
} }
@Test @Test
@ -125,7 +93,7 @@ public class DoNotDisturbUtilsTest {
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL); when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
assertTrue("shouldVibrate incorrectly returned false", assertTrue("shouldVibrate incorrectly returned false",
DoNotDisturbUtils.INSTANCE.shouldVibrate(true)); DoNotDisturbUtils.INSTANCE.shouldVibrate(context, true));
} }
@Test @Test
@ -135,6 +103,6 @@ public class DoNotDisturbUtilsTest {
when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_SILENT); when(audioManager.getRingerMode()).thenReturn(AudioManager.RINGER_MODE_SILENT);
assertFalse("shouldVibrate returned true despite ringer mode set to silent", assertFalse("shouldVibrate returned true despite ringer mode set to silent",
DoNotDisturbUtils.INSTANCE.shouldVibrate(true)); DoNotDisturbUtils.INSTANCE.shouldVibrate(context, true));
} }
} }

View File

@ -21,8 +21,6 @@ package com.nextcloud.talk.utils
import android.content.Context import android.content.Context
import android.content.res.Resources import android.content.res.Resources
import android.text.TextUtils
import at.bitfire.dav4jvm.HttpUtils.parseDate
import com.nextcloud.talk.R import com.nextcloud.talk.R
import com.nextcloud.talk.models.database.UserEntity import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.models.json.conversations.Conversation import com.nextcloud.talk.models.json.conversations.Conversation
@ -30,21 +28,11 @@ import com.nextcloud.talk.utils.database.user.UserUtils
import org.junit.Assert import org.junit.Assert
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Before import org.junit.Before
import org.junit.Ignore
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito import org.mockito.Mockito
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import java.text.ParseException
@RunWith(PowerMockRunner::class)
@PrepareForTest(TextUtils::class)
@Ignore("Test fails on CI server. See issue https://github.com/nextcloud/talk-android/issues/1737")
class ShareUtilsTest { class ShareUtilsTest {
@Mock @Mock
private val context: Context? = null private val context: Context? = null
@ -55,30 +43,29 @@ class ShareUtilsTest {
@Mock @Mock
private val userUtils: UserUtils? = null private val userUtils: UserUtils? = null
@Mock
private val conversation: Conversation? = null
@Mock @Mock
private val userEntity: UserEntity? = null private val userEntity: UserEntity? = null
private val baseUrl = "https://my.nextcloud.com" private val baseUrl = "https://my.nextcloud.com"
private val token = "2aotbrjr" private val token = "2aotbrjr"
private lateinit var conversation: Conversation
@Before @Before
fun setUp() { fun setUp() {
MockitoAnnotations.openMocks(this) MockitoAnnotations.openMocks(this)
PowerMockito.mockStatic(TextUtils::class.java)
Mockito.`when`(userUtils!!.currentUser).thenReturn(userEntity) Mockito.`when`(userUtils!!.currentUser).thenReturn(userEntity)
Mockito.`when`(userEntity!!.baseUrl).thenReturn(baseUrl) Mockito.`when`(userEntity!!.baseUrl).thenReturn(baseUrl)
Mockito.`when`(conversation!!.token).thenReturn(token)
Mockito.`when`(context!!.resources).thenReturn(resources) Mockito.`when`(context!!.resources).thenReturn(resources)
Mockito.`when`(resources!!.getString(R.string.nc_share_text)) Mockito.`when`(resources!!.getString(R.string.nc_share_text))
.thenReturn("Join the conversation at %1\$s/index.php/call/%2\$s") .thenReturn("Join the conversation at %1\$s/index.php/call/%2\$s")
Mockito.`when`(resources.getString(R.string.nc_share_text_pass)).thenReturn("\nPassword: %1\$s") Mockito.`when`(resources.getString(R.string.nc_share_text_pass)).thenReturn("\nPassword: %1\$s")
conversation = Conversation(token = token)
} }
@Test @Test
fun stringForIntent_noPasswordGiven_correctStringWithoutPasswordReturned() { fun stringForIntent_noPasswordGiven_correctStringWithoutPasswordReturned() {
PowerMockito.`when`(TextUtils.isEmpty(ArgumentMatchers.anyString())).thenReturn(true)
val expectedResult = String.format( val expectedResult = String.format(
"Join the conversation at %s/index.php/call/%s", "Join the conversation at %s/index.php/call/%s",
baseUrl, token baseUrl, token
@ -91,7 +78,6 @@ class ShareUtilsTest {
@Test @Test
fun stringForIntent_passwordGiven_correctStringWithPasswordReturned() { fun stringForIntent_passwordGiven_correctStringWithPasswordReturned() {
PowerMockito.`when`(TextUtils.isEmpty(ArgumentMatchers.anyString())).thenReturn(false)
val password = "superSecret" val password = "superSecret"
val expectedResult = String.format( val expectedResult = String.format(
"Join the conversation at %s/index.php/call/%s\nPassword: %s", "Join the conversation at %s/index.php/call/%s\nPassword: %s",
@ -102,10 +88,4 @@ class ShareUtilsTest {
expectedResult, ShareUtils.getStringForIntent(context, password, userUtils, conversation) expectedResult, ShareUtils.getStringForIntent(context, password, userUtils, conversation)
) )
} }
@Test
@Throws(ParseException::class)
fun date() {
assertEquals(1207778138000, parseDate("Mon, 09 Apr 2008 23:55:38 GMT")?.time)
}
} }