2014-06-11 103 views
2

我想創建一個簡單的測試,檢查列表視圖的數據,直到找到相應的條目。這裏是ListActivity:安卓濃咖啡庫導入

package com.example.unittest.app; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 


public class ListActivity extends ActionBarActivity { 

    ListView list; 
    List<Map<String, String>> characterList; 
    SimpleAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 

     list = (ListView)findViewById(R.id.listView); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       Toast.makeText(getBaseContext(), "Item " + i , Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     characterList = new ArrayList<Map<String, String>>(); 
     initList(); 
     adapter = new SimpleAdapter(this, characterList, android.R.layout.simple_list_item_1, new String[] {"character"}, new int[] {android.R.id.text1}); 
     list.setAdapter(adapter); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.list, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    private void initList() { 
     characterList.add(createItem("character", "Don")); 
     characterList.add(createItem("character", "Sally")); 
     characterList.add(createItem("character", "Betty")); 
     characterList.add(createItem("character", "Joan")); 
     characterList.add(createItem("character", "Freddy")); 
     characterList.add(createItem("character", "Bert")); 
     characterList.add(createItem("character", "Roger")); 
     characterList.add(createItem("character", "Peggy")); 
     characterList.add(createItem("character", "Dawn")); 
     characterList.add(createItem("character", "Megan")); 
     characterList.add(createItem("character", "Bob")); 
     characterList.add(createItem("character", "Pete")); 
    } 

    private HashMap<String, String> createItem(String key, String name) { 
     HashMap<String, String> month = new HashMap<String, String>(); 
     month.put(key, name); 
     return month; 
    } 

} 

爲了找到正確的行,我試圖用昂達通過地圖的數組迭代,以找到正確的條目。這裏是我的測試類:

package com.example.unittest.test; 

import android.test.ActivityInstrumentationTestCase2; 

import com.example.unittest.app.MainActivity; 
import com.example.unittest.app.R; 

import org.hamcrest.Matchers; 

import java.util.Map; 

import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView; 
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData; 
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click; 
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches; 
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withContentDescription; 
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId; 
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText; 
import static org.hamcrest.Matchers.allOf; 
import static org.hamcrest.Matchers.anything; 
import static org.hamcrest.Matchers.equalTo; 
import static org.hamcrest.Matchers.hasEntry; 
import static org.hamcrest.Matchers.instanceOf; 
import static org.hamcrest.Matchers.is; 


/** 
* Created by user on 2014-04-21. 
*/ 
public class EspressoInterfaceTest extends ActivityInstrumentationTestCase2<MainActivity> { 

    public EspressoInterfaceTest() { 
     super(MainActivity.class); 
    } 

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

    public void testButton1() { 
     onView(withId(R.id.button)).perform(click()); 
     onView(withId(R.id.textview))        // ViewMatcher 
       .check(matches(withText("Pressed!")));    // ViewAssertion 
    } 

    public void testButton2() { 
     onView(withId(R.id.button2)) 
       .perform(click()); 
     onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("character"), is("Don"))) 
       .perform(click()); 

    } 
} 

不幸的是,我想我輸入了錯誤的匹配器,因爲我的昂達的.perform方法不能得到解決,並顯示爲紅色。有沒有辦法來解決這個問題?我的onData電話會工作嗎?我使用https://code.google.com/p/android-test-kit/wiki/EspressoSamples作爲參考,但我似乎無法讓它工作。謝謝。

回答

2

你錯過了一個括號前執行操作:

onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("character"), is("Don")))) 
       .perform(click());