Merge pull request #1270 from Xefir/fix-unit-test

Fix tests
This commit is contained in:
Tlaster 2020-04-26 14:13:02 +08:00 committed by GitHub
commit 632dc57a9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 35 additions and 206 deletions

View File

@ -1,172 +0,0 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.Choreographer;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Created by mariotaku on 2017/8/20.
*/
public abstract class ChoreographerCompat {
private ChoreographerCompat() {
}
public abstract void postFrameCallback(ChoreographerCompat.FrameCallback callback);
public abstract void postFrameCallbackDelayed(ChoreographerCompat.FrameCallback callback, long delayMillis);
public abstract void removeFrameCallback(ChoreographerCompat.FrameCallback callback);
public static ChoreographerCompat getInstance() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
return ChoreographerCompatImpl.getCompatInstance();
}
return ChoreographerNativeDelegate.getNativeInstance();
}
public interface FrameCallback {
void doFrame(long frameTimeNanos);
}
private static class ChoreographerCompatImpl extends ChoreographerCompat {
private static final Map<Looper, ChoreographerCompat> sInstances = new WeakHashMap<>();
private final Handler handler;
ChoreographerCompatImpl(Looper looper) {
handler = new Handler(looper);
}
@Override
public void postFrameCallback(FrameCallback callback) {
handler.postDelayed(CompatFrameCallbackWrapper.wrap(callback), 0);
}
@Override
public void postFrameCallbackDelayed(FrameCallback callback, long delayMillis) {
handler.postDelayed(CompatFrameCallbackWrapper.wrap(callback), delayMillis + 16);
}
@Override
public void removeFrameCallback(FrameCallback callback) {
handler.removeCallbacks(CompatFrameCallbackWrapper.wrap(callback));
}
static ChoreographerCompat getCompatInstance() {
final Looper looper = Looper.myLooper();
ChoreographerCompat instance = sInstances.get(looper);
if (instance != null) return instance;
instance = new ChoreographerCompatImpl(looper);
sInstances.put(looper, instance);
return instance;
}
private static class CompatFrameCallbackWrapper implements Runnable {
private static final Map<FrameCallback, Runnable> sInstances = new WeakHashMap<>();
private final FrameCallback callback;
private CompatFrameCallbackWrapper(ChoreographerCompat.FrameCallback callback) {
this.callback = callback;
}
@Override
public void run() {
callback.doFrame(System.nanoTime());
}
static Runnable wrap(FrameCallback callback) {
Runnable wrapper = sInstances.get(callback);
if (wrapper != null) return wrapper;
wrapper = new CompatFrameCallbackWrapper(callback);
sInstances.put(callback, wrapper);
return wrapper;
}
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static class ChoreographerNativeDelegate extends ChoreographerCompat {
private static final Map<Choreographer, ChoreographerCompat> sInstances = new WeakHashMap<>();
private final Choreographer implementation;
ChoreographerNativeDelegate(Choreographer implementation) {
this.implementation = implementation;
}
public void postFrameCallback(ChoreographerCompat.FrameCallback callback) {
implementation.postFrameCallback(NativeFrameCallbackWrapper.wrap(callback));
}
public void postFrameCallbackDelayed(ChoreographerCompat.FrameCallback callback, long delayMillis) {
implementation.postFrameCallbackDelayed(NativeFrameCallbackWrapper.wrap(callback), delayMillis);
}
public void removeFrameCallback(ChoreographerCompat.FrameCallback callback) {
implementation.removeFrameCallback(NativeFrameCallbackWrapper.wrap(callback));
}
static ChoreographerCompat getNativeInstance() {
final Choreographer implementation = Choreographer.getInstance();
ChoreographerCompat instance = sInstances.get(implementation);
if (instance != null) return instance;
instance = new ChoreographerNativeDelegate(implementation);
sInstances.put(implementation, instance);
return instance;
}
private static class NativeFrameCallbackWrapper implements Choreographer.FrameCallback {
private static final Map<FrameCallback, Choreographer.FrameCallback> sInstances = new WeakHashMap<>();
private final FrameCallback callback;
private NativeFrameCallbackWrapper(ChoreographerCompat.FrameCallback callback) {
this.callback = callback;
}
@Override
public void doFrame(long frameTimeNanos) {
callback.doFrame(frameTimeNanos);
}
static Choreographer.FrameCallback wrap(ChoreographerCompat.FrameCallback callback) {
Choreographer.FrameCallback wrapper = sInstances.get(callback);
if (wrapper != null) return wrapper;
wrapper = new NativeFrameCallbackWrapper(callback);
sInstances.put(callback, wrapper);
return wrapper;
}
}
}
}

View File

@ -47,8 +47,8 @@ class ComposeActivityTest {
@Test
fun testReply() {
val context = InstrumentationRegistry.getContext()
val targetContext = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().context
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val status: ParcelableStatus = context.resources.getJsonResource(R.raw.parcelable_status_848051071444410368)
val intent = Intent(INTENT_ACTION_REPLY)
intent.setClass(targetContext, ComposeActivity::class.java)
@ -67,8 +67,8 @@ class ComposeActivityTest {
@Test
fun testReplyRemovedSomeMentions() {
val context = InstrumentationRegistry.getContext()
val targetContext = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().context
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val status: ParcelableStatus = context.resources.getJsonResource(R.raw.parcelable_status_848051071444410368)
val intent = Intent(INTENT_ACTION_REPLY)
intent.setClass(targetContext, ComposeActivity::class.java)
@ -87,8 +87,8 @@ class ComposeActivityTest {
@Test
fun testReplyNoMentions() {
val context = InstrumentationRegistry.getContext()
val targetContext = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().context
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val status: ParcelableStatus = context.resources.getJsonResource(R.raw.parcelable_status_848051071444410368)
val intent = Intent(INTENT_ACTION_REPLY)
intent.setClass(targetContext, ComposeActivity::class.java)
@ -109,8 +109,8 @@ class ComposeActivityTest {
@Test
fun testReplySelf() {
val context = InstrumentationRegistry.getContext()
val targetContext = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().context
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val status: ParcelableStatus = context.resources.getJsonResource(R.raw.parcelable_status_852737226718838790)
val intent = Intent(INTENT_ACTION_REPLY)
intent.setClass(targetContext, ComposeActivity::class.java)
@ -129,8 +129,8 @@ class ComposeActivityTest {
@Test
fun testReplySelfRemovedSomeMentions() {
val context = InstrumentationRegistry.getContext()
val targetContext = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().context
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val status: ParcelableStatus = context.resources.getJsonResource(R.raw.parcelable_status_852737226718838790)
val intent = Intent(INTENT_ACTION_REPLY)
intent.setClass(targetContext, ComposeActivity::class.java)
@ -149,8 +149,8 @@ class ComposeActivityTest {
@Test
fun testReplySelfNoMentions() {
val context = InstrumentationRegistry.getContext()
val targetContext = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().context
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val status: ParcelableStatus = context.resources.getJsonResource(R.raw.parcelable_status_852737226718838790)
val intent = Intent(INTENT_ACTION_REPLY)
intent.setClass(targetContext, ComposeActivity::class.java)
@ -174,7 +174,7 @@ class ComposeActivityTest {
private fun ComposeActivity.getStatusUpdateTest(checkLength: Boolean): ParcelableStatusUpdate {
val getStatusUpdate = javaClass.getDeclaredMethod("getStatusUpdate",
kotlin.Boolean::class.java).apply {
Boolean::class.java).apply {
isAccessible = true
}
return getStatusUpdate(this, checkLength) as ParcelableStatusUpdate

View File

@ -33,7 +33,7 @@ import java.util.*
class FileExtensionsTest {
@Test
fun testTempFileInputStream() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
val random = Random()
val testData = ByteArray(1024)
random.nextBytes(testData)

View File

@ -1,8 +1,9 @@
package org.mariotaku.twidere.extension.model
import android.media.RingtoneManager
import android.net.Uri
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
@ -19,7 +20,7 @@ import java.util.concurrent.TimeUnit
class DraftExtensionsTest {
@Test
fun testMimeMessageProcessing() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
val draft = Draft()
draft.action_type = Draft.Action.UPDATE_STATUS
draft.timestamp = System.currentTimeMillis()
@ -27,12 +28,12 @@ class DraftExtensionsTest {
draft.text = "Hello world 测试"
draft.location = ParcelableLocation(-11.956, 99.625) // Randomly generated
draft.media = arrayOf(
"file:///system/media/audio/ringtones/Atria.ogg",
"file:///system/media/audio/ringtones/Callisto.ogg",
"file:///system/media/audio/ringtones/Dione.ogg"
RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE),
RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION),
RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM)
).map { uri ->
ParcelableMediaUpdate().apply {
this.uri = uri
this.uri = uri.toString()
this.type = ParcelableMedia.Type.VIDEO
this.alt_text = String(CharArray(420).apply {
fill('A')
@ -58,7 +59,7 @@ class DraftExtensionsTest {
Assert.assertEquals(expected.type, actual.type)
val stl = context.contentResolver.openInputStream(Uri.parse(expected.uri))
val str = context.contentResolver.openInputStream(Uri.parse(actual.uri))
Assert.assertTrue(stl.contentEquals(str))
Assert.assertTrue(stl!!.contentEquals(str!!))
stl.close()
str.close()
}

View File

@ -42,7 +42,7 @@ class ExtractorExtensionsTest {
@Before
fun setUp() {
val context = InstrumentationRegistry.getContext()
val context = InstrumentationRegistry.getInstrumentation().context
// This is a tweet by @t_deyarmin, mentioning @nixcraft
inReplyTo = context.resources.openRawResource(R.raw.parcelable_status_848051071444410368).use {

View File

@ -23,7 +23,7 @@ class ParcelableStatusUtilsTest {
@Test
fun testFromStatus() {
val context = InstrumentationRegistry.getContext()
val context = InstrumentationRegistry.getInstrumentation().context
val status_8754050 = context.resources.openRawResource(R.raw.status_8754050).use {
val status = JsonSerializer.parse(it, Status::class.java)
return@use status.toParcelable(UserKey("1234567", "gnusocial.de"), AccountType.STATUSNET)

View File

@ -15,7 +15,7 @@ import org.junit.runner.RunWith
class TwidereDataStoreTest {
@Test
fun testBaseUris() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
val resolver = context.contentResolver
Assert.assertEquals(TwidereDataStore.BASE_CONTENT_URI, Uri.parse("content://twidere"))
Assert.assertNull(resolver.query(TwidereDataStore.CONTENT_URI_NULL, null, null, null, null))

View File

@ -11,7 +11,7 @@ class SaveFileTaskTest {
@Test
@Throws(Exception::class)
fun testGetFileNameWithExtension() {
assertEquals("pbs_twimg_com_media_abcdefghijklmn.jpg",
assertEquals("abcdefghijklmn.jpg",
SaveFileTask.getFileNameWithExtension("pbs_twimg_com_media_abcdefghijklmn_jpg",
"jpg", '_', null))
assertEquals("pbs_twimg_com_media_abcdefghijklmn_jpg",

View File

@ -12,13 +12,13 @@ import org.junit.runner.RunWith
class DataStoreUtilsTest {
@Test
fun testCleanDatabasesByItemLimit() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
DataStoreUtils.cleanDatabasesByItemLimit(context)
}
@Test
fun testGetAccountKeys() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
DataStoreUtils.getAccountKeys(context)
}
}

View File

@ -12,7 +12,7 @@ import org.junit.runner.RunWith
class MapFragmentFactoryTest {
@Test
fun testGetInstance() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
MapFragmentFactory.instance.createMapFragment(context = context)
}
}

View File

@ -24,7 +24,7 @@ class StatusShortenerInterfaceTest {
@Test
@FlakyTest
fun testConnection() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
val application = context.applicationContext as Application
val preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
val shortenerComponent = preferences.getString(TwidereConstants.KEY_STATUS_SHORTENER, null) ?: return

View File

@ -35,8 +35,8 @@ object TestAccountUtils {
private val accountResources = intArrayOf(R.raw.account_4223092274_twitter_com)
fun insertTestAccounts() {
val targetContext = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getContext()
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val context = InstrumentationRegistry.getInstrumentation().context
val am = AccountManager.get(targetContext)
val existingAccounts = AccountUtils.getAllAccountDetails(am, false)
accountResources.forEach { resId ->
@ -52,7 +52,7 @@ object TestAccountUtils {
}
fun removeTestAccounts() {
val targetContext = InstrumentationRegistry.getTargetContext()
val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
val am = AccountManager.get(targetContext)
val existingAccounts = AccountUtils.getAllAccountDetails(am, false)
existingAccounts.filter { it.test }.forEach { am.removeAccountSupport(it.account) }

View File

@ -1,6 +1,5 @@
package org.mariotaku.twidere.util
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith

View File

@ -15,7 +15,7 @@ import org.mariotaku.twidere.model.filter.UrlFiltersSubscriptionProviderArgument
class UrlFiltersSubscriptionProviderTest {
@Test
fun testFetchXml() {
val context = InstrumentationRegistry.getTargetContext()
val context = InstrumentationRegistry.getInstrumentation().targetContext
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (cm.activeNetworkInfo?.isConnected != true) return

View File

@ -148,6 +148,7 @@ abstract class SaveFileTask(
sb.append(name
.removeSuffix(extension)
.removeSuffix(".")
.removeSuffix(specialCharacter.toString())
.takeLastWhile { it != specialCharacter })
} else {
sb.append(name)