gpt4 book ai didi

android - 用 robolectric 测试 toast

转载 作者:行者123 更新时间:2023-11-29 00:13:50 28 4
gpt4 key购买 nike

当使用 Robolectric 执行测试时,我的 BroadcastReceiver 中的

onReceive 永远不会被调用。

测试

@Test
public void WhenSentNewMessageInAirplaneMode_ToastShouldBeDisplayed() {
// Arrange
ShadowSettings.setAirplaneMode(true);
ImageButton sendBtn = (ImageButton)activity.findViewById(R.id.sendBtn);
EditText messageEditText = (EditText)activity.findViewById(R.id.messageEditText);
EditText recipientsEditText = (EditText)activity.findViewById(R.id.recipientsEditText);

messageEditText.setText("<a message>");
recipientsEditText.setText("<a address>");

// Act
sendBtn.performClick();

// Assert
assertEquals(Robolectric.application.getString(R.string.message_not_sent), ShadowToast.getTextOfLatestToast()); // --> ShadowToast.getTextOfLatestToast() is always null
}

我的 Activity

创建时

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SmsMessageSender.SENT), PendingIntent.FLAG_UPDATE_CURRENT);

覆盖

@Override
protected void onPause() {
super.onPause();
unregisterReceiver(smsSentReceiver);
}

@Override
protected void onResume() {
super.onResume(); // This call always
smsSentReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
if (getResultCode() != Activity.RESULT_OK) {
Toast.makeText(getBaseContext(), getString(R.string.message_not_sent), Toast.LENGTH_LONG).show(); // On test this never call
}
}
};

registerReceiver(smsSentReceiver, new IntentFilter(SmsMessageSender.SENT));
}

点击发送按钮

smsManager.sendMultipartTextMessage(recipient, null, messageParts,
new ArrayList<>(Arrays.asList(sentPI))
, null)
);

最佳答案

我认为这与您注册广播接收器的方式有关。如果你正在收听 LocalBroadcast,你可以像这样实现它们:

本地广播 Activity

package be.acuzio.mrta;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import be.acuzio.mrta.R;
import be.acuzio.mrta.util.Logger;

public class LocalBroadcastActivity extends Activity {
final private BroadcastReceiver myReceiver =new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Logger.d(TAG, "onReceive called");

Toast.makeText(context, "onReceive Toast Called", Toast.LENGTH_SHORT).show();
}
};

private final static String TAG = LocalBroadcastActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_broadcast);
}

@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(this.myReceiver);
}

@Override
protected void onResume() {
super.onResume();

LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter("be.acuzio.mrta.LOCAL_BROADCAST"));
}
}

本地广播 Activity 测试

在您的测试中,确保您构建了 Activity 并触发了恢复。

package be.acuzio.mrta.test;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowToast;

import java.util.Iterator;
import java.util.List;

import be.acuzio.mrta.LocalBroadcastActivity;
import be.acuzio.mrta.MainActivity;
import be.acuzio.mrta.receiver.MyBroadcastReceiver;


@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class LocalBroadcastActivityTest {
@Before
public void setup() {
//do whatever is necessary before every test
}

@Test
public void testActivityFound() {
Activity activity = Robolectric.buildActivity(LocalBroadcastActivity.class).create().get();

Assert.assertNotNull(activity);
}

@Test
public void testBroadcastReceiverRegistered() {
Activity activity = Robolectric.buildActivity(LocalBroadcastActivity.class).create().resume().get();

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(activity);

localBroadcastManager.sendBroadcast(new Intent("be.acuzio.mrta.LOCAL_BROADCAST"));

Assert.assertEquals("onReceive Toast Called", ShadowToast.getTextOfLatestToast());

}
}

希望这对您有所帮助!

注意:尝试使您的代码可测试:尝试将不同的“功能”拆分到不同的文件/类中,您可以单独对其进行测试。

关于android - 用 robolectric 测试 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28051396/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com