I've got a PercentView class, and I'm trying to set up a test where I want to verify it was drawn to the correct size. I've got this working for a simple bar, but now I want an image/icon above it, which is a drawable (vector actually).
Some notes about this:
- I have a drawable file (which is vector) that is named
app_enabled.xml, which ismain\res\drawable\app_enabled.xml. - I'm using
Robolectric 3.5.1 - I've seen the code for the test the Robolectric team has here: Robolectric example: ViewStubTest.java
I've declared the stylable:
<declare-styleable name="PercentageView">
<attr name="image" format="integer" />
</declare-styleable>
And the view:
public class PercentageView extends View {
private int _activeColor;
private int _barBackgroundColor;
private float _barHeight;
private Drawable _icon;
private Paint _barBackgroundPaint;
private Paint _barActivePaint;
private int _percent;
public PercentageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray args = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.PercentageView,
0,
0);
try {
_icon = args.getDrawable(R.styleable.PercentageView_image);
} finally {
args.recycle();
}
init();
}
}
And now the test:
@RunWith(RobolectricTestRunner.class)
public class PercentageViewTests {
private ShadowCanvas _shadowCanvas;
private int _width;
private int _height;
private float _expectedPercentWidth;
private int _thePercent;
private Canvas _canvas;
private PercentageView SetUpView(int width, int height, int percent, boolean useIcon) {
ShadowApplication app = shadowOf(RuntimeEnvironment.application);
//Attempt to pass in the icon, if useIcon is true
AttributeSet attr = useIcon ? Robolectric.buildAttributeSet()
.addAttribute(R.styleable.PercentageView_image, "@drawable/app_enabled") //NullPointerException here
.build() : null;
PercentageView view = new PercentageView(app.getApplicationContext(), attr);
view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
view.setPercent(percent);
return view;
}
@Before
public void setup() {
Random random = new Random(10);
_width = random.nextInt(100);
random.setSeed(101);
_height = random.nextInt(200);
random = new Random(0);
_thePercent = random.nextInt(100);
_expectedPercentWidth = _width * _thePercent / 100;
_canvas = new Canvas();
_shadowCanvas = shadowOf(_canvas);
}
@Test
public void ShouldHaveIcon() {
PercentageView view = SetUpView(_width, _height, _thePercent, true);
view.onDraw(_canvas);
assertThat(_shadowCanvas.getRectPaintHistoryCount(), is(equalTo(3)));
}
}
And the VERY sad, stack trace:
java.lang.NullPointerException
at org.robolectric.Robolectric$AttributeSetBuilder.addAttribute(Robolectric.java:161)
at myorg.controls.PercentageViewTests.SetUpView(PercentageViewTests.java:41)
at myorg.controls.PercentageViewTests.ShouldHaveIcon(PercentageViewTests.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:523)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:226)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:62)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)