forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Improve 'rewrite[Sync]Error' performance
* rename: 'rewriteError' -> 'rewriteSyncError' * remove 'DeckPicker' dependency * remove 'DeckPicker' dependency from tests * AndroidTest code copied from `RobolectricTesr` Affects methods: * verifyBadCodesNoMessage * verifyCodeMessages Outcomes: * Tests are not run twice * Tests do not need DeckPicker's `onCreate` etc... Stats: * Removes ~5s from targeted test run (startup costs) * Tests take ~50ms (down from ~225ms): ~900ms savings
- Loading branch information
1 parent
55f08d2
commit 83b0611
Showing
4 changed files
with
135 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,62 @@ | ||
/* | ||
* Copyright (c) 2023 David Allison <[email protected]> | ||
* | ||
* 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 com.ichi2.anki | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.ichi2.testutils.AndroidTest | ||
import com.ichi2.testutils.EmptyApplication | ||
import com.ichi2.testutils.getString | ||
import com.ichi2.testutils.targetContext | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.annotation.Config | ||
import kotlin.test.assertNull | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
@Config(application = EmptyApplication::class) | ||
class SyncTest : AndroidTest { | ||
|
||
@Test | ||
fun verifyCodeMessages() { | ||
val codeResponsePairs = hashMapOf( | ||
407 to getString(R.string.sync_error_407_proxy_required), | ||
409 to getString(R.string.sync_error_409), | ||
413 to getString(R.string.sync_error_413_collection_size), | ||
500 to getString(R.string.sync_error_500_unknown), | ||
501 to getString(R.string.sync_error_501_upgrade_required), | ||
502 to getString(R.string.sync_error_502_maintenance), | ||
503 to getString(R.string.sync_too_busy), | ||
504 to getString(R.string.sync_error_504_gateway_timeout) | ||
) | ||
|
||
for ((key, value) in codeResponsePairs) { | ||
Assert.assertEquals(getMessageFromSyncErrorCode(key), value) | ||
} | ||
} | ||
|
||
@Test | ||
fun verifyBadCodesNoMessage() { | ||
assertNull(getMessageFromSyncErrorCode(0)) | ||
assertNull(getMessageFromSyncErrorCode(-1)) | ||
assertNull(getMessageFromSyncErrorCode(1)) | ||
assertNull(getMessageFromSyncErrorCode(Int.MIN_VALUE)) | ||
assertNull(getMessageFromSyncErrorCode(Int.MAX_VALUE)) | ||
} | ||
|
||
private fun getMessageFromSyncErrorCode(key: Int) = getMessageFromSyncErrorCode(targetContext.resources, key) | ||
} |
61 changes: 61 additions & 0 deletions
61
AnkiDroid/src/test/java/com/ichi2/testutils/AndroidTest.kt
This file contains 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,61 @@ | ||
/* | ||
* Copyright (c) 2023 David Allison <[email protected]> | ||
* | ||
* 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 com.ichi2.testutils | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.ichi2.anki.AnkiDroidApp | ||
|
||
/** Marker interface for classes annotated with `@RunWith(AndroidJUnit4.class)` */ | ||
interface AndroidTest | ||
|
||
val AndroidTest.targetContext: Context | ||
get() { | ||
return try { | ||
ApplicationProvider.getApplicationContext() | ||
} catch (e: IllegalStateException) { | ||
if (e.message != null && e.message!!.startsWith("No instrumentation registered!")) { | ||
// Explicitly ignore the inner exception - generates line noise | ||
throw IllegalStateException("Annotate class: '${javaClass.simpleName}' with '@RunWith(AndroidJUnit4.class)'") | ||
} | ||
throw e | ||
} | ||
} | ||
|
||
/** | ||
* Returns an instance of [SharedPreferences] using the test context | ||
* @see [editPreferences] for editing | ||
*/ | ||
fun AndroidTest.getSharedPrefs(): SharedPreferences = AnkiDroidApp.getSharedPrefs(targetContext) | ||
|
||
fun AndroidTest.getString(res: Int): String = targetContext.getString(res) | ||
|
||
@Suppress("unused") | ||
fun AndroidTest.getQuantityString(res: Int, quantity: Int, vararg formatArgs: Any): String = | ||
targetContext.resources.getQuantityString(res, quantity, *formatArgs) | ||
|
||
/** | ||
* Allows editing of preferences, followed by a call to [apply][SharedPreferences.Editor.apply]: | ||
* | ||
* ``` | ||
* editPreferences { putString("key", value) } | ||
* ``` | ||
*/ | ||
fun AndroidTest.editPreferences(action: SharedPreferences.Editor.() -> Unit) = | ||
getSharedPrefs().edit(action = action) |