0

I have a newly created android-18 app, with a PreferenceScreen that's too obvious & simple to list.

And I also have an automated test [fixed to convert the questions into the answers].

public class MyPreferenceTest extends ActivityUnitTestCase<MyPreference> {  
    Intent intent;
    MyPreference activity;

    public MyPreferenceTest() {
        super(MyPreference.class);
    }

    @Override
    protected void setUp() throws Exception {
        Context targetContext = getInstrumentation().getTargetContext();
        intent = new Intent(targetContext, MyPreference.class);
        super.setUp();
    }

    private void assembleActivity() {
        startActivity(intent, null, null);
        activity = getActivity();
    }

    private void assemblePreferences(String device, String userName, String password) {
        Context targetContext = getInstrumentation().getTargetContext();        
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(targetContext);
        SharedPreferences.Editor editor = sharedPref.edit();

        editor.putString("smart_phin_bluetooth_devices", device);
        editor.putString("username", userName);  
        editor.putString("password", password);  
        editor.commit();
    }

    public void test_empty_userNames_dont_reflect_into_the_summary() {
        assemblePreferences("", "", ""); 
        assembleActivity();
        activity.onResume();
        EditTextPreference userName = (EditTextPreference) activity.findPreference("username");  // FIXME  userName
        assertEquals("", userName.getEditText().getText().toString());
        assertEquals("", userName.getText());
        assertEquals("Enter your user name", userName.getSummary().toString());
    }

    public void test_full_userNames_reflect_into_the_summary() {
        assemblePreferences("", "BookerT", "");
        assembleActivity();

        activity.onResume();  //  We must call this bc Android does but the test rig does not...
        EditTextPreference userName = (EditTextPreference) activity.findPreference("username");
        assertEquals("username", userName.getKey());

        assertEquals("BookerT", String.valueOf(userName.getText().toString());
//        assertEquals("BookerT", userName.getEditText().getText().toString());
        assertEquals("BookerT", userName.getSummary().toString());
    }

}  // end of class MyPreferenceTest

The commented-out assertion would fail because nothing has invoked that EditText view yet.

The test now checks that new code in onResume() updates the Summary from generic verbiage to reflect the Preference's current value.

Phlip
  • 5,253
  • 5
  • 32
  • 48
  • Which of the assertions "return "" strings, as if the preferences were empty"? You have four in `test_full_userNames_reflect_into_the_summary()`, two of which are commented out. So, for example, are you saying that `userName.getKey()` is returning an empty string? – CommonsWare Oct 07 '13 at 19:13
  • I was kind'a rooting for `userName.getEditText().getText()` to return `"BookerT"`. If (working theory) the EditText object is not yet populated because it's not displayed, the general question is how to write tests that extract details of behavior from the PreferenceScreen - preferably _without_ throwing Robotium at the problem... – Phlip Oct 07 '13 at 20:43

1 Answers1

1

You have a bug in your tests.

private void assemblePreferences(String device, String userName, String password)

Note that the order of values here is device, userName, password.

assemblePreferences("BookerT", "", "");

Hence device is BookerT, not userName.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ouch. Thanks! But... That fixed the first assertion (which we may now un-clutter to `assertEquals("BookerT", userName.getText().toString());`.) I can proceed from here with my actual feature, but the second commented assertion still cannot be freed from its comment marker. – Phlip Oct 07 '13 at 21:54
  • @Phlip: The `EditText` will not be populated until the dialog is opened. IMHO, that test is pointless, as you are testing the functionality of `EditTextPreference`, which you did not write. – CommonsWare Oct 07 '13 at 22:12
  • I'm doing this: http://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-su . So the test was a work-in-progress. We were discussing the "assembly" phase, not the actual assertion goal. – Phlip Oct 07 '13 at 23:50