2014-05-07 93 views
0

需要一種解決方法來停止在ADT上運行在AVD上的長時間單元測試。在JUnit視圖中的停止按鈕不起作用,在調試視圖中都沒有停止按鈕。即使它打印該VM已終止。例如:停止按鈕不停止當前android單元測試

public class StopTest extends AndroidTestCase { 

    public void testSleep() throws Exception { 
     Thread.sleep(20000); 
    } 
} 

可能的解決方案:

顯示設備屏幕上的取消按鈕。

我設法用我的測試項目中的按鈕創建一個新的活動。不幸的是,測試套件是在我測試的應用程序的上下文中執行的。所以,要開始這個活動,我需要編輯我不想要的應用程序清單。所以我將這個活動添加到測試清單並從InstrumentationTestCase.getInstrumentation().getContext()開始。

用這個按鈕我的活動出現在屏幕上,但它在另一個進程中啓動。現在我需要從活動向我測試的應用程序發送消息。

不想將服務添加到我的應用程序,因爲,正如我所說的,我不想編輯應用程序清單。

是否可以發送Binder到活動並使用它發回消息?

回答

0

setUp()我用一個按鈕開始活動。按下後,我會回覆廣播,並在接收器中撥打System.exit()

==> ./src/org/foo/StopButtonInstrumentationTestCase.java <== 
... 
public class StopButtonInstrumentationTestCase extends InstrumentationTestCase { 

     public void testSleep() throws Exception { 
       Thread.sleep(10000); 
     } 

     public void testSleep2() throws Exception { 
       Thread.sleep(10000); 
     } 

     @Override 
     protected void setUp() throws Exception { 
       super.setUp(); 
       showStopButton(getInstrumentation(), getName()); 
     } 

     @Override 
     protected void tearDown() throws Exception { 
       hideStopButton(getInstrumentation()); 
       super.tearDown(); 
     } 

     private static final BroadcastReceiver receiver = new BroadcastReceiver() { 
       @Override 
       public void onReceive(Context context, Intent intent) { 
         System.exit(1); 
       } 
     }; 

     public static void showStopButton(Instrumentation instrumentation, String testName) { 
       // register stop tests receiver 
       Context targetCtx = instrumentation.getTargetContext(); 
       targetCtx.registerReceiver(receiver, new IntentFilter(StopperActivity.ACTION)); 

       // show stop tests button 
       Context ctx = instrumentation.getContext(); 
       Intent intent = new Intent(ctx, StopperActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
       intent.putExtra(StopperActivity.PARAM_TEST_NAME, testName); 
       ctx.startActivity(intent); 
     } 

     public static void hideStopButton(Instrumentation instrumentation) { 
       Context targetCtx = instrumentation.getTargetContext(); 
       targetCtx.unregisterReceiver(receiver); 
       // hide stop tests button 
       Context ctx = instrumentation.getContext(); 
       Intent intent = new Intent(ctx, StopperActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); // android:launchMode="singleTop" doesn't work in old versions 
       intent.putExtra(StopperActivity.PARAM_KILL, true); 
       ctx.startActivity(intent); 
     } 
} 

==> ./src/org/foo/StopperActivity.java <== 
... 
public class StopperActivity extends Activity { 

     public static final String ACTION = StopperActivity.class.getName() + ".ACTION"; 
     public static final String PARAM_TEST_NAME = "testName"; 
     public static final String PARAM_KILL = "kill"; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

       setContentView(R.layout.activity_stopper); 

       findViewById(R.id.stop_button).setOnClickListener(
           new View.OnClickListener() { 
             @Override 
             public void onClick(View view) { 
               stopTest(); 
             } 
           }); 
       Intent intent = getIntent(); 
       Bundle extras = intent.getExtras(); 
       if (extras != null) { 
         displayTestName(extras); 
       } 
     } 

     @Override 
     protected void onNewIntent(Intent intent) { 
       super.onNewIntent(intent); 
       Bundle extras = intent.getExtras(); 
       if (extras != null) { 
         if (extras.containsKey(PARAM_KILL)) { 
           finish(); 
         } else { 
           displayTestName(extras); 
         } 
       } 
     } 

     private void stopTest() { 
       sendBroadcast(new Intent(ACTION)); 
       finish(); 
     } 

     private void displayTestName(Bundle extras) { 
       String testName = extras.getString(PARAM_TEST_NAME); 
       if (testName == null) 
         testName = ""; 
       setTitle(testName); 
     } 
} 

==> ./res/layout/activity_stopper.xml <== 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="org.foo.StopperActivity" > 

    <Button 
     android:id="@+id/stop_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 

     android:layout_marginTop="16dp" 
     android:paddingLeft="32dp" 
     android:paddingRight="32dp" 

     tools:ignore="HardcodedText" 
     android:text="Stop test" /> 

</merge> 

==> AndroidManifest.xml <== 
... 
<!-- android:exported="true" - allow launch test activity in context of testee --> 
<activity 
    android:name="org.foo.StopperActivity" 
    android:exported="true" 
    android:windowSoftInputMode="adjustResize|stateVisible"> 
</activity> 
...