Guest User

Untitled

a guest
Nov 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. <declare-styleable name="PercentageView">
  2. <attr name="image" format="integer" />
  3. </declare-styleable>
  4.  
  5. public class PercentageView extends View {
  6. private int _activeColor;
  7. private int _barBackgroundColor;
  8. private float _barHeight;
  9. private Drawable _icon;
  10. private Paint _barBackgroundPaint;
  11. private Paint _barActivePaint;
  12. private int _percent;
  13.  
  14. public PercentageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  15. super(context, attrs, defStyleAttr, defStyleRes);
  16. TypedArray args = context.getTheme().obtainStyledAttributes(
  17. attrs,
  18. R.styleable.PercentageView,
  19. 0,
  20. 0);
  21.  
  22. try {
  23. _icon = args.getDrawable(R.styleable.PercentageView_image);
  24.  
  25. } finally {
  26. args.recycle();
  27. }
  28.  
  29. init();
  30. }
  31. }
  32.  
  33. @RunWith(RobolectricTestRunner.class)
  34. public class PercentageViewTests {
  35. private ShadowCanvas _shadowCanvas;
  36. private int _width;
  37. private int _height;
  38. private float _expectedPercentWidth;
  39. private int _thePercent;
  40. private Canvas _canvas;
  41.  
  42. private PercentageView SetUpView(int width, int height, int percent, boolean useIcon) {
  43. ShadowApplication app = shadowOf(RuntimeEnvironment.application);
  44.  
  45. //Attempt to pass in the icon, if useIcon is true
  46. AttributeSet attr = useIcon ? Robolectric.buildAttributeSet()
  47. .addAttribute(R.styleable.PercentageView_image, "@drawable/app_enabled") //NullPointerException here
  48. .build() : null;
  49.  
  50. PercentageView view = new PercentageView(app.getApplicationContext(), attr);
  51. view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
  52. view.setPercent(percent);
  53.  
  54. return view;
  55. }
  56.  
  57. @Before
  58. public void setup() {
  59. Random random = new Random(10);
  60. _width = random.nextInt(100);
  61. random.setSeed(101);
  62. _height = random.nextInt(200);
  63.  
  64. random = new Random(0);
  65. _thePercent = random.nextInt(100);
  66. _expectedPercentWidth = _width * _thePercent / 100;
  67.  
  68. _canvas = new Canvas();
  69. _shadowCanvas = shadowOf(_canvas);
  70. }
  71.  
  72. @Test
  73. public void ShouldHaveIcon() {
  74. PercentageView view = SetUpView(_width, _height, _thePercent, true);
  75. view.onDraw(_canvas);
  76.  
  77. assertThat(_shadowCanvas.getRectPaintHistoryCount(), is(equalTo(3)));
  78. }
  79. }
  80.  
  81. java.lang.NullPointerException
  82. at org.robolectric.Robolectric$AttributeSetBuilder.addAttribute(Robolectric.java:161)
  83. at myorg.controls.PercentageViewTests.SetUpView(PercentageViewTests.java:41)
  84. at myorg.controls.PercentageViewTests.ShouldHaveIcon(PercentageViewTests.java:140)
  85. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  86. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  87. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  88. at java.lang.reflect.Method.invoke(Method.java:498)
  89. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  90. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  91. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  92. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  93. at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:523)
  94. at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  95. at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:226)
  96. at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
  97. at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
  98. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  99. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  100. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  101. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  102. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  103. at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:62)
  104. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  105. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  106. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  107. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
  108. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  109. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  110. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  111. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  112. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  113. at java.lang.reflect.Method.invoke(Method.java:498)
  114. at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Add Comment
Please, Sign In to add comment