Skip to content

Commit

Permalink
Add ViewPump.reset() function (#75)
Browse files Browse the repository at this point in the history
* Add ViewPump.reset() function

This allows for tearing down the viewpump singleton instance in tests.

* Make init not nullable
  • Loading branch information
ZacSweers committed Mar 29, 2023
1 parent db3ba26 commit 13e9ba7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions viewpump/src/main/java/io/github/inflationx/viewpump/ViewPump.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,16 @@ class ViewPump private constructor(
}

@JvmStatic
fun init(viewPump: ViewPump?) {
fun init(viewPump: ViewPump) {
INSTANCE = viewPump
}

/** Resets the current singleton instance. This should usually only be used for testing. */
@JvmStatic
fun reset() {
INSTANCE = null
}

@JvmStatic
@MainThread
fun get(): ViewPump {
Expand All @@ -177,7 +183,7 @@ class ViewPump private constructor(
* @return The processed view, which might not necessarily be the same type as clazz.
*/
@JvmStatic
fun create(context: Context, clazz: Class<out View>, attrs :AttributeSet): View? {
fun create(context: Context, clazz: Class<out View>, attrs: AttributeSet): View? {
return get()
.inflate(InflateRequest(
context = context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.github.inflationx.viewpump.util.TestFallbackViewCreator;
import io.github.inflationx.viewpump.util.TestPostInflationInterceptor;
import io.github.inflationx.viewpump.util.TestView;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -37,7 +39,11 @@ public class ViewPumpTest {
@Before
public void setup() {
MockitoAnnotations.openMocks(this);
ViewPump.init(null);
}

@After
public void tearDown() {
ViewPump.reset();
}

@Test
Expand Down Expand Up @@ -273,4 +279,22 @@ public void createView_withPostInflationInterceptor_shouldReturnPostProcessedVie
assertThat(((TestView) view).isSameContextAs(mockContext)).isTrue();
assertThat(((TestView) view).isPostProcessed()).isTrue();
}

@Test
public void reset() {
ViewPump first = ViewPump.builder()
.addInterceptor(new TestPostInflationInterceptor())
.build();
ViewPump.init(first);

assertThat(ViewPump.get())
.isSameAs(first);

// Now reset
ViewPump.reset();

// Now it's cleared the previously installed one
assertThat(ViewPump.get())
.isNotSameAs(first);
}
}

0 comments on commit 13e9ba7

Please sign in to comment.