ServiceTestRule

public class ServiceTestRule
extends Object implements TestRule

java.lang.Object
   ↳ android.support.test.rule.ServiceTestRule


一个JUnit规则,提供简化的机制,在测试持续时间之前和之后启动和关闭服务。 它还保证服务在启动(或绑定到)服务时已成功连接。 该服务可以使用辅助方法之一启动(或绑定)。 测试完成后它将自动停止(或解除绑定),并且任何使用After注释的方法都已完成。

注意:此规则不支持IntentService因为当onHandleIntent(android.content.Intent)完成所有未完成的命令时它会自动销毁。 因此,无法及时建立成功的连接。

用法:

 @Rule
 public final ServiceTestRule mServiceRule = new ServiceTestRule();

 @Test
 public void testWithStartedService() {
     mServiceRule.startService(
         new Intent(InstrumentationRegistry.getTargetContext(), MyService.class));
     //do something
 }

 @Test
 public void testWithBoundService() {
     IBinder binder = mServiceRule.bindService(
         new Intent(InstrumentationRegistry.getTargetContext(), MyService.class));
     MyService service = ((MyService.LocalBinder) binder).getService();
     assertTrue("True wasn't returned", service.doSomethingToReturnTrue());
 }
 

Summary

Public constructors

ServiceTestRule()

创建 ServiceTestRule ,默认超时时间为5秒

Public methods

Statement apply(Statement base, Description description)
IBinder bindService(Intent intent)

bindService(android.content.Intent, android.content.ServiceConnection, int)一样 bindService(android.content.Intent, android.content.ServiceConnection, int)除了使用内部 ServiceConnection来保证成功的界限。

IBinder bindService(Intent intent, ServiceConnection connection, int flags)

在测试开始的服务,就好像它是由开始以同样的方式 Context.bindService(Intent, ServiceConnection, flags)Intent标识服务。

void startService(Intent intent)

启动被测服务并阻塞,直到服务连接,就像 Context.startService(Intent)Intent启动标识服务一样。

static ServiceTestRule withTimeout(long timeout, TimeUnit timeUnit)

工厂方法创建具有自定义超时的 ServiceTestRule

Protected methods

void afterService()

重写此方法以在服务关闭后执行您自己的服务特定清理。

void beforeService()

在开始或绑定服务之前,重写此方法以执行您自己的特定于服务的初始化。

Inherited methods

From class java.lang.Object
From interface org.junit.rules.TestRule

Public constructors

ServiceTestRule

ServiceTestRule ()

创建一个 ServiceTestRule ,默认超时时间为5秒

Public methods

apply

Statement apply (Statement base, 
                Description description)

Parameters
base Statement
description 描述
Returns
Statement

bindService

IBinder bindService (Intent intent)

bindService(android.content.Intent, android.content.ServiceConnection, int)一样bindService(android.content.Intent, android.content.ServiceConnection, int)除了使用内部ServiceConnection来保证成功的界限。 操作选项标志默认为BIND_AUTO_CREATE

Parameters
intent Intent
Returns
IBinder
Throws
TimeoutException

也可以看看:

bindService

IBinder bindService (Intent intent, 
                ServiceConnection connection, 
                int flags)

开始测试服务,就像Context.bindService(Intent, ServiceConnection, flags)以识别服务的Intent启动服务一样。 但是,它在返回之前等待onServiceConnected(android.content.ComponentName, android.os.IBinder)被调用。

Parameters
intent Intent: Identifies the service to connect to. The Intent may specify either an explicit component name, or a logical description (action, category, etc) to match an IntentFilter published by a service.
connection ServiceConnection: Receives information as the service is started and stopped. This must be a valid ServiceConnection object; it must not be null.
flags int: Operation options for the binding. May be 0, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, BIND_ABOVE_CLIENT, BIND_ALLOW_OOM_MANAGEMENT, or BIND_WAIVE_PRIORITY.
Returns
IBinder An object whose type is a subclass of IBinder, for making further calls into the service.
Throws
SecurityException if the called doesn't have permission to bind to the given service.
TimeoutException if timed out waiting for a successful connection with the service.

也可以看看:

startService

void startService (Intent intent)

直到服务连接,以相同的方式,就好像它是由开始测试和块启动服务Context.startService(Intent)Intent标识的服务。 如果您使用此方法启动服务,则会在测试运行结束时自动停止。 但是,它也绑定到该服务并等待onServiceConnected(android.content.ComponentName, android.os.IBinder)被调用以确保连接成功。

Parameters
intent Intent: An Intent that identifies a service, of the same form as the Intent passed to Context.startService (Intent).
Throws
SecurityException if you do not have permission to bind to the given service.
TimeoutException if timed out waiting for a successful connection with the service.

withTimeout

ServiceTestRule withTimeout (long timeout, 
                TimeUnit timeUnit)

工厂方法创建具有自定义超时的 ServiceTestRule

Parameters
timeout long: the amount of time to wait for a service to connect.
timeUnit TimeUnit: the time unit representing how the timeout parameter should be interpreted
Returns
ServiceTestRule a ServiceTestRule with the desired timeout

Protected methods

afterService

void afterService ()

重写此方法以在服务关闭后执行您自己的服务特定清理。 在执行每个测试方法后调用该方法,包括使用After注释的任何方法以及调用停止(或解除绑定)被测试服务的必要调用。

beforeService

void beforeService ()

在开始或绑定服务之前,重写此方法以执行您自己的特定于服务的初始化。 在每个测试方法执行之前调用该方法,包括使用Before注解的任何方法。 不要从这里开始或绑定到服务!