Skip to content

Commit

Permalink
Convert leakcanary-sample to Kotlin (#1229)
Browse files Browse the repository at this point in the history
See #1205
  • Loading branch information
colinmarsch authored and pyricau committed Apr 1, 2019
1 parent b1eee07 commit 9034840
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 159 deletions.
2 changes: 1 addition & 1 deletion leakcanary-sample/build.gradle
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'

dependencies {
implementation deps.kotlin.stdLib.jdk8
debugImplementation project(':leakcanary-android')
implementation project(':leakcanary-android')

testImplementation "junit:junit:4.12"
testImplementation "org.robolectric:robolectric:4.0-alpha-3"
Expand Down

This file was deleted.

@@ -0,0 +1,12 @@
package com.example.leakcanary.tests

import com.example.leakcanary.ExampleApplication
import com.squareup.leakcanary.InstrumentationLeakDetector

class InstrumentationExampleApplication : ExampleApplication() {

override fun setupLeakCanary() {
InstrumentationLeakDetector.instrumentationRefWatcher(this)
.buildAndInstall()
}
}

This file was deleted.

@@ -0,0 +1,52 @@
package com.example.leakcanary.tests

import androidx.test.rule.ActivityTestRule
import com.example.leakcanary.MainActivity
import com.example.leakcanary.R
import org.junit.Rule
import org.junit.Test

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText

/**
* This UI test looks like it should succeed, but it will actually fail because
* it triggers a leak.
*
* Run this test with:
*
* ./gradlew leakcanary-sample:connectedCheck
*
* To set this up, we installed a special RefWatcher dedicated to detecting leaks in
* instrumentation tests in [InstrumentationExampleApplication], and then added the FailTestOnLeakRunListener
* to the config of our build.gradle:
*
* testInstrumentationRunnerArgument "listener", "com.squareup.leakcanary.FailTestOnLeakRunListener"
*
*
* Why is this class named "TuPeuxPasTest"?
*
* This test fails, intentionally. In French, "Tu peux pas test" could mean "you cannot test"
* written with poor grammar. Except, that's not what it means.
* If you're curious, interested in French and have time to waste:
* https://www.youtube.com/watch?v=DZZpbmAc-0A
* https://www.youtube.com/watch?v=nHeAA6X-XUQ
*/
class TuPeuxPasTest {

@Rule
var activityRule = ActivityTestRule(MainActivity::class.java)

@Test
fun clickAsyncWork() {
onView(withId(R.id.async_work)).perform(click())
}

@Test
fun asyncButtonHasStartText() {
onView(withId(R.id.async_work)).check(matches(withText(R.string.start_async_work)))
}
}

This file was deleted.

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.leakcanary

import android.app.Application
import android.os.Build
import android.os.StrictMode
import com.squareup.leakcanary.LeakCanary

open class ExampleApplication : Application() {
override fun onCreate() {
super.onCreate()
setupLeakCanary()
}

protected open fun setupLeakCanary() {
enabledStrictMode()
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return
}
LeakCanary.install(this)
}

companion object {
private fun enabledStrictMode() {
val builder = StrictMode.ThreadPolicy.Builder()
// Disabled DiskReadViolation, see https://github.com/square/leakcanary/issues/1222
// builder.detectDiskReads();
builder.detectDiskWrites()
builder.detectNetwork()
builder.detectCustomSlowCalls()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
builder.detectResourceMismatches()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.detectUnbufferedIo()
}
builder.penaltyLog()
builder.penaltyDeath()
StrictMode.setThreadPolicy(builder.build())
}
}
}
Expand Up @@ -13,18 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.leakcanary;
package com.example.leakcanary

import android.view.View;
import android.view.View

/**
* Fake class for the purpose of demonstrating a leak.
*/
public class HttpRequestHelper {

private final View button;

HttpRequestHelper(View button) {
this.button = button;
}
}
class HttpRequestHelper internal constructor(private val button: View)

This file was deleted.

@@ -0,0 +1,20 @@
package com.example.leakcanary

import android.view.View
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(application = TestExampleApplication::class)
class SampleTest {
@Test
fun testTheThing() {
val controller = Robolectric.buildActivity(MainActivity::class.java).create().start().resume().visible()
controller.get().findViewById<View>(R.id.async_work).performClick()
controller.stop()
controller.destroy()
}
}

This file was deleted.

@@ -0,0 +1,7 @@
package com.example.leakcanary

class TestExampleApplication : ExampleApplication() {
override fun setupLeakCanary() {
// No leakcanary in unit tests.
}
}

0 comments on commit 9034840

Please sign in to comment.