测试应该出现在ActionBar中的片段菜单项

时间:2014-06-06 01:22:33

标签: android unit-testing junit robotium

在我的应用中,点击某个复选框应启用操作栏上的菜单项。当我手动运行应用程序时,这可以正常工作。但是,在检查菜单项的视图时,自动JUnit / Robotium测试失败。

为什么我在测试套件中看到不同的行为?

这是测试代码:

public class FilterCardsTest extends ActivityInstrumentationTestCase2<FragmentTestActivity> {

    public FilterCardsTest() {
        super(FragmentTestActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        this.activity = this.getActivity();
        this.activity.replaceFragment(new FilterCards());
        this.getInstrumentation().waitForIdleSync();
        this.solo = new Solo(this.getInstrumentation(), this.activity);
    }

    public void testBrandCheckBox() {
        this.testCheckBox(R.id.brand_check, R.id.brand_input);
    }

    private void testCheckBox(int checkId, int inputId) {
        CheckBox cb = (CheckBox) this.activity.findViewById(checkId);
        EditText input = (EditText) this.activity.findViewById(inputId);
        this.solo.clickOnView(cb);
        this.getInstrumentation().waitForIdleSync();
        Assert.assertTrue(input.isEnabled());
        Assert.assertTrue(input.hasFocus());

        Assert.assertTrue(this.solo.waitForView(R.id.save_menu)); // FilterCardsTest.java:106
    }
}

为了测试片段,我创建了一个存根FragmentTestActivity

public class FragmentTestActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity);
    }

    public void replaceFragment(Fragment fragment) {
        this.getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
    }

}

设置复选框onClickListener()的代码以及启用菜单项的代码:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    MenuItem confirm = menu.findItem(R.id.save_menu);

    if (this.numberChecked() > 0) {
        confirm.setVisible(true);
        confirm.setEnabled(true);
    } else {
        confirm.setVisible(false);
        confirm.setEnabled(false);
    }
}

private View.OnClickListener onCheckBoxClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText input = null;

        for (int i = 0; i < CHECKBOXES.length; i++) {
            if (v.getId() == CHECKBOXES[i]) {
                input = (EditText) FilterCards.this.getActivity().findViewById(TEXT_FIELDS[i]);
            }
        }

        FilterCards.this.toggleTextField(input);
        FilterCards.this.getActivity().supportInvalidateOptionsMenu();
    }
};

和logcat:

junit.framework.AssertionFailedError
at bbct.android.common.activity.test.FilterCardsTest.testCheckBox(FilterCardsTest.java:106)
at bbct.android.common.activity.test.FilterCardsTest.testBrandCheckBox(FilterCardsTest.java:114)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)

1 个答案:

答案 0 :(得分:1)

由于FragmentTestActivity仅展开FragmentActivity,因此不会显示操作栏项。它应该扩展ActionBarActivity