unit-testing - 使用 Robolectric 对 android listview 滚动进行单元测试

标签 unit-testing scroll robolectric

我有一个方法尝试以编程方式滚动到 ListView 中的某个位置。该方法有一些条件,因此其实现会根据应用程序运行的 Android SDK 版本而略有不同。

该功能在我测试过的 3 台 Android 设备上运行良好。但是,我已经使用 junitRoboelectric 编写了单元测试。检查滚动是否使目标位置处的项目可见。测试失败。当我调试时,我注意到在使用 Roboelectric 运行时(即在桌面上与设备或模拟器上),android.os.Build.VERSION.SDK_INT 为 0。

我尝试忽略版本并仅使用 ListView.smoothScrollToPosition(),但使用 getFirstVisilePoition()getLastVisiblePosition() 方法即使在我调用 smoothScrollToPosition(150) 之后立即继续返回 0。

有谁知道是否/如何使用 Roboelectric 测试滚动 ListView ?

任何帮助将不胜感激 - 我似乎找不到有关该主题的任何信息。

谢谢

安娜

最佳答案

如果您使用的是 robolectric 1.1 或 1.2,这里是 AbsListView 的源代码: https://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAbsListView.java

看起来所有的滚动功能都是由 ShadowAdapterView 实现的: https://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAdapterView.java

看起来没有实现 getFirstVisiblePoition()getLastVisiblePosition()

我能够获得平滑的滚动位置。

@RunWith(RobolectricTestRunner.class)
public class SmoothScroll {

    @Test
    public void testSmoothScroll() {
        Activity context = new Activity();
        ListView view = new ListView(context);

        view.smoothScrollToPosition(100);
        Assert.assertEquals(100, Robolectric.shadowOf(view).getSmoothScrolledPosition());
    }

}

可以获得平滑的滚动位置。根据元素的高度,您也许能够确定哪些元素是可见的。

关于unit-testing - 使用 Robolectric 对 android listview 滚动进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14912945/

相关文章:

python - 如何测试数据检索模块?

c# - 在 Content Size Fitter 更新内容的 Rect Transform 后,如何让 Unity Scroll Rect 滚动到底部?

jquery - 滚动过去后坚持导航

javascript - 计算滚动条的宽度并在 calc() css 中使用结果

android - Robolectric 与 test.R.java

android - 带有 Fabric Crashlytics 的 Robolectric 3

ios - 在单元测试中,我如何以编程方式关闭系统权限对话框?

ios - OCMock [OCMAnyConstraint isProxy] : message sent to deallocated instance, 发生了什么事?

Angular6 - 无法解析 selenium-webdriver\lib 中的 'fs'

android - java.lang.NoClassDefFoundError : Test Module could not resolve classes of other Feature Module, 单元测试 - Kotlin - Android