From e6f67af4cd3e42e55957ec406ca62de4b993c13c Mon Sep 17 00:00:00 2001 From: Colin Marsch Date: Tue, 2 Apr 2019 16:18:56 -0700 Subject: [PATCH] Convert android-instrumentation tests to Kotlin See #1205 --- .../InstrumentationLeakDetectorTest.java | 44 ----------------- .../InstrumentationLeakDetectorTest.kt | 48 +++++++++++++++++++ .../InstrumentationTestApplication.java | 11 ----- .../InstrumentationTestApplication.kt | 11 +++++ 4 files changed, 59 insertions(+), 55 deletions(-) delete mode 100644 leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.java create mode 100644 leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.kt delete mode 100644 leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.java create mode 100644 leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.kt diff --git a/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.java b/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.java deleted file mode 100644 index 6814c53573..0000000000 --- a/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.squareup.leakcanary; - -import java.util.Date; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -/** - * Tests that the {@link InstrumentationLeakDetector} can detect leaks - * in instrumentation tests - */ -public class InstrumentationLeakDetectorTest { - - private static Object leaking; - - @Before public void setUp() { - LeakCanary.installedRefWatcher().clearWatchedReferences(); - } - - @After public void tearDown() { - LeakCanary.installedRefWatcher().clearWatchedReferences(); - } - - @Test public void detectsLeak() { - leaking = new Date(); - RefWatcher refWatcher = LeakCanary.installedRefWatcher(); - refWatcher.watch(leaking); - - InstrumentationLeakDetector leakDetector = new InstrumentationLeakDetector(); - InstrumentationLeakResults results = leakDetector.detectLeaks(); - - if (results.getDetectedLeaks().size() != 1) { - throw new AssertionError("Expected exactly one leak, not " + results.getDetectedLeaks().size()); - } - - InstrumentationLeakResults.Result firstResult = results.getDetectedLeaks().get(0); - - String leakingClassName = firstResult.getAnalysisResult().getClassName(); - - if (!leakingClassName.equals(Date.class.getName())) { - throw new AssertionError("Expected a leak of Date, not " + leakingClassName); - } - } -} diff --git a/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.kt b/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.kt new file mode 100644 index 0000000000..3d075a0777 --- /dev/null +++ b/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationLeakDetectorTest.kt @@ -0,0 +1,48 @@ +package com.squareup.leakcanary + +import org.junit.After +import org.junit.Before +import org.junit.Test +import java.util.Date + +/** + * Tests that the [InstrumentationLeakDetector] can detect leaks + * in instrumentation tests + */ +class InstrumentationLeakDetectorTest { + + @Before fun setUp() { + LeakCanary.installedRefWatcher() + .clearWatchedReferences() + } + + @After fun tearDown() { + LeakCanary.installedRefWatcher() + .clearWatchedReferences() + } + + @Test fun detectsLeak() { + leaking = Date() + val refWatcher = LeakCanary.installedRefWatcher() + refWatcher.watch(leaking) + + val leakDetector = InstrumentationLeakDetector() + val results = leakDetector.detectLeaks() + + if (results.detectedLeaks.size != 1) { + throw AssertionError("Expected exactly one leak, not ${results.detectedLeaks.size}") + } + + val firstResult = results.detectedLeaks[0] + + val leakingClassName = firstResult.analysisResult.className + + if (leakingClassName != Date::class.java.name) { + throw AssertionError("Expected a leak of Date, not $leakingClassName") + } + } + + companion object { + private var leaking: Any? = null + } +} diff --git a/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.java b/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.java deleted file mode 100644 index 8258e79ebe..0000000000 --- a/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.squareup.leakcanary; - -import android.app.Application; - -public class InstrumentationTestApplication extends Application { - @Override public void onCreate() { - super.onCreate(); - InstrumentationLeakDetector.Companion.instrumentationRefWatcher(this) - .buildAndInstall(); - } -} diff --git a/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.kt b/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.kt new file mode 100644 index 0000000000..a9ced6b9d1 --- /dev/null +++ b/leakcanary-android-instrumentation/src/androidTest/java/com/squareup/leakcanary/InstrumentationTestApplication.kt @@ -0,0 +1,11 @@ +package com.squareup.leakcanary + +import android.app.Application + +class InstrumentationTestApplication : Application() { + override fun onCreate() { + super.onCreate() + InstrumentationLeakDetector.instrumentationRefWatcher(this) + .buildAndInstall() + } +}