簡體   English   中英

如何檢查一個活動是否收到另一個活動發送的意圖?

[英]How to check if an Activity receives an intent sent from another activity?

單擊按鈕后,我將意圖從活動 1 發送到活動 2。 我帶着這個意圖發送額外的內容。

使用espresso,我需要測試activity2是否收到了從activity1發送的這個意圖。

我不知道,該怎么做。 我沒有寫任何代碼。

假設從 ActivityA 中單擊一個啟動 ActivityB 的按鈕:

Intent intent = new Intent(this, ActivityB.class);
                intent.putExtra("MY_EXTRA", "MY EXTRA VALUE");

檢查您是否向 ActivityB 發送正確數據的測試將是:

@Rule
public IntentsTestRule<ActivityA> intentsTestRule = new IntentsTestRule<>(ActivityA.class);

@Test
public void testIntents() {
    //from ActivityA, click the button which starts the ActivityB
    onView(withText("ClickMe")).perform(click());

    //validate intent and check its data
    intended(allOf(
            toPackage("com.your.package.name"),
            hasExtra("MY_EXTRA", "MY EXTRA VALUE")
    ));
}

查看谷歌提供的示例: https : //github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample

以及這里的一些文檔: https : //developer.android.com/training/testing/espresso/intents.html

首先使用getIntent()

Intent intent = getIntent();

然后使用意圖對象從另一個活動接收數據

String id = intent.getStringExtra("your key");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM