Skip to content

Deprecate TestLifecycleOwner #433

Merged
ZacSweers merged 5 commits into
mainfrom
z/deprecate
Aug 9, 2021
Merged

Deprecate TestLifecycleOwner #433
ZacSweers merged 5 commits into
mainfrom
z/deprecate

Conversation

/**
* Extension that returns a [TestLifecycleOwner] for this [LifecycleRegistry].
*/
@Deprecated("Switch to androidx.lifecycle.testing.TestLifecycleOwner")
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally not using ERROR since there's no immediate replaceWith option


// Spin it up
TestLifecycleOwner lifecycle = TestLifecycleOwner.create();
TestLifecycleOwner lifecycle = new TestLifecycleOwner();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default initialState is Lifecycle.State.STARTED - you'll want to use new TestLifecycleOwner(LifecycleState.INITIALIZING) if you want to manually move through each lifecycle state.

Same for all other uses.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat!

import androidx.test.annotation.UiThreadTest;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import autodispose2.androidx.lifecycle.test.TestLifecycleOwner;
import androidx.lifecycle.testing.TestLifecycleOwner;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if you care about sorting imports, but I would expect this to be sorted above the androidx.test imports.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, will let spotless clean this up

Comment on lines +48 to +51
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that handleLifecycleEvent(Lifecycle.Event.ON_RESUME) will catch the Lifecycle up through all events, so there's no technical reason to call handleLifecycleEvent multiple times in a row like this.

You might also consider using lifecycle.currentState = Lifecycle.State.RESUMED, which does the exact same thing.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat!

Comment on lines +106 to +109
TestLifecycleOwner lifecycle = new TestLifecycleOwner();
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the default state of TestLifecycleOwner of STARTED means you can remove the ON_CREATE and ON_START calls here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, good catch

@ZacSweers ZacSweers merged commit e5b4c32 into main Aug 9, 2021
@ZacSweers ZacSweers deleted the z/deprecate branch August 9, 2021 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants