Skip to content

Commit

Permalink
wrap Activity and Compose rules in LeakCanary checks on both sides
Browse files Browse the repository at this point in the history
fixes #657
  • Loading branch information
RBusarow committed Feb 2, 2022
1 parent 85718c3 commit 8c3042d
Show file tree
Hide file tree
Showing 28 changed files with 84 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -18,9 +18,9 @@ import org.junit.runner.RunWith
class HelloComposeTest {

private val composeRule = createAndroidComposeRule<HelloComposeActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@Test fun togglesBetweenStates() {
composeRule.onNodeWithText("Hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -18,9 +18,9 @@ import org.junit.runner.RunWith
class HelloBindingTest {

private val composeRule = createAndroidComposeRule<HelloBindingActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@Test fun togglesBetweenStates() {
composeRule.onNodeWithText("Hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -18,9 +18,9 @@ import org.junit.runner.RunWith
class HelloComposeWorkflowTest {

private val composeRule = createAndroidComposeRule<HelloComposeWorkflowActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@Test fun togglesBetweenStates() {
composeRule.onNodeWithText("Hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -19,9 +19,9 @@ import org.junit.runner.RunWith
class InlineRenderingTest {

private val composeRule = createAndroidComposeRule<InlineRenderingActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@Test fun counterIncrements() {
composeRule.onNode(hasClickAction())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.squareup.sample.compose.R
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -24,9 +24,9 @@ import org.junit.runner.RunWith
class SampleLauncherTest {

private val composeRule = createAndroidComposeRule<SampleLauncherActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@OptIn(ExperimentalTestApi::class)
@Test fun allSamplesLaunch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -24,9 +24,9 @@ private const val ADD_BUTTON_TEXT = "Add Child"
class NestedRenderingsTest {

private val composeRule = createAndroidComposeRule<NestedRenderingsActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@Test fun childrenAreAddedAndRemoved() {
composeRule.onNodeWithText(ADD_BUTTON_TEXT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -18,9 +18,9 @@ import org.junit.runner.RunWith
class PreviewTest {

private val composeRule = createAndroidComposeRule<PreviewActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@Test fun showsPreviewRendering() {
composeRule.onNodeWithText(ContactDetailsRendering::class.java.simpleName, substring = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.test.performTextReplacement
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
Expand All @@ -22,9 +22,9 @@ import org.junit.runner.RunWith
class TextInputTest {

private val composeRule = createAndroidComposeRule<TextInputActivity>()
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess())
.around(IdleAfterTestRule)
@get:Rule val rules: RuleChain = RuleChain.outerRule(IdleAfterTestRule)
.around(composeRule)
.wrapInLeakCanary()

@OptIn(ExperimentalTestApi::class)
@Test fun allowsTextEditing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.sample.container.poetryapp.R
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class PoetryAppTest {

private val scenarioRule = ActivityScenarioRule(PoetryActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()

@Test fun launches() {
inAnyView(withText(R.string.poems))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class RavenAppTest {

private val scenarioRule = ActivityScenarioRule(RavenActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()

@Test fun launches() {
inAnyView(withText("The Raven"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.actuallyPressBack
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class HelloBackButtonEspressoTest {

private val scenarioRule = ActivityScenarioRule(HelloBackButtonActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()

@Test fun wrappedTakesPrecedence() {
inAnyView(withId(R.id.hello_message)).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class DungeonAppTest {

private val scenarioRule = ActivityScenarioRule(DungeonActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()

@Test fun loadsBoardsList() {
inAnyView(withText(R.string.boards_list_label))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.hamcrest.Matchers.containsString
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class HelloWorkflowFragmentAppTest {

private val scenarioRule = ActivityScenarioRule(HelloWorkflowFragmentActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()

@Test fun togglesHelloAndGoodbye() {
inAnyView(withText(containsString("Hello")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class HelloWorkflowAppTest {

private val scenarioRule = ActivityScenarioRule(HelloWorkflowActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()

@Test fun togglesHelloAndGoodbye() {
inAnyView(withText("Hello"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.hamcrest.CoreMatchers.not
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
internal class StubVisibilityAppTest {

private val scenarioRule = ActivityScenarioRule(StubVisibilityActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()

@Test fun togglesFooter() {
inAnyView(withId(R.id.should_be_wrapped))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.environment
import com.squareup.workflow1.ui.getRendering
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.actuallyPressBack
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith
import java.util.concurrent.atomic.AtomicReference

Expand All @@ -46,7 +45,7 @@ import java.util.concurrent.atomic.AtomicReference
class TicTacToeEspressoTest {

private val scenarioRule = ActivityScenarioRule(TicTacToeActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()
private val scenario get() = scenarioRule.scenario

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ import androidx.test.uiautomator.UiDevice
import com.squareup.sample.todo.R
import com.squareup.sample.todo.ToDoActivity
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.internal.test.DetectLeaksAfterTestSuccess
import com.squareup.workflow1.ui.internal.test.actuallyPressBack
import com.squareup.workflow1.ui.internal.test.inAnyView
import com.squareup.workflow1.ui.internal.test.wrapInLeakCanary
import org.hamcrest.Matchers.allOf
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@OptIn(WorkflowUiExperimentalApi::class)
class TodoAppTest {

private val scenarioRule = ActivityScenarioRule(ToDoActivity::class.java)
@get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!!
@get:Rule val rules = scenarioRule.wrapInLeakCanary()
private val uiDevice by lazy { UiDevice.getInstance(getInstrumentation()) }

@Before
Expand Down
Loading

0 comments on commit 8c3042d

Please sign in to comment.