Skip to content

Enable more disabled ktlint rules #3540

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

Merged
merged 6 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ ij_kotlin_allow_trailing_comma=true

[*]
insert_final_newline = true
max_line_length = 120 # same as detekt.yml

# rules we should look into enabling
ktlint_standard_blank-line-before-declaration = disabled
ktlint_standard_chain-method-continuation = disabled
ktlint_standard_class-signature = disabled
ktlint_standard_function-expression-body = disabled
ktlint_standard_function-naming = disabled
ktlint_standard_function-signature = disabled
ktlint_standard_indent = disabled
ktlint_standard_multiline-expression-wrapping = disabled
ktlint_standard_no-empty-first-line-in-class-body = disabled
ktlint_standard_string-template-indent = disabled
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import android.content.Context
import androidx.test.runner.AndroidJUnitRunner

class FirefoxTestRunner : AndroidJUnitRunner() {

override fun newApplication(cl: ClassLoader?, className: String?, context: Context?): Application {
return super.newApplication(cl, FirefoxTestApplication::class.java.name, context)
}
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?,
): Application = super.newApplication(cl, FirefoxTestApplication::class.java.name, context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import org.mozilla.reference.browser.helpers.TestAssetHelper.waitingTime
*
* @throws AssertionError if no [UiObject2] matches the condition
*/
fun UiDevice.waitAndInteract(condition: SearchCondition<UiObject2>, interaction: UiObject2.() -> Unit) {
fun UiDevice.waitAndInteract(
condition: SearchCondition<UiObject2>,
interaction: UiObject2.() -> Unit,
) {
val obj = this.wait(condition, waitingTime)
assertNotNull(obj)
interaction.invoke(obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import org.junit.Assert.assertEquals
* pixel data are the same (within the provided delta): this is the same metrics that
* [Bitmap.sameAs] uses.
*/
fun assertEqualsWithDelta(expectedB: Bitmap, actualB: Bitmap, delta: Float) {
fun assertEqualsWithDelta(
expectedB: Bitmap,
actualB: Bitmap,
delta: Float,
) {
assertEquals("widths should be equal", expectedB.width, actualB.width)
assertEquals("heights should be equal", expectedB.height, actualB.height)
assertEquals("config should be equal", expectedB.config, actualB.config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class BrowserActivityTestRule(
initialTouchMode: Boolean = false,
launchActivity: Boolean = true,
) : ActivityTestRule<BrowserActivity>(BrowserActivity::class.java, initialTouchMode, launchActivity) {

/**
* Ensures the test doesn't advance until session page load is completed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ fun isSelected(isSelected: Boolean): Matcher<View> = maybeInvertMatcher(espresso
*
* @param matcher The matcher that our view cousins will be checked against to
*/
fun hasCousin(matcher: Matcher<View>): Matcher<View> = object : BaseMatcher<View>() {
fun hasCousin(matcher: Matcher<View>): Matcher<View> =
object : BaseMatcher<View>() {
override fun describeTo(description: Description?) {
description?.apply {
appendText("has cousin that matches: ")
Expand All @@ -54,15 +55,20 @@ fun hasCousin(matcher: Matcher<View>): Matcher<View> = object : BaseMatcher<View
?.count() == 1
}

private fun matchChildren(matcher: Matcher<View>): (View?) -> Boolean = {
private fun matchChildren(matcher: Matcher<View>): (View?) -> Boolean =
{
(it as? ViewGroup)
?.children
?.filter { v -> matcher.matches(v) }
?.count() == 1
}
}

private fun maybeInvertMatcher(matcher: Matcher<View>, useUnmodifiedMatcher: Boolean): Matcher<View> = when {
private fun maybeInvertMatcher(
matcher: Matcher<View>,
useUnmodifiedMatcher: Boolean,
): Matcher<View> =
when {
useUnmodifiedMatcher -> matcher
else -> not(matcher)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import java.io.IOException
import java.io.InputStream

object MockWebServerHelper {

fun initMockWebServerAndReturnEndpoints(vararg messages: String): List<Uri> {
val mockServer = MockWebServer()
var uniquePath = 0
Expand Down Expand Up @@ -48,7 +47,9 @@ class AndroidAssetDispatcher : Dispatcher() {
private val mainThreadHandler = Handler(Looper.getMainLooper())

override fun dispatch(request: RecordedRequest): MockResponse {
val assetManager = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().context.assets
val assetManager = androidx.test.platform.app.InstrumentationRegistry
.getInstrumentation()
.context.assets
try {
val pathWithoutQueryParams = Uri.parse(request.path!!.drop(1)).path
assetManager.open(pathWithoutQueryParams!!).use { inputStream ->
Expand All @@ -64,12 +65,14 @@ class AndroidAssetDispatcher : Dispatcher() {
}

@Throws(IOException::class)
private fun fileToResponse(path: String, file: InputStream): MockResponse {
return MockResponse()
private fun fileToResponse(
path: String,
file: InputStream,
): MockResponse =
MockResponse()
.setResponseCode(HTTP_OK)
.setBody(fileToBytes(file)!!)
.addHeader("content-type: " + contentType(path))
}

@Throws(IOException::class)
private fun fileToBytes(file: InputStream): Buffer? {
Expand All @@ -78,8 +81,8 @@ private fun fileToBytes(file: InputStream): Buffer? {
return result
}

private fun contentType(path: String): String? {
return when {
private fun contentType(path: String): String? =
when {
path.endsWith(".png") -> "image/png"
path.endsWith(".jpg") -> "image/jpeg"
path.endsWith(".jpeg") -> "image/jpeg"
Expand All @@ -89,4 +92,3 @@ private fun contentType(path: String): String? {
path.endsWith(".txt") -> "text/plain; charset=utf-8"
else -> "application/octet-stream"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import org.junit.runner.Description
import org.junit.runners.model.Statement
import java.lang.AssertionError

class RetryTestRule(private val retryCount: Int = 5) : TestRule {

override fun apply(base: Statement, description: Description): Statement {
return statement {
class RetryTestRule(
private val retryCount: Int = 5,
) : TestRule {
override fun apply(
base: Statement,
description: Description,
): Statement =
statement {
for (i in 1..retryCount) {
try {
base.evaluate()
Expand All @@ -34,11 +38,9 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule {
}
}
}
}

private inline fun statement(crossinline eval: () -> Unit): Statement {
return object : Statement() {
private inline fun statement(crossinline eval: () -> Unit): Statement =
object : Statement() {
override fun evaluate() = eval()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ import org.mozilla.reference.browser.BrowserApplication
class SessionLoadedIdlingResource : IdlingResource {
private var resourceCallback: IdlingResource.ResourceCallback? = null

override fun getName(): String {
return SessionLoadedIdlingResource::class.java.simpleName
}
override fun getName(): String = SessionLoadedIdlingResource::class.java.simpleName

override fun isIdleNow(): Boolean {
val context = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
as BrowserApplication

val store = context.components.core.store

return if (store.state.selectedTab?.content?.loading == true) {
return if (store.state.selectedTab
?.content
?.loading == true
) {
false
} else {
invokeCallback()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import java.util.concurrent.TimeUnit
object TestAssetHelper {
val waitingTime: Long = TimeUnit.SECONDS.toMillis(15)
val waitingTimeShort: Long = TimeUnit.SECONDS.toMillis(1)
data class TestAsset(val url: Uri, val content: String, val title: String)

data class TestAsset(
val url: Uri,
val content: String,
val title: String,
)

/**
* Hosts 3 simple websites, found at androidTest/assets/pages/generic[1|2|3].html
Expand All @@ -25,17 +30,19 @@ object TestAssetHelper {
* Content for these pages all follow the same pattern. See [generic1.html] for
* content implementation details.
*/
fun getGenericAssets(server: MockWebServer): List<TestAsset> {
return (1..4).map {
fun getGenericAssets(server: MockWebServer): List<TestAsset> =
(1..4).map {
TestAsset(
server.url("pages/generic$it.html").toString().toUri()!!,
"Page content: $it",
"",
)
}
}

fun getGenericAsset(server: MockWebServer, pageNum: Int): TestAsset {
fun getGenericAsset(
server: MockWebServer,
pageNum: Int,
): TestAsset {
val url = server.url("pages/generic$pageNum.html").toString().toUri()!!
val content = "Page content: $pageNum"
val title = "Test_Page_$pageNum"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import org.mozilla.reference.browser.helpers.TestAssetHelper.waitingTimeShort
import org.mozilla.reference.browser.ui.robots.mDevice

object TestHelper {

val packageName = InstrumentationRegistry.getInstrumentation().targetContext.packageName
val appContext: Context = InstrumentationRegistry.getInstrumentation().targetContext
val appName = appContext.appName
Expand All @@ -38,30 +37,30 @@ object TestHelper {
return appView
}

fun getPermissionAllowID(): String {
return when (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
fun getPermissionAllowID(): String =
when (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
true -> "com.android.permissioncontroller"
false -> "com.android.packageinstaller"
}
}

fun createCustomTabIntent(
pageUrl: String,
customActionButtonDescription: String = "",
): Intent {
val appContext = InstrumentationRegistry.getInstrumentation()
val appContext = InstrumentationRegistry
.getInstrumentation()
.targetContext
.applicationContext
val pendingIntent = PendingIntent.getActivity(appContext, 0, Intent(), 0)
val customTabsIntent = CustomTabsIntent.Builder()
val customTabsIntent = CustomTabsIntent
.Builder()
.setShareState(CustomTabsIntent.SHARE_STATE_ON)
.setActionButton(
createTestBitmap(),
customActionButtonDescription,
pendingIntent,
true,
)
.build()
).build()
customTabsIntent.intent.data = Uri.parse(pageUrl)
return customTabsIntent.intent
}
Expand All @@ -73,21 +72,27 @@ object TestHelper {
return bitmap
}

fun UiDevice.waitForObjects(obj: UiObject, waitingTime: Long = TestAssetHelper.waitingTime) {
fun UiDevice.waitForObjects(
obj: UiObject,
waitingTime: Long = TestAssetHelper.waitingTime,
) {
this.waitForIdle()
Assert.assertNotNull(obj.waitForExists(waitingTime))
}

fun itemWithResId(resourceId: String) =
mDevice.findObject(UiSelector().resourceId(resourceId))
fun itemWithResId(resourceId: String) = mDevice.findObject(UiSelector().resourceId(resourceId))

fun itemWithText(itemText: String) =
mDevice.findObject(UiSelector().text(itemText))
fun itemWithText(itemText: String) = mDevice.findObject(UiSelector().text(itemText))

fun itemWithResIdContainingText(resourceId: String, text: String) =
mDevice.findObject(UiSelector().resourceId(resourceId).textContains(text))
fun itemWithResIdContainingText(
resourceId: String,
text: String,
) = mDevice.findObject(UiSelector().resourceId(resourceId).textContains(text))

fun assertUIObjectExists(vararg appItems: UiObject, exists: Boolean = true) {
fun assertUIObjectExists(
vararg appItems: UiObject,
exists: Boolean = true,
) {
if (exists) {
for (appItem in appItems) {
assertTrue(appItem.waitForExists(waitingTime))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import android.net.Uri

// Extension functions for the String class

fun String?.toUri(): Uri? = if (this == null) {
fun String?.toUri(): Uri? =
if (this == null) {
null
} else {
Uri.parse(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ import androidx.test.espresso.assertion.ViewAssertions.matches

fun ViewInteraction.click(): ViewInteraction = this.perform(ViewActions.click())!!

fun ViewInteraction.assertIsEnabled(isEnabled: Boolean): ViewInteraction {
return this.check(matches(isEnabled(isEnabled)))!!
}
fun ViewInteraction.assertIsEnabled(isEnabled: Boolean): ViewInteraction = this.check(matches(isEnabled(isEnabled)))!!

fun ViewInteraction.assertIsChecked(isChecked: Boolean): ViewInteraction {
return this.check(matches(isChecked(isChecked)))!!
}
fun ViewInteraction.assertIsChecked(isChecked: Boolean): ViewInteraction = this.check(matches(isChecked(isChecked)))!!

fun ViewInteraction.assertIsSelected(isSelected: Boolean): ViewInteraction {
return this.check(matches(isSelected(isSelected)))!!
}
fun ViewInteraction.assertIsSelected(isSelected: Boolean): ViewInteraction =
this.check(matches(isSelected(isSelected)))!!
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ class TabMatcher<T : View>(
}

companion object {
fun withText(text: String): Matcher<View> {
return TabMatcher<View>(
fun withText(text: String): Matcher<View> =
TabMatcher<View>(
R.id.mozac_browser_tabstray_title,
) {
(it as TextView).text == text
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.mozilla.reference.browser.helpers.RetryTestRule
import org.mozilla.reference.browser.ui.robots.navigationToolbar

class AddonsTest {

private lateinit var mockWebServer: MockWebServer

@get:Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.mozilla.reference.browser.ui.robots.browser
import org.mozilla.reference.browser.ui.robots.navigationToolbar

class ContextMenusTest {

private lateinit var mockWebServer: MockWebServer

@get:Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.mozilla.reference.browser.helpers.TestHelper.createCustomTabIntent
import org.mozilla.reference.browser.ui.robots.customTabScreen

class CustomTabsTest {

private lateinit var mockWebServer: MockWebServer

@get:Rule
Expand Down
Loading