【问题标题】:onCreate() in ActivityInstrumentationTestCase2 not called when phone is locked/sleeping手机锁定/睡眠时未调用 ActivityInstrumentationTestCase2 中的 onCreate()
【发布时间】:2011-08-31 21:51:15
【问题描述】:

我正在使用 ActivityInstrumentationTestCase2 编写测试。

当模拟器打开并且活动可以进入前台时,我的测试工作完美。

当我在手机锁定的情况下开始测试时,不会创建活动。 这似乎是“有道理的”,因为如果活动无法进入前台(手机已锁定!),则没有理由加载活动。但是,在运行测试套件时,这会导致误报...

这是一个示例测试

public void testSplashscreenRedirectsSomewhereAfterTimeout() throws Exception {

    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(SomeActivity.class.getName(), null, false);

    getActivity(); // to make sure that the activity gets launched
    Activity activity = monitor.waitForActivityWithTimeout(10000); // waiting for the other activity
    assertNotNull("BaselineActivity was not called", activity);
}

当手机被锁定时,getActivity() 返回正确的活动,但它的 onCreate() 方法永远不会被调用。

有什么方法可以在屏幕被锁定的情况下进行测试?

【问题讨论】:

    标签: android testing android-activity android-emulator


    【解决方案1】:

    假设您对没有创建活动的看法是正确的,为什么不直接使用KeyGuardManager 解锁手机。除非在手机锁定和解锁时必须采取特定操作,或者您正在测试一项服务,否则我只会运行一个 “准备测试环境” 功能来检查屏幕是否被锁定并且解锁它并运行您的测试。

    How to tell if your screen is unlocked。如果没有,则将其解锁并运行测试。

    KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.disableKeyguard();

    更新

    使用以下命令解除 Key Guard Lock

    //Get the current window 
    Window window = getWindow();  
    //Add the Flag from the window manager class
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    【讨论】:

    • KeyguardManager 已弃用。有什么选择吗?
    猜你喜欢
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多