测试自定义View的onMeasure / onLayout / onDraw方法有什么好方法?

时间:2022-09-06 18:47:19

I just wrote a custom View class that, among other things, allows a developer to easily set borders (e.g. setBorderWidth, setBorderColor, setBorderWidthLeft, etc.). I did this by overriding onMeasure/onDraw and I want to test that the View properly draws these borders.

我刚刚编写了一个自定义View类,除其他外,它允许开发人员轻松设置边框(例如setBorderWidth,setBorderColor,setBorderWidthLeft等)。我通过覆盖onMeasure / onDraw来做到这一点,我想测试View正确绘制这些边框。

Ideally, I want something at a higher level than a unit test: basically I want to enforce that if I set borders, they are drawn as expected; if I don't set them they aren't drawn; if I change them, the new borders are drawn and the old ones are no longer visible. This allows me to know that my view is working at a high level.

理想情况下,我想要一个比单元测试更高级别的东西:基本上我想强制执行如果我设置边框,它们按预期绘制;如果我不设置它们就不会被绘制;如果我更改它们,则会绘制新边框并且不再显示旧边框。这让我知道我的观点在高层次上工作。

Things I've considered:

我考虑过的事情:

  1. Taking the view in isolation with Robolectric and calling onDraw manually with a mock Canvas (doesn't test invalidation though)
  2. 将视图与Robolectric隔离并使用模拟Canvas手动调用onDraw(但不测试失效)

  3. Making an Activity test case and somehow saving a screenshot of the Activity and analyzing it programmatically.
  4. 制作一个Activity测试用例并以某种方式保存Activity的屏幕截图并以编程方式对其进行分析。

Neither of these seem great to me, but I'm leaning towards 2). Any other ideas?

这些对我来说都不是很好,但我倾向于2)。还有其他想法吗?

1 个解决方案

#1


0  

For testing onDraw, onLayout and onMeasurescreenshots-tests-for-android is considerable option.

对于测试onDraw,onLayout和onMeasurescreenshots-tests-for-android是相当多的选择。

#1


0  

For testing onDraw, onLayout and onMeasurescreenshots-tests-for-android is considerable option.

对于测试onDraw,onLayout和onMeasurescreenshots-tests-for-android是相当多的选择。