0

I have a unit test, and after I destroy and finish the app, I call getActivity() again to verify it loads correctly. However, it is not calling onCreate()!

public void testHistory() { 
            ...
    mActivity.onPause(); 
    mActivity.finish(); 
    assertTrue(mActivity.getExtractor() == null);
    assertTrue(mActivity.getSettings() == null);
    Log.d(TAG, "**************  Restarting app to verify load.");
    mActivity = getActivity(); 
    assertTrue(mActivity != null);
    assertTrue(mActivity.getExtractor() != null); //////// THIS FAILS!!!

Here is the log

10-07 21:11:40.467: D/SpeedyReader(15441): onPause()
10-07 21:11:40.514: D/SpeedyReader(15441): Saving 5 articles, 1 historical articles...
10-07 21:11:40.709: D/SpeedyReader(15441): Saved articles. success: true, length: 88218
10-07 21:11:40.764: D/SpeedyReader(15441): onStop()
10-07 21:11:40.764: D/SpeedyReader(15441): onDestroy()
10-07 21:11:40.772: D/SpeedyReader(15441): finish()
10-07 21:11:40.772: D/SpeedyReader(15441): **************  Restarting app to verify load.
10-07 21:11:40.772: D/SpeedyReader(15441): finish()
10-07 21:11:40.897: D/SpeedyReader(15441): onCreate()
10-07 21:11:40.944: D/SpeedyReader(15441): finalize()
10-07 21:11:40.944: D/SpeedyReader(15441): Settings.load() wpm:300
10-07 21:11:40.944: D/SpeedyReader(15441): Loading articles...

I don't know why it says onCreate in the log file. It's like onCreate() is being called asynchronously! Here is my onCreate().

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d(TAG, "onCreate()");
    ....
    extract = new Extractor();

I tried waitForIdleSync()

    Log.d(TAG, "**************  Restarting app to verify load.");
    mActivity = getActivity(); // restart the app, reload the history
    getInstrumentation().waitForIdleSync(); // ------------ NEW

But that didn't help.

2
  • This isn't a threading issue. The test framework does not kill activities within a testcase. So finish will do nothing Oct 8, 2012 at 1:47
  • Just to clarify for future readers, finish() does appear to call onStop(), onDestroy().
    – Chloe
    Oct 8, 2012 at 2:19

2 Answers 2

1

I think this is expected behavior as far as the framework is concerned.

I am assuming you are extending ActivityInstrumentationTestCase2 or ActivityUnitTestCase. ActivityUnitTestCase states that finish() does nothing. As for your async behavior, you usually run the test from the InstrumentationThread, and I believe the callbacks for lifecycle need to be called from the main thread. This is not being done in your example code.

As for the instance not having onCreate called, getActivity() will assert that onCreate() is called correctly but then the rest if up to you. Calling finish() on the activity within the same test invokation does nothing as far as the real lifecycle is concerned. It won't automatically deregister the activity. Only run code in the finish() method of the activity. Since getActivity() can be called multiple times and only will call onCreate() once on the same instance. It is not ever going to call onCreate() again from within the same test case.

7
  • I'm using ActivityInstrumentationTestCase2.
    – Chloe
    Oct 8, 2012 at 1:42
  • 1
    Cool. So I am not sure what you are expecting to be tested. The OS handling lifecycle invocation is probably not an issue. However if you want to make sure that your activity initializes itself when coming back from a kill or onCreate -> onDestroy(), you can devise a testcase that provides the savedInstanceState bundle and any stored values, prior to calling getActivity. You can't "kill and restart" an activity from within your test case. Oct 8, 2012 at 1:46
  • mActivity.finish() does do something, it calls onDestroy(), which is where I set everything to null to plug a memory leak. That is why I need onCreate() to be executed.
    – Chloe
    Oct 8, 2012 at 1:49
  • 1
    I apologize you are correct in that regard but it doesn't seem to remove the instance from the live Activity list of the ActivityManager. After you call finish, the only thing you can do is verify that the end state of the Activity is as you desire. Oct 8, 2012 at 1:50
  • 1
    Android will take care of calling onCreate() again in a real app but in a test invocation it will assert only once. After that you would have to call onCreate() manually. However, I think this approach is outside of what your expect in a test case. after onDestroy in the real app, that activity should be GC'd or eligible for gc. If you are tracking down a memory leak and are approaching it from the null out everything in sight methodology (not saying it's a good methodology), isn't the fact that all is null after calling onDestroy(), enough? Oct 8, 2012 at 2:05
0

Found it! Use

    getInstrumentation().callActivityOnCreate(mActivity, null);
1
  • This calls onCreate explicitly, but on the same Activity object. Use setActivity(null) to have the instrumentation framework re-create a new Activity object, and also call onCreate() automatically (making callActivityOnCreate() unnecessary).
    – Chloe
    Oct 22, 2012 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.