2016-10-24 69 views
0

我目前正在尝试使用Espresso在Android上测试ListView,并使用自定义ArrayAdapter。 下面是测试:Android Espresso - 测试ListView行

final AssociationsListActivity listActivity = getActivity(); 

    final Association association1 = new Association(ASSOCIATION_NAMES[0], ASSOCIATION_DESCRIPTIONS[0]); 
    final Association association2 = new Association(ASSOCIATION_NAMES[1], ASSOCIATION_DESCRIPTIONS[1]); 
    final Association association3 = new Association(ASSOCIATION_NAMES[2], ASSOCIATION_DESCRIPTIONS[2]); 

    listActivity.addAssociation(association1); 
    listActivity.addAssociation(association2); 
    listActivity.addAssociation(association3); 

    assertTrue(listActivity.getAssociationsList().size() == 3); 

    onView(withId(R.id.associationsListView)).check(matches(isDisplayed())); 
    onData(anything()) 
      .inAdapterView(withId(R.id.associationsListView)) 
      .atPosition(0) 
      .perform(click()); 

    assertCurrentActivityIsInstanceOf(AssociationsListActivityTest.this 
      , AssociationsPageActivity.class); 

的方法,简单地listActivity.addAssociation(association);确实associationArrayAdapter.add(association);

ArrayAdapter用于元素与ID添加到ListView(默认可见)R.id.associationsListView

当我运行这个测试,我得到以下错误:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view. 
Expected: is displayed on the screen to the user 
Got: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}" 

这是造成下面的断言,onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));

此外,当评论这个断言,下一个例外导致以下异常:

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: ch.epfl.sweng.project:id/associationsListView'. 
... 
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: 
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user) 
Target view: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}" 

我经历了很多覆盖这些异常的堆栈溢出线程,我用其他等价物替换了那些测试,但是这些错误仍然显示出来,我不知道该怎么做!谢谢你的帮助!

回答

0

我发现有什么问题!

我向ArrayAdapter添加元素的方法使用了runOnUIThread,但似乎测试并未等待此方法,因此associationsListView被认为不会显示给用户!所以我固定我的方法listActivity.addAssociation(Association association)这样的:

public void addAssociation(final Association association) { 
    final CountDownLatch latch = new CountDownLatch(1); 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      if (!associationList.contains(association)) { 
       associationArrayAdapter.add(association); 
      } 
      latch.countDown(); 
     } 
    }); 
    try { 
     latch.await(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    if (!associationList.contains(association)) { 
     associationList.add(association); 
    } 
} 

测试的所有部分现在都通过了:)

相关问题