如何通过 Espresso 检查拨号器上的 phone 号码 - 通过 autoLink 触发的拨号器

How to check phone number on dialer through Espresso - Dialer fired through autoLink

我正在尝试通过 Espresso 检查 phone 拨号器是否以正确的 phone 号码打开,但目前还无法做到。 我试过:

    Intents.init();

    Intent stubIntent = new Intent();
    Instrumentation.ActivityResult stubResult = new Instrumentation.ActivityResult(Activity.RESULT_OK, stubIntent);
    intending(hasAction(Intent.ACTION_DIAL)).respondWith(stubResult);
    intended(Matchers.allOf(hasAction(Intent.ACTION_DIAL), hasData(Uri.parse("+3531234567"))));

还有

    Intents.init();

    intended(allOf(hasAction(Intent.ACTION_DIAL), hasData(Uri.parse("+3531234567"))));

正在通过我 XML 上 TextView 上的自动链接触发拨号程序,如果这有任何帮助?

android:autoLink="all"

这就是 Espresso 打开的拨号器

非常感谢。

我认为您需要在号码前加上 tel:,所以请尝试:

Uri.parse("tel:+3531234567")

它似乎确实不适用于 autoLink 并且基于@Aaron 的巨大帮助,这对我有用。

1 - 从 TextView

中删除自动链接

2 - 通常通过 OnClickListener 触发意图

tvCallNumber.setText(phone); // phone = +3531234567

    tvCallNumber.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phone)); 
            startActivity(intent);
        }
    });

3 - 确保 phone 号码的 前缀为 "tel:"

4 - 测试操作正确顺序

    Intents.init();

    Intent stubIntent = new Intent();
    Instrumentation.ActivityResult stubResult = new Instrumentation.ActivityResult(Activity.RESULT_OK, stubIntent);

    intending(hasAction(Intent.ACTION_DIAL)).respondWith(stubResult);
    onView(withId(R.id.tv_call_number)).perform(click());
    intended(Matchers.allOf(hasAction(Intent.ACTION_DIAL), hasData(Uri.parse("tel:+3531234567"))));