Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.71 KB | None | 0 0
  1. package com.sudox.design.widgets.etlayout.label
  2.  
  3. import android.graphics.Canvas
  4. import android.graphics.Paint
  5. import android.graphics.Rect
  6. import android.widget.EditText
  7. import com.sudox.design.helpers.isTextRtl
  8. import org.junit.Assert
  9. import org.junit.Test
  10. import org.junit.runner.RunWith
  11. import org.mockito.ArgumentMatchers
  12. import org.mockito.ArgumentMatchers.anyFloat
  13. import org.mockito.ArgumentMatchers.anyString
  14. import org.mockito.Mockito
  15. import org.powermock.api.mockito.PowerMockito
  16. import org.powermock.core.classloader.annotations.PrepareForTest
  17. import org.powermock.modules.junit4.PowerMockRunner
  18.  
  19. @RunWith(PowerMockRunner::class)
  20. @PrepareForTest(EditTextLayoutLabel::class, Canvas::class, EditText::class, Paint::class,
  21. fullyQualifiedNames = ["com.sudox.design.helpers.TextHelperKt"])
  22. class EditTextLayoutLabelTest : Assert() {
  23.  
  24. @Test
  25. fun testInactiveState() {
  26. val editTextLayoutLabel = PowerMockito.mock(EditTextLayoutLabel::class.java)
  27. val editText = PowerMockito.mock(EditText::class.java)
  28. val params = EditTextLayoutLabelParams()
  29.  
  30. PowerMockito.`when`(editTextLayoutLabel.needShowingError()).thenCallRealMethod()
  31.  
  32. EditTextLayoutLabel::class.java
  33. .getDeclaredField("editText")
  34. .apply { isAccessible = true }
  35. .set(editTextLayoutLabel, editText)
  36.  
  37. EditTextLayoutLabel::class.java
  38. .getDeclaredField("errorText")
  39. .apply { isAccessible = true }
  40. .set(editTextLayoutLabel, "Error")
  41.  
  42. EditTextLayoutLabel::class.java
  43. .getDeclaredField("originalText")
  44. .apply { isAccessible = true }
  45. .set(editTextLayoutLabel, "Original")
  46.  
  47. EditTextLayoutLabel::class.java
  48. .getDeclaredField("params")
  49. .apply { isAccessible = true }
  50. .set(editTextLayoutLabel, params)
  51.  
  52. params.errorTextColor = 1
  53.  
  54. PowerMockito.`when`(editText.currentHintTextColor).thenReturn(2)
  55. PowerMockito.`when`(editText.isEnabled).thenReturn(true)
  56. PowerMockito.`when`(editTextLayoutLabel.isEditTextActive()).thenReturn(false)
  57. PowerMockito.`when`(editTextLayoutLabel.getCurrentColor()).thenCallRealMethod()
  58. PowerMockito.`when`(editTextLayoutLabel.getCurrentText()).thenCallRealMethod()
  59.  
  60. assertEquals(1, editTextLayoutLabel.getCurrentColor())
  61. assertEquals("Error", editTextLayoutLabel.getCurrentText())
  62.  
  63. PowerMockito.`when`(editText.isEnabled).thenReturn(false)
  64. assertEquals(2, editTextLayoutLabel.getCurrentColor())
  65. assertEquals("Original", editTextLayoutLabel.getCurrentText())
  66.  
  67. // Without error
  68. PowerMockito.`when`(editText.isEnabled).thenReturn(true)
  69. EditTextLayoutLabel::class.java
  70. .getDeclaredField("errorText")
  71. .apply { isAccessible = true }
  72. .set(editTextLayoutLabel, null)
  73.  
  74. assertEquals(2, editTextLayoutLabel.getCurrentColor())
  75. assertEquals("Original", editTextLayoutLabel.getCurrentText())
  76.  
  77. PowerMockito.`when`(editText.isEnabled).thenReturn(false)
  78. assertEquals(2, editTextLayoutLabel.getCurrentColor())
  79. assertEquals("Original", editTextLayoutLabel.getCurrentText())
  80. }
  81.  
  82. @Test
  83. fun testActiveState() {
  84. val editTextLayoutLabel = PowerMockito.mock(EditTextLayoutLabel::class.java)
  85. val editText = PowerMockito.mock(EditText::class.java)
  86. val params = EditTextLayoutLabelParams()
  87.  
  88. PowerMockito.`when`(editTextLayoutLabel.needShowingError()).thenCallRealMethod()
  89.  
  90. EditTextLayoutLabel::class.java
  91. .getDeclaredField("editText")
  92. .apply { isAccessible = true }
  93. .set(editTextLayoutLabel, editText)
  94.  
  95. EditTextLayoutLabel::class.java
  96. .getDeclaredField("errorText")
  97. .apply { isAccessible = true }
  98. .set(editTextLayoutLabel, "Error")
  99.  
  100. EditTextLayoutLabel::class.java
  101. .getDeclaredField("originalText")
  102. .apply { isAccessible = true }
  103. .set(editTextLayoutLabel, "Original")
  104.  
  105. EditTextLayoutLabel::class.java
  106. .getDeclaredField("params")
  107. .apply { isAccessible = true }
  108. .set(editTextLayoutLabel, params)
  109.  
  110. params.errorTextColor = 1
  111.  
  112. PowerMockito.`when`(editText.currentHintTextColor).thenReturn(2)
  113. PowerMockito.`when`(editText.currentTextColor).thenReturn(3)
  114. PowerMockito.`when`(editText.isEnabled).thenReturn(true)
  115. PowerMockito.`when`(editTextLayoutLabel.isEditTextActive()).thenReturn(true)
  116. PowerMockito.`when`(editTextLayoutLabel.getCurrentColor()).thenCallRealMethod()
  117. PowerMockito.`when`(editTextLayoutLabel.getCurrentText()).thenCallRealMethod()
  118.  
  119. assertEquals(1, editTextLayoutLabel.getCurrentColor())
  120. assertEquals("Error", editTextLayoutLabel.getCurrentText())
  121.  
  122. // Without error
  123. PowerMockito.`when`(editText.isEnabled).thenReturn(true)
  124.  
  125. EditTextLayoutLabel::class.java
  126. .getDeclaredField("errorText")
  127. .apply { isAccessible = true }
  128. .set(editTextLayoutLabel, null)
  129.  
  130. assertEquals(3, editTextLayoutLabel.getCurrentColor())
  131. assertEquals("Original", editTextLayoutLabel.getCurrentText())
  132. }
  133.  
  134. @Test
  135. fun testDraw_no_text() {
  136. val editTextLayoutLabel = PowerMockito.mock(EditTextLayoutLabel::class.java)
  137. val canvas = PowerMockito.mock(Canvas::class.java)
  138.  
  139. PowerMockito.`when`(editTextLayoutLabel.getCurrentText()).thenReturn(null)
  140. PowerMockito.`when`(editTextLayoutLabel.dispatchDraw(canvas)).thenCallRealMethod()
  141. editTextLayoutLabel.dispatchDraw(canvas)
  142.  
  143. Mockito.verify(canvas, Mockito.never()).drawText(anyString(), anyFloat(), anyFloat(), ArgumentMatchers.any())
  144. }
  145.  
  146. @Test
  147. fun testDraw_normal() {
  148. val editTextLayoutLabel = PowerMockito.mock(EditTextLayoutLabel::class.java)
  149. val editText = PowerMockito.mock(EditText::class.java)
  150. val canvas = PowerMockito.mock(Canvas::class.java)
  151. val paint = PowerMockito.mock(Paint::class.java)
  152.  
  153. EditTextLayoutLabel::class.java
  154. .getDeclaredField("editText")
  155. .apply { isAccessible = true }
  156. .set(editTextLayoutLabel, editText)
  157.  
  158. EditTextLayoutLabel::class.java
  159. .getDeclaredField("paint")
  160. .apply { isAccessible = true }
  161. .set(editTextLayoutLabel, paint)
  162.  
  163. PowerMockito.`when`(editTextLayoutLabel.dispatchDraw(canvas)).thenCallRealMethod()
  164. PowerMockito.`when`(editTextLayoutLabel.getCurrentText()).thenReturn("Test")
  165. PowerMockito.`when`(editTextLayoutLabel.getHeight()).thenReturn(15)
  166. PowerMockito.`when`(editTextLayoutLabel.getCurrentColor()).thenReturn(3)
  167. PowerMockito.`when`(editTextLayoutLabel.getXCoord("Test")).thenReturn(10)
  168. editTextLayoutLabel.dispatchDraw(canvas)
  169.  
  170. Mockito.verify(paint).color = 3
  171. Mockito.verify(canvas).drawText("Test", 10F, 15F, paint)
  172. }
  173.  
  174. @Test
  175. fun testXCoordRtl() {
  176. val editTextLayoutLabel = PowerMockito.mock(EditTextLayoutLabel::class.java)
  177. val editText = PowerMockito.mock(EditText::class.java)
  178. val paint = PowerMockito.mock(Paint::class.java)
  179. val bounds = PowerMockito.mock(Rect::class.java)
  180. val editTextWidth = 512
  181. val paddingStart = 10
  182. val textWidth = 256
  183. val text = "لوحة المفاتيح العربية"
  184.  
  185. EditTextLayoutLabel::class.java
  186. .getDeclaredField("editText")
  187. .apply { isAccessible = true }
  188. .set(editTextLayoutLabel, editText)
  189.  
  190. EditTextLayoutLabel::class.java
  191. .getDeclaredField("paint")
  192. .apply { isAccessible = true }
  193. .set(editTextLayoutLabel, paint)
  194.  
  195. EditTextLayoutLabel::class.java
  196. .getDeclaredField("bounds")
  197. .apply { isAccessible = true }
  198. .set(editTextLayoutLabel, bounds)
  199.  
  200. PowerMockito.mockStatic(Class.forName("com.sudox.design.helpers.TextHelperKt"))
  201. PowerMockito.`when`(editText.isTextRtl(text)).thenReturn(true)
  202.  
  203. PowerMockito.`when`(editText.measuredWidth).thenReturn(editTextWidth)
  204. PowerMockito.`when`(editText.compoundPaddingStart).thenReturn(paddingStart)
  205. PowerMockito.`when`(bounds.width()).thenReturn(textWidth)
  206. PowerMockito.`when`(editTextLayoutLabel.getXCoord(anyString())).thenCallRealMethod()
  207.  
  208. val result = editTextLayoutLabel.getXCoord(text)
  209. assertEquals(editTextWidth - paddingStart - textWidth, result)
  210.  
  211. Mockito.verify(paint).getTextBounds(text, 0, text.length, bounds)
  212. }
  213.  
  214. @Test
  215. fun testXCoordLtr() {
  216. val editTextLayoutLabel = PowerMockito.mock(EditTextLayoutLabel::class.java)
  217. val editText = PowerMockito.mock(EditText::class.java)
  218. val paint = PowerMockito.mock(Paint::class.java)
  219. val bounds = PowerMockito.mock(Rect::class.java)
  220. val editTextWidth = 512
  221. val paddingStart = 10
  222. val textWidth = 256
  223. val text = "Hello World"
  224.  
  225. EditTextLayoutLabel::class.java
  226. .getDeclaredField("editText")
  227. .apply { isAccessible = true }
  228. .set(editTextLayoutLabel, editText)
  229.  
  230. EditTextLayoutLabel::class.java
  231. .getDeclaredField("paint")
  232. .apply { isAccessible = true }
  233. .set(editTextLayoutLabel, paint)
  234.  
  235. PowerMockito.mockStatic(Class.forName("com.sudox.design.helpers.TextHelperKt"))
  236. PowerMockito.`when`(editText.isTextRtl(text)).thenReturn(false)
  237.  
  238. PowerMockito.`when`(editText.measuredWidth).thenReturn(editTextWidth)
  239. PowerMockito.`when`(editText.compoundPaddingStart).thenReturn(paddingStart)
  240. PowerMockito.`when`(bounds.width()).thenReturn(textWidth)
  241. PowerMockito.`when`(editTextLayoutLabel.getXCoord(anyString())).thenCallRealMethod()
  242.  
  243. val result = editTextLayoutLabel.getXCoord(text)
  244. assertEquals(paddingStart, result)
  245. }
  246.  
  247. @Test
  248. fun testIsEditTextActive() {
  249. val editTextLayoutLabel = PowerMockito.mock(EditTextLayoutLabel::class.java)
  250. val editText = PowerMockito.mock(EditText::class.java)
  251.  
  252. EditTextLayoutLabel::class.java
  253. .getDeclaredField("editText")
  254. .apply { isAccessible = true }
  255. .set(editTextLayoutLabel, editText)
  256.  
  257. PowerMockito.`when`(editTextLayoutLabel.isEditTextActive()).thenCallRealMethod()
  258.  
  259. PowerMockito.`when`(editText.isFocused).thenReturn(true)
  260. PowerMockito.`when`(editText.isEnabled).thenReturn(false)
  261. assertFalse(editTextLayoutLabel.isEditTextActive())
  262.  
  263. PowerMockito.`when`(editText.isFocused).thenReturn(false)
  264. PowerMockito.`when`(editText.isPressed).thenReturn(true)
  265. PowerMockito.`when`(editText.isEnabled).thenReturn(false)
  266. assertFalse(editTextLayoutLabel.isEditTextActive())
  267.  
  268. PowerMockito.`when`(editText.isFocused).thenReturn(false)
  269. PowerMockito.`when`(editText.isPressed).thenReturn(true)
  270. PowerMockito.`when`(editText.isEnabled).thenReturn(true)
  271. assertTrue(editTextLayoutLabel.isEditTextActive())
  272.  
  273. PowerMockito.`when`(editText.isPressed).thenReturn(false)
  274. PowerMockito.`when`(editText.isFocused).thenReturn(true)
  275. PowerMockito.`when`(editText.isEnabled).thenReturn(true)
  276. assertTrue(editTextLayoutLabel.isEditTextActive())
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement