5

मैं इस समस्या से थोड़ा संघर्ष कर रहा हूं, और मुझे लगता है कि मुझे Robolectric के बारे में कुछ मौलिक नहीं मिल रहा है। आम तौर पर कुछ Google खोज मुझे इस प्रकार की समस्या के निचले भाग तक पहुंचने में मदद कर सकती हैं, लेकिन उसके बीच और नमूना कोड को देखकर मुझे कुछ भी उपयोग नहीं मिल रहा है।Robolectric, सूची आइटम पर क्लिक करने में समस्याएं

मैं एक सूची दृश्य आइटम पर एक क्लिक अनुकरण करने की कोशिश कर रहा हूं और जांचता हूं कि क्लिक के बाद एक गतिविधि लॉन्च की जाती है। मैं वापस आ रहा हूं कि वर्तमान गतिविधि जो मैं परीक्षण कर रहा हूं वह परिणामस्वरूप गतिविधि है। मैंने सभी सूची आइटम क्लिक करने वाले कोड को हटाने और परिणामी गतिविधि की जांच करने का प्रयास किया, और यह इंस्टॉलेशनलिस्ट एक्टिविटी के रूप में वापस आया जो मैं परीक्षण कर रहा हूं। तो मैं इस निष्कर्ष पर पहुंचा कि सूची दृश्य आइटम पर क्लिक नहीं किया जा रहा है, और मुझे यकीन नहीं है कि क्यों। सिस्टम लॉग जो मैंने नीचे दिए गए परीक्षण कोड में स्थापित किए हैं वे वे मान हैं जिन्हें मैं उम्मीद करता हूं। सूची 13 आइटम है, और getChildAt (0) शीर्षलेख देता है। मुझे लगता है कि पहली वस्तु (getChildAt (1)) प्राप्त करना और कॉलिंग पर क्लिक करना या उसके बच्चे के टेक्स्ट व्यू पर मेरी अपेक्षित गतिविधि लॉन्च होगी, लेकिन ऐसा लगता है कि ऐसा नहीं लगता है।

@Before 
    public void setUp() { 
     mAppLaunch = new ApplicationLaunchActivity(); 
     mAppLaunch.onCreate(null); 
     mActivity = new InstallationListActivity(); 
     mActivity.onCreate(null); 
    } 

    @Test 
    public void shouldHaveNonEmptyInstallationList() throws Exception { 
     assert(mActivity.installationListCount() > 0); 
    } 

    @Test 
    public void shouldHaveSameNumberOfElements() throws Exception { 
     ListView installationListView = (ListView) mActivity.getListView(); 

     ShadowListView shadowListView = shadowOf(installationListView); 

     assert(shadowListView.getChildCount() == mActivity.installationListCount()); 
    } 

    @Test 
    public void pressingTheFirstItemInTheListShouldLaunchVenueListActivity() { 
     ListView installationListView = (ListView) mActivity.findViewById(android.R.id.list); 

     System.out.println("qty: " + installationListView.getChildCount()); 
     System.out.println("class: " + installationListView.getChildAt(0).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(1).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(2).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(3).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(4).getClass()); 

     LinearLayout firstItemLayout = (LinearLayout) installationListView.getChildAt(1); 
     TextView firstItem = (TextView) firstItemLayout.getChildAt(0); 

     ShadowTextView shadowFirstItem = shadowOf(firstItem); 
     ShadowLinearLayout shadowLayout = (ShadowLinearLayout) shadowOf(firstItemLayout); 

     shadowLayout.performClick(); 
     shadowFirstItem.performClick(); 

     clickOn(firstItem); 
     clickOn(firstItemLayout); 

     System.out.println("class: " + firstItemLayout.getChildAt(0).getClass()); 
     System.out.println("Layout shadow" + shadowOf(firstItemLayout).getClass()); 
     System.out.println("First Item Text: " + shadowFirstItem.getText()); 

     ShadowActivity shadowActivity = shadowOf(mActivity); 
     Intent startedIntent = shadowActivity.getNextStartedActivity(); 
     assertNotNull(startedIntent); 
     ShadowIntent shadowIntent = shadowOf(startedIntent); 

     assertThat(shadowIntent.getComponent().getClassName(), equalTo(VenueListActivity.class.getName())); 
    } 
} 

यहाँ लेआउट के मैं सूची दृश्य का निर्माण करने का उपयोग कर रहा है:

This is list.xml 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
    <ListView android:id="@android:id/list" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:cacheColorHint="#00000000" 
       android:background="@drawable/background"/> 
</LinearLayout> 

list_item.xml 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/list_item" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent"> 
    <TextView android:id="@+id/list_item_text" 
       style="@style/tw_list_font" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:gravity="center_vertical" 
       android:paddingLeft="6dip" 
       android:minHeight="?android:attr/listPreferredItemHeight" /> 
</LinearLayout> 

और यहाँ है कोड है कि सूची initializes वैसे भी, यहाँ मैं उपयोग कर रहा हूँ robolectric/परीक्षण कोड है:

InstallationListActivity.java 

setContentView(R.layout.list); 
     final ListView installationListView = getListView(); 
     LayoutInflater inflater = getLayoutInflater(); 
     ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, installationListView, false); 
     TextView headerText = (TextView) header.findViewById(R.id.header_text); 
     headerText.setText("Installations On Live"); // TODO: This should not be hardcoded 
     installationListView.addHeaderView(header, null, false); 

     setListAdapter(new ArrayAdapter<Installation>(this, R.layout.list_item, R.id.list_item_text, mInstallations)); 

     // Click event 
     installationListView.setClickable(true); 
     installationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg1) { 
       Installation installationClicked = (Installation) installationListView.getItemAtPosition(position); 
       if (LOCAL_LOG) { 
        Log.d(LOG_TAG, "Installation: " + installationClicked.toString() + " clicked."); 
        Log.d(LOG_TAG, installationClicked.toString() + " has installationDirectory: " + 
          installationClicked.rootDir); 
       } 
       AppState.installation = installationClicked; 
       AppState.serverInstallationName = installationClicked.rootDir; 
       Intent venueListIntent = new Intent(InstallationListActivity.this, VenueListActivity.class); 
       startActivity(venueListIntent); 
      } 
     }); 

किसी भी मदद की बहुत सराहना की जाती है! अनेक अनेक धन्यवाद!

उत्तर

12

प्रयास करें सहायक विधि performItemClick:

Robolectric.shadowOf(listView).performItemClick(position); 
संबंधित मुद्दे