2017-03-06 31 views
1

マイアクティビティ 'MainActivity'は、いくつかの条件のもとで、自分自身の別のアクティビティオンタップを開始する必要があります。アクティビティが別のアクティビティを開始したため、アクティビティのタイムアウトを開始する

私はMainActivityのテストを実行して、それらの条件でAnotherActivityが開始されていることを確信します。

MainActivityがScreenに表示されるのを待っているように見えるので、私のEspressoテストでMainActivityが起動しません。私は他の活動を始めているので、これは起こりません。アクティビティは、ActivityTestRuleを介して起動されます。

私のエスプレッソ・テストを実行しているとき、私は次の例外を得た:

java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.djoglobal.corebelt.debug/com.djo.compex.corebelt.ui.MainActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1488804986217 and now the last time the queue went idle was: 1488804986217. If these numbers are the same your activity might be hogging the event queue. 
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:360) 
at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:219) 
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:268) 
at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at tools.fastlane.screengrab.locale.LocaleTestRule$1.evaluate(LocaleTestRule.java:32) 
at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932) 

私はonResumeで「AnotherActivity」を開始した場合、私は同じ動作をします。

私はこのシナリオをテストするためにエスプレッソ2.2.1

任意のアイデアを使用していますか?

+0

この問題の解決策をお探しですか? – makovkastar

答えて

0

あなたはHandlerに活動を開始投稿することができます:私は表示されているからそれを防ぐために始めた活動の意図をスタブにしEspresso Intentsを利用している

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

    ... 

    if (condition) { 
     final int millisUntilLaunch = 500; 

     final Handler handler = new Handler(); 

     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       startActivity(new Intent(this, AnotherActivity.class)); 
      } 
     }, millisUntilLaunch); 
    } 
} 
+0

これはありがとうございます。しかし私は好むだろう:1)テスト機能を働かせるために、アプリケーションの機能性/生産コードを変更しないこと。 2)ユーザーが間にMainActivityの小さな出現を見ることを許可しない – Kain

0

。 LoginActivityが開始されていることを検証し

Account account = AccountUtils.getAccount(this); 
if (account == null) { 
    startActivity(this, LoginActivity.class); 
    finish(); 
    return; 
} 

エスプレッソテスト:

@Test 
public void startsLoginActivityImmediately() { 
    Intents.init(); 

    // Stub the login intent to prevent LoginActivity from being displayed. 
    // This helps to fix the Espresso timeout exception. 
    Matcher<Intent> loginIntent = hasComponent(LoginActivity.class.getName()); 
    intending(loginIntent).respondWith(new Instrumentation.ActivityResult(0, null)); 

    mActivityRule.launchActivity(null); 
    intended(hasComponent(LoginActivity.class.getName())); 

    Intents.release(); 
} 

Iユーザーがログインしていない場合私の場合LoginActivityに

はMainActivityののonCreate()で開始されましたこのアプローチがあなたの問題を解決することを願っています。

0

は、私はあなたがどこかにここで問題があると思う:私はnullを渡すと mActivityTestRule.launchActivity(null); が、私はあなたと同じエラーを持っています。

(コメントアウト部分を参照)これは私のコード例であり、それはすべてのシナリオで動作します:

@RunWith(AndroidJUnit4.class) 
public class MyTest { 

    @Rule 
    public ActivityTestRule<TestActivity1> mActivityTestRule = new ActivityTestRule<>(TestActivity1.class); 

    @Before 
    public void setup() { 
     Intents.init(); 
    } 

    @After 
    public void tearDown() { 
     Intents.release(); 
    } 

    @Test 
    public void test() { 
     Intent intent = new Intent(); 
     mActivityTestRule.launchActivity(intent); 
     intended(hasComponent(TestActivity2.class.getName())); 
    } 
} 

....

public class TestActivity1 extends Activity{ 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     setTitle("first"); 

     if (isConditionMatch()) { 
      startActivity(new Intent(this, TestActivity2.class)); 
     } 
    } 
} 

また、あなたは置き換えることによって、ここに実装するユニットテストを試すことができますメソッド呼び出しでstartActivity(new Intent(this, TestActivity2.class));を呼び出し、これをプレゼンターなどに配置します。

関連する問題