Created
March 12, 2015 02:45
-
-
Save dingzhihu/b9995493fa71604d77c7 to your computer and use it in GitHub Desktop.
test loader using robolectric
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
public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<String> { | |
public boolean onCreateLoaderExecuted; | |
public boolean onLoadFinishedExecuted; | |
public boolean loadInBackgroundExecuted; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
getSupportLoaderManager().initLoader(0, null, this); | |
} | |
@Override | |
public Loader<String> onCreateLoader(int i, Bundle bundle) { | |
onCreateLoaderExecuted = true; | |
return new AsyncTaskLoader<String>(this) { | |
@Override | |
public String loadInBackground() { | |
loadInBackgroundExecuted = true; | |
return "result"; | |
} | |
}; | |
} | |
@Override | |
public void onLoadFinished(Loader<String> stringLoader, String s) { | |
onLoadFinishedExecuted = true; | |
} | |
@Override | |
public void onLoaderReset(Loader<String> stringLoader) { | |
} | |
} |
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
@RunWith(RobolectricTestRunner.class) | |
@Config(emulateSdk = 18) | |
public class MainActivityTest { | |
@Test | |
public void test() throws Exception { | |
MainActivity mainActivity = Robolectric.buildActivity(MainActivity.class).create().start().resume().visible().get(); | |
assertTrue(mainActivity.onCreateLoaderExecuted); | |
assertTrue(mainActivity.loadInBackgroundExecuted); //fail | |
assertTrue(mainActivity.onLoadFinishedExecuted); //fail | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment