3

I'll apologize in advance for posting alot of code, this issue has really got me!

I have two Android JUnit tests that are causing me problems. Run each individually and they work fine, but when run together in one go (PasswordEntryActivityTests and then CryptoKeystoreTests) CryptoKeystoreTests hangs indefinitely.

I know it's not just the emulator being slow because each individually finishes in less than a second but it can hang for more than 20 minutes. I also tested it on a real device (Droid Razr) and it does the same thing.

The problematic code is the PasswordEntryActivity.launchNewPasswordActivity(). Removing that function makes everything work fine.

Pausing the function in the debugger while it's hanging says it's in:

MessageQueue.nativePollOnce(int, int) line: not available [native method]

What's going on?

I've copied below:

  • PasswordEntryActivity
  • PasswordEntryActivityTests
  • CryptoManagerKeystoreTests

Please let me know to post any other code you'd like to see. Thanks!

    public class PasswordEntryActivity extends Activity 
    {
...
        private void launchNewPasswordActivity()
        {
            Intent launchNewPasswordIntent = new Intent(this, NewPasswordActivity.class);
            startActivity(launchNewPasswordIntent);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.password_entry_layout);
...
            //this code should be LAST in onCreate because it exits the Activity
            //CryptoManager.passwordIsRight returns 0 if no password has been set
            passwordExists = CryptoManager.passwordIsRight("x", this) != 0;
            if(!passwordExists)
                launchNewPasswordActivity();
        }
    }

That Activity's test:

    //supposed to make sure the application responds correctly when no password is set
    public class PasswordEntryActivityTests extends android.test.ActivityInstrumentationTestCase2<                          crypnote.controller.main.PasswordEntryActivity>{
    protected void setUp() throws Exception 
    {
        passwordEntryActivity = getActivity();

        //delete the database if it exists
        File file = passwordEntryActivity.getFileStreamPath(DBInterface.Constants.DatabaseName);
        if(file.exists())
            assertTrue(file.delete());
        file = passwordEntryActivity.getFileStreamPath(CryptoManager.Constants.KEYSTORE_PATH);
        if(file.exists())
            assertTrue(file.delete());
    }
    //allows us to access the interface
    @UiThreadTest
    public void testNoPassword() throws Exception
    {
        passwordEntryActivity  = getActivity();

        EditText passwordEntryEditText = 
                (EditText) passwordEntryActivity.findViewById(
                crypnote.controller.main.R.id.passwordentrylayout_passwordedittext);
        Button unlockButton = (Button) passwordEntryActivity.findViewById(
                                        crypnote.controller.main.R.id.passwordentrylayout_unlockbutton);


        int passwordResult = CryptoManager.passwordIsRight("x", getActivity());
        assertTrue(passwordResult == 0);

        //pass a wrong password to the edittext and click the unlock button
        passwordEntryEditText.setText("x");
        assertTrue(unlockButton.performClick());

        //get the foreground activity class name
        ActivityManager am = (ActivityManager) passwordEntryActivity.
                                                        getSystemService(Context.ACTIVITY_SERVICE);

        // get the info from the currently running task
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); 

        ComponentName componentInfo = taskInfo.get(0).topActivity;
        String foregroundClassName = componentInfo.getShortClassName();

        //don't forget the leading '.'
        assertTrue(!foregroundClassName.equals(".PasswordEntryActivity"));
    }
}

The CryptoKeystoreTests:

public class CryptoKeystoreTests extends android.test.ActivityInstrumentationTestCase2<
                                                                crypnote.controller.main.PasswordEntryActivity>
{
    public void testKeystore() throws Exception
    {
        Context context = getActivity();

        //delete the database if it exists
        File file = context.getFileStreamPath(DBInterface.Constants.DatabaseName);
        if(file.exists())
            assertTrue(file.delete());
        file = context.getFileStreamPath(CryptoManager.Constants.KEYSTORE_PATH);
        if(file.exists())
            assertTrue(file.delete());

        CryptoManager cryptoManager=null;
        String password = CryptoManager.Constants.DEBUG_PASSWORD;
        FileInputStream fis=null;

            //the cryptomanager will generate a new key and keystore
            cryptoManager = new CryptoManager(password, context);
            Key CRYPTOKEY = cryptoManager.getKey();
            cryptoManager.close();

            //initialize KeyStore
            KeyStore keystore = KeyStore.getInstance(Constants.KEYSTORE_INSTANCE_TYPE);
            fis = context.openFileInput(CryptoManager.Constants.KEYSTORE_PATH);
            keystore.load(fis, password.toCharArray());

            assertTrue(keystore.containsAlias(Constants.APP_ALIAS));    
            assertTrue(keystore.isKeyEntry(Constants.APP_ALIAS));

            Key key = keystore.getKey(CryptoManager.Constants.APP_ALIAS, 
                                        password.toCharArray());
            assertTrue(key.getAlgorithm().equals(CryptoManager.Constants.PROVIDER_NAME));

            assertTrue(key.getAlgorithm().equals(CRYPTOKEY.getAlgorithm()));
            assertTrue(key.getFormat().equals(CRYPTOKEY.getFormat()));

            if(fis != null)
                fis.close();
    }
}

EDIT: NewPasswordActivity.onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_password_layout);
}
Prime
  • 4,081
  • 9
  • 47
  • 64
  • A lot of code is painful, and can attract a lot of downvotes. Since you have not taken the effort to narrow down the problem at your end. Even worse when you put so many empty lines between lines of code. I wonder what purpose empty lines have on code. Sorry, downvote. Please redo your code and reduce it to the part that is causing the problem. – Siddharth Jan 31 '13 at 03:25
  • I'll clean up the empty lines as best I can, but they were there for improving readbility. I didn't want to truncate too much code because I wasn't certain exactly what was causing the issue. – Prime Jan 31 '13 at 04:28
  • is, launchNewPasswordActivity being called ? can you put a log.d before the launchNewPasswordActivity and obv put both statements in a {} – Siddharth Jan 31 '13 at 04:37
  • put the oncreate code of launchNewPasswordActivity – Siddharth Jan 31 '13 at 04:38
  • I think you mean the onCreate of NewPasswordActivity. It's in the above. – Prime Jan 31 '13 at 18:02
  • and launchNewPasswordActivity is being called for sure. – Prime Jan 31 '13 at 18:03

1 Answers1

2

It hangs because PasswordEntryActivityTests does not release/finish the resources/UI events that has been addressed/created by itself during its own running lifecycle, more specifically, then newly opened NewPasswordActivity.

PasswordEntryActivityTests starts by testing a creation of PasswordEntryActivity, i.e. getActivity(), which in consequence, based on the condition, launch a second NewPasswordActivity, the newly opened NewPasswordActivity occupy the foreground window and stay forever, it is developer's responsibility to release it properly after you have done your testing.

In instrumentation test, the correct way of detecting/monitoring second activity startup from current activity is to use ActivityMonitor, see the pseudo code below:

// No password result starting a second activity.
public void testNoPassword() {
  // register NewPasswordActivity that need to be monitored.
  ActivityMonitor activityMonitor = getInstrumentation().addMonitor(NewPasswordActivity.class.getName(), null, false);

  // Get current activity, it will start NewPasswordActivity in consequence.
  PasswordEntryActivity currentActivity = getActivity();

  NewPasswordActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5);
  // NewPasswordActivity is opened and captured.
  assertNotNull(nextActivity);
  // Don't forget to release/finish NewPasswordActivity after test finish.
  nextActivity.finish();
}
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • I keep getting a null value for nextActivity no matter where I call getInstrumentation().waitForMonitorWithTimeout. What's the correct way to do this? – Prime Feb 04 '13 at 00:41
  • `getInstrumentation().waitForMonitorWithTimeout();` is probably been called too soon, try adding `Thread.sleep(3000);` right after `PasswordEntryActivity currentActivity = getActivity();` and before calling `getInstrumentation().waitForMonitorWithTimeout();` If the condition `if(!passwordExists)` is met, you should able to see the motion i.e. NewPasswordActivity is opened in the emulator. – yorkw Feb 04 '13 at 01:40
  • still no good- does it have anything to do with running the test on the UI thread (via @UiThreadTest). Also, if I replace getInstrumentation().addMonitor(NewPasswordActivity.class.getName(), null, false); with getInstrumentation().addMonitor(PasswordEntryActivity.class.getName(), null, false); I still get null. That doesn't seem right. – Prime Feb 04 '13 at 05:07
  • @Prime, yes, @UiThreadTest make the whole test method running on UI thread, whereas `waitForMonitorWithTimeout()` **must be called off the UI thread**, otherwise it will return null all the time. Check out [this related discussion](http://stackoverflow.com/questions/13105202/starting-another-activity-in-a-junit-test-by-simulating-a-button-press) for more details. – yorkw Feb 04 '13 at 06:14
  • still no good- I'm going with Robotium. It's more trouble than it's worth. – Prime Feb 05 '13 at 18:04