使用Robolectric进行粘性广播的自定义上下文

时间:2017-04-14 16:24:05

标签: android robolectric

我想用Robolectric测试我的BroadcastReceiver,这取决于粘性广播。默认情况下,Robolectric不支持粘性广播,因此我创建了自定义Context以获得如下工作的粘性广播:

public class MyContext extends MockContext {

    public MyContext() {
        super();
    }

    @Override
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        if(receiver == null) { // A sticky broadcast was requested
            Intent request = new Intent();
            String action = filter.getAction(0);

            if(Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                request.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
            } else if(Intent.ACTION_HEADSET_PLUG.equals(action)) {
                request.putExtra("state", 1); 
            }

            return request;
        }

        return super.registerReceiver(receiver, filter);
    }
}

我的问题是我必须使用RuntimeEnvironment.application.getApplicationContext来获取有效的Context对象(我试图简单地调用我的自定义Context的构造函数,但这不起作用)。那么如何才能获得我的自定义Context的有效实例,或者使用robolectric不可能实现这一点?

编辑:以下是我的测试和我的BroadcastReceiver的代码:

@Before
public void setup() {
    context = RuntimeEnvironment.application.getApplicationContext();
    receiver = new MyBroadcastReceiver(); // Create Receiver
}

@After
public void finish() {
    context.unregisterReceiver(receiver);
}

@Test
public void validateUsbChargingChange() {
    IntentFilter filter = new IntentFilter("android.intent.action.ACTION_POWER_CONNECTED");
    context.registerReceiver(receiver, filter);

    // Simmulate SocketCharging by sending the corresponding Intent
    Intent chargingChange = new Intent("android.intent.action.ACTION_POWER_CONNECTED");
    RuntimeEnvironment.application.sendBroadcast(chargingChange);

    validatePreferences();
}

广播接收器:

@Override
public void onReceive(Context context, Intent intent) {
    IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent chargingIntent = appContext.registerReceiver(null, iFilter); // sticky
    int pluggedState = chargingIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

    boolean usbCharge = (pluggedState == BatteryManager.BATTERY_PLUGGED_USB);
    if(usbCharge) {  /* Write values to preferences */  }
}

1 个答案:

答案 0 :(得分:0)

您可以在测试类构造函数中显式发送StickBroadcast,如下所示

@RunWith(RobolectricTestRunner.class)
public class BatterySettingActivityTest {
    {
        ApplicationProvider.getApplicationContext().sendStickyBroadcast(new Intent(Intent.ACTION_BATTERY_CHANGED).putExtra(BatteryManager.EXTRA_LEVEL, 75).putExtra(BatteryManager.EXTRA_PLUGGED, 0));
    }

    @Rule
    public ActivityScenarioRule<BatterySettingActivity> activityScenarioRule = new ActivityScenarioRule<>(BatterySettingActivity.class);


    @Test
    public void testStickBroadcast() {
        activityScenarioRule.getScenario().onActivity(activity -> {
            System.out.println("intentForStickyBroadcast=" + activity.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)));
        });
    }
}