-
Notifications
You must be signed in to change notification settings - Fork 79
Fix bug where onDetach is called before onAttach #66
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.lyft.scoop; | ||
|
||
import android.content.Context; | ||
import android.view.View; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
|
||
import static junit.framework.Assert.assertFalse; | ||
import static junit.framework.Assert.assertTrue; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE) | ||
public class ViewControllerTest { | ||
private static final String TEST_RESULT = "TEST_RESULT"; | ||
|
||
private MockViewController viewController; | ||
private View mockView; | ||
|
||
@Before | ||
public void setUp() { | ||
viewController = new MockViewController(); | ||
mockView = new TestView(RuntimeEnvironment.application); | ||
} | ||
@Test | ||
public void onAttachFirst() { | ||
viewController.attach(mockView); | ||
assertTrue(viewController.attached()); | ||
viewController.detach(mockView); | ||
assertFalse(viewController.attached()); | ||
} | ||
|
||
@Test | ||
public void onDetachFirst() { | ||
viewController.detach(mockView); | ||
assertFalse(viewController.attached()); | ||
viewController.attach(mockView); | ||
assertFalse(viewController.attached()); | ||
} | ||
|
||
private class MockViewController extends ViewController { | ||
private String variable = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @itspbj call this MockViewController. Use public fields instead private |
||
|
||
@Override | ||
public void onAttach() { | ||
variable = TEST_RESULT; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @itspbj what this varialbe is used for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lexer Simulate a variable being used in onDetach that was initialized in onAttach. In our code this throws an NPE in some cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @itspbj ok |
||
} | ||
|
||
@Override | ||
public void onDetach() { | ||
variable.toString(); | ||
} | ||
|
||
@Override | ||
protected int layoutId() { | ||
return 0; | ||
} | ||
} | ||
|
||
static class TestView extends View { | ||
|
||
public TestView(Context context) { | ||
super(context); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@itspbj Do we use attached anywhere in the code right now? may be delete it.