Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert leakcanary-sample to Kotlin (#1229)
See #1205
- Loading branch information
1 parent
b1eee07
commit 9034840
Showing
12 changed files
with
153 additions
and
159 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
12 changes: 0 additions & 12 deletions
12
.../src/androidTest/java/com/example/leakcanary/tests/InstrumentationExampleApplication.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
...le/src/androidTest/java/com/example/leakcanary/tests/InstrumentationExampleApplication.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,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() | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
leakcanary-sample/src/androidTest/java/com/example/leakcanary/tests/TuPeuxPasTest.java
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
leakcanary-sample/src/androidTest/java/com/example/leakcanary/tests/TuPeuxPasTest.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,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))) | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
leakcanary-sample/src/main/java/com/example/leakcanary/ExampleApplication.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
leakcanary-sample/src/main/java/com/example/leakcanary/ExampleApplication.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,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()) | ||
} | ||
} | ||
} |
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
20 changes: 0 additions & 20 deletions
20
leakcanary-sample/src/test/java/com/example/leakcanary/SampleTest.java
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
leakcanary-sample/src/test/java/com/example/leakcanary/SampleTest.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,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() | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
leakcanary-sample/src/test/java/com/example/leakcanary/TestExampleApplication.java
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
leakcanary-sample/src/test/java/com/example/leakcanary/TestExampleApplication.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,7 @@ | ||
package com.example.leakcanary | ||
|
||
class TestExampleApplication : ExampleApplication() { | ||
override fun setupLeakCanary() { | ||
// No leakcanary in unit tests. | ||
} | ||
} |