2013-04-08 43 views
5

Tôi có một logic nghiệp vụ trong phân đoạn ít giao diện người dùng mà tôi phải thử nghiệm. Tôi đã thử 2 lựa chọn và cả hai đều thất bại.Thử nghiệm các mảnh vỡ Android

1. Sử dụng AndroidTestCase và tạo hoạt động giả.

Tiếp theo đang

@Override 
protected void setUp() { 
    Intent i = new Intent(getTestContext(), TestActivity.class); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    getTestContext().startActivity(i); 
} 

ném một ngoại lệ

Permission denied: checkComponentPermission() reqUid=10104 
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.xxx.iabsample.test/.TestActivity } from ProcessRecord{40769510 28116:com.xxx.iabsample/10070 (pid=28116, uid=10070) requires null 

2. Sử dụng ActivityInstrumentationTestCase2 với các hoạt động giả

public class IabTest extends ActivityInstrumentationTestCase2<TestActivity> { 
    public IabTest() { 
     super("com.xxx.iabsample.test", TestActivity.class); 
    } 
} 

ném một ngoại lệ

java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.xxx.iabsample/.test.TestActivity } 

Dường như nó cố gắng để bắt đầu hoạt động từ ứng dụng thử nghiệm mục tiêu, không phải từ ứng dụng thử nghiệm.

Vì vậy, cách chính xác để kiểm tra phân đoạn là gì?

+0

thấy điều này http://stackoverflow.com/questions/4162447/android-java-lang-securityexception-permission-denial-start-intent – Elior

Trả lời

-2
//Please try to use bellow code. 


// activity layout main.xml 
<LinearLayout 
    android:id="@+id/frags" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <ListView 
     android:id="@+id/number_list" 
     android:layout_width="250dip" 
     android:layout_height="match_parent" /> 

    <fragment 
     android:id="@+id/the_frag" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.example.fragmenttest.TestFragment" /> 

</LinearLayout> 

//Fragments java class. 



public class TestFragment extends Fragment 

{ 

    private int nAndroids; 

    public TestFragment() { 

    } 

    /** 
    * Constructor for being created explicitly 
    */ 
    public TestFragment(int nAndroids) { 
     this.nAndroids = nAndroids; 
    } 

    /** 
    * If we are being created with saved state, restore our state 
    */ 
    @Override 
    public void onCreate(Bundle saved) { 
     super.onCreate(saved); 
     if (null != saved) { 
      nAndroids = saved.getInt("nAndroids"); 
     } 
    } 

    /** 
    * Save the number of Androids to be displayed 
    */ 
    @Override 
    public void onSaveInstanceState(Bundle toSave) { 
     toSave.putInt("nAndroids", nAndroids); 
    } 

    /** 
    * Make a grid and fill it with n Androids 
    */ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) { 
     int n; 
     Context c = getActivity().getApplicationContext(); 
     LinearLayout l = new LinearLayout(c); 
     for (n = 0; n < nAndroids; n++) { 
      ImageView i = new ImageView(c); 
      i.setImageResource(R.drawable.android); 
      l.addView(i); 
      TranslateAnimation a = new TranslateAnimation(-50, 30, 0, 0); 
      a.setDuration(1000); 

      l.startAnimation(a); 
      a.setFillAfter(true); 
     } 
     return l; 
    } 
} 

// Activity 


public class FragmentTestActivity extends FragmentActivity implements OnItemClickListener { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ListView l = (ListView) findViewById(R.id.number_list); 
     ArrayAdapter numbers = new ArrayAdapter<String>(getApplicationContext(), 
       android.R.layout.simple_list_item_1, 
       new String [] { 
      "one", "two", "three", "four", "five", "six" 
     }); 
     l.setAdapter(numbers); 
     l.setOnItemClickListener(this); 
    } 


    /** 
    * Add a Fragment to our stack with n Androids in it 
    */ 
    private void stackAFragment(int nAndroids) { 
     Fragment f = new TestFragment(nAndroids); 

     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.replace(R.id.the_frag, f); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
     ft.addToBackStack(null); 
     ft.commit(); 
    } 

    /** 
    * Called when a number gets clicked 
    */ 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     stackAFragment(position + 1); 
    } 
} 
+0

Tôi không nghĩ rằng câu trả lời này có bất cứ điều gì để làm với thử nghiệm android khuôn khổ. Tâm trí đăng một số lời giải thích cùng với mã? – ernazm

Các vấn đề liên quan