PageRenderTime 125ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/cts/tests/tests/view/src/android/view/cts/ChoreographerTest.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 316 lines | 230 code | 42 blank | 44 comment | 1 complexity | 34ccc70b7a19267bd22e94b3411f25c0 MD5 | raw file
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.view.cts;
  17. import android.test.AndroidTestCase;
  18. import android.view.Choreographer;
  19. public class ChoreographerTest extends AndroidTestCase {
  20. private static final long NOMINAL_VSYNC_PERIOD = 16;
  21. private static final long DELAY_PERIOD = NOMINAL_VSYNC_PERIOD * 5;
  22. private static final long NANOS_PER_MS = 1000000;
  23. private static final Object TOKEN = new Object();
  24. private Choreographer mChoreographer = Choreographer.getInstance();
  25. public void testFrameDelay() {
  26. assertTrue(Choreographer.getFrameDelay() > 0);
  27. long oldFrameDelay = Choreographer.getFrameDelay();
  28. long newFrameDelay = oldFrameDelay * 2;
  29. Choreographer.setFrameDelay(newFrameDelay);
  30. assertEquals(newFrameDelay, Choreographer.getFrameDelay());
  31. Choreographer.setFrameDelay(oldFrameDelay);
  32. }
  33. public void testPostCallbackWithoutDelayEventuallyRunsCallbacks() {
  34. MockRunnable addedCallback1 = new MockRunnable();
  35. MockRunnable addedCallback2 = new MockRunnable();
  36. MockRunnable removedCallback = new MockRunnable();
  37. try {
  38. // Add and remove a few callbacks.
  39. mChoreographer.postCallback(
  40. Choreographer.CALLBACK_ANIMATION, addedCallback1, null);
  41. mChoreographer.postCallback(
  42. Choreographer.CALLBACK_ANIMATION, addedCallback2, null);
  43. mChoreographer.postCallback(
  44. Choreographer.CALLBACK_ANIMATION, removedCallback, null);
  45. mChoreographer.removeCallbacks(
  46. Choreographer.CALLBACK_ANIMATION, removedCallback, null);
  47. // Sleep for a couple of frames.
  48. sleep(NOMINAL_VSYNC_PERIOD * 3);
  49. // We expect the remaining callbacks to have been invoked once.
  50. assertEquals(1, addedCallback1.invocationCount);
  51. assertEquals(1, addedCallback2.invocationCount);
  52. assertEquals(0, removedCallback.invocationCount);
  53. // If we post a callback again, then it should be invoked again.
  54. mChoreographer.postCallback(
  55. Choreographer.CALLBACK_ANIMATION, addedCallback1, null);
  56. sleep(NOMINAL_VSYNC_PERIOD * 3);
  57. assertEquals(2, addedCallback1.invocationCount);
  58. assertEquals(1, addedCallback2.invocationCount);
  59. assertEquals(0, removedCallback.invocationCount);
  60. // If the token matches, the the callback should be removed.
  61. mChoreographer.postCallback(
  62. Choreographer.CALLBACK_ANIMATION, addedCallback1, null);
  63. mChoreographer.postCallback(
  64. Choreographer.CALLBACK_ANIMATION, removedCallback, TOKEN);
  65. mChoreographer.removeCallbacks(
  66. Choreographer.CALLBACK_ANIMATION, null, TOKEN);
  67. sleep(NOMINAL_VSYNC_PERIOD * 3);
  68. assertEquals(3, addedCallback1.invocationCount);
  69. assertEquals(0, removedCallback.invocationCount);
  70. // If the action and token matches, then the callback should be removed.
  71. // If only the token matches, then the callback should not be removed.
  72. mChoreographer.postCallback(
  73. Choreographer.CALLBACK_ANIMATION, addedCallback1, TOKEN);
  74. mChoreographer.postCallback(
  75. Choreographer.CALLBACK_ANIMATION, removedCallback, TOKEN);
  76. mChoreographer.removeCallbacks(
  77. Choreographer.CALLBACK_ANIMATION, removedCallback, TOKEN);
  78. sleep(NOMINAL_VSYNC_PERIOD * 3);
  79. assertEquals(4, addedCallback1.invocationCount);
  80. assertEquals(0, removedCallback.invocationCount);
  81. } finally {
  82. mChoreographer.removeCallbacks(
  83. Choreographer.CALLBACK_ANIMATION, addedCallback1, null);
  84. mChoreographer.removeCallbacks(
  85. Choreographer.CALLBACK_ANIMATION, addedCallback2, null);
  86. mChoreographer.removeCallbacks(
  87. Choreographer.CALLBACK_ANIMATION, removedCallback, null);
  88. }
  89. }
  90. public void testPostCallbackWithDelayEventuallyRunsCallbacksAfterDelay() {
  91. MockRunnable addedCallback = new MockRunnable();
  92. MockRunnable removedCallback = new MockRunnable();
  93. try {
  94. // Add and remove a few callbacks.
  95. mChoreographer.postCallbackDelayed(
  96. Choreographer.CALLBACK_ANIMATION, addedCallback, null, DELAY_PERIOD);
  97. mChoreographer.postCallbackDelayed(
  98. Choreographer.CALLBACK_ANIMATION, removedCallback, null, DELAY_PERIOD);
  99. mChoreographer.removeCallbacks(
  100. Choreographer.CALLBACK_ANIMATION, removedCallback, null);
  101. // Sleep for a couple of frames.
  102. sleep(NOMINAL_VSYNC_PERIOD * 3);
  103. // The callbacks should not have been invoked yet because of the delay.
  104. assertEquals(0, addedCallback.invocationCount);
  105. assertEquals(0, removedCallback.invocationCount);
  106. // Sleep for the rest of the delay time.
  107. sleep(DELAY_PERIOD);
  108. // We expect the remaining callbacks to have been invoked.
  109. assertEquals(1, addedCallback.invocationCount);
  110. assertEquals(0, removedCallback.invocationCount);
  111. // If the token matches, the the callback should be removed.
  112. mChoreographer.postCallbackDelayed(
  113. Choreographer.CALLBACK_ANIMATION, addedCallback, null, DELAY_PERIOD);
  114. mChoreographer.postCallbackDelayed(
  115. Choreographer.CALLBACK_ANIMATION, removedCallback, TOKEN, DELAY_PERIOD);
  116. mChoreographer.removeCallbacks(
  117. Choreographer.CALLBACK_ANIMATION, null, TOKEN);
  118. sleep(NOMINAL_VSYNC_PERIOD * 3 + DELAY_PERIOD);
  119. assertEquals(2, addedCallback.invocationCount);
  120. assertEquals(0, removedCallback.invocationCount);
  121. // If the action and token matches, then the callback should be removed.
  122. // If only the token matches, then the callback should not be removed.
  123. mChoreographer.postCallbackDelayed(
  124. Choreographer.CALLBACK_ANIMATION, addedCallback, TOKEN, DELAY_PERIOD);
  125. mChoreographer.postCallbackDelayed(
  126. Choreographer.CALLBACK_ANIMATION, removedCallback, TOKEN, DELAY_PERIOD);
  127. mChoreographer.removeCallbacks(
  128. Choreographer.CALLBACK_ANIMATION, removedCallback, TOKEN);
  129. sleep(NOMINAL_VSYNC_PERIOD * 3 + DELAY_PERIOD);
  130. assertEquals(3, addedCallback.invocationCount);
  131. assertEquals(0, removedCallback.invocationCount);
  132. } finally {
  133. mChoreographer.removeCallbacks(
  134. Choreographer.CALLBACK_ANIMATION, addedCallback, null);
  135. mChoreographer.removeCallbacks(
  136. Choreographer.CALLBACK_ANIMATION, removedCallback, null);
  137. }
  138. }
  139. public void testPostCallbackThrowsIfRunnableIsNull() {
  140. try {
  141. mChoreographer.postCallback(
  142. Choreographer.CALLBACK_ANIMATION, null, TOKEN);
  143. fail("Expected IllegalArgumentException");
  144. } catch (IllegalArgumentException ex) {
  145. // expected
  146. }
  147. }
  148. public void testPostCallbackDelayedThrowsIfRunnableIsNull() {
  149. try {
  150. mChoreographer.postCallbackDelayed(
  151. Choreographer.CALLBACK_ANIMATION, null, TOKEN, DELAY_PERIOD);
  152. fail("Expected IllegalArgumentException");
  153. } catch (IllegalArgumentException ex) {
  154. // expected
  155. }
  156. }
  157. public void testPostFrameCallbackWithoutDelayEventuallyRunsFrameCallbacks() {
  158. MockFrameCallback addedFrameCallback1 = new MockFrameCallback();
  159. MockFrameCallback addedFrameCallback2 = new MockFrameCallback();
  160. MockFrameCallback removedFrameCallback = new MockFrameCallback();
  161. try {
  162. // Add and remove a few callbacks.
  163. long postTimeNanos = System.nanoTime();
  164. mChoreographer.postFrameCallback(addedFrameCallback1);
  165. mChoreographer.postFrameCallback(addedFrameCallback2);
  166. mChoreographer.postFrameCallback(removedFrameCallback);
  167. mChoreographer.removeFrameCallback(removedFrameCallback);
  168. // Sleep for a couple of frames.
  169. sleep(NOMINAL_VSYNC_PERIOD * 3);
  170. // We expect the remaining callbacks to have been invoked once.
  171. assertEquals(1, addedFrameCallback1.invocationCount);
  172. assertEquals(1, addedFrameCallback2.invocationCount);
  173. assertEquals(0, removedFrameCallback.invocationCount);
  174. assertTimeDeltaLessThan(addedFrameCallback1.frameTimeNanos - postTimeNanos,
  175. NOMINAL_VSYNC_PERIOD * 3 * NANOS_PER_MS);
  176. assertTimeDeltaLessThan(addedFrameCallback2.frameTimeNanos - postTimeNanos,
  177. NOMINAL_VSYNC_PERIOD * 3 * NANOS_PER_MS);
  178. assertTimeDeltaLessThan(Math.abs(addedFrameCallback2.frameTimeNanos
  179. - addedFrameCallback1.frameTimeNanos), NOMINAL_VSYNC_PERIOD * NANOS_PER_MS);
  180. // If we post a callback again, then it should be invoked again.
  181. postTimeNanos = System.nanoTime();
  182. mChoreographer.postFrameCallback(addedFrameCallback1);
  183. sleep(NOMINAL_VSYNC_PERIOD * 3);
  184. assertEquals(2, addedFrameCallback1.invocationCount);
  185. assertEquals(1, addedFrameCallback2.invocationCount);
  186. assertEquals(0, removedFrameCallback.invocationCount);
  187. assertTimeDeltaLessThan(addedFrameCallback1.frameTimeNanos - postTimeNanos,
  188. NOMINAL_VSYNC_PERIOD * 3 * NANOS_PER_MS);
  189. } finally {
  190. mChoreographer.removeFrameCallback(addedFrameCallback1);
  191. mChoreographer.removeFrameCallback(addedFrameCallback2);
  192. mChoreographer.removeFrameCallback(removedFrameCallback);
  193. }
  194. }
  195. public void testPostFrameCallbackWithDelayEventuallyRunsFrameCallbacksAfterDelay() {
  196. MockFrameCallback addedFrameCallback = new MockFrameCallback();
  197. MockFrameCallback removedFrameCallback = new MockFrameCallback();
  198. try {
  199. // Add and remove a few callbacks.
  200. long postTimeNanos = System.nanoTime();
  201. mChoreographer.postFrameCallbackDelayed(addedFrameCallback, DELAY_PERIOD);
  202. mChoreographer.postFrameCallbackDelayed(removedFrameCallback, DELAY_PERIOD);
  203. mChoreographer.removeFrameCallback(removedFrameCallback);
  204. // Sleep for a couple of frames.
  205. sleep(NOMINAL_VSYNC_PERIOD * 3);
  206. // The callbacks should not have been invoked yet because of the delay.
  207. assertEquals(0, addedFrameCallback.invocationCount);
  208. assertEquals(0, removedFrameCallback.invocationCount);
  209. // Sleep for the rest of the delay time.
  210. sleep(DELAY_PERIOD);
  211. // We expect the remaining callbacks to have been invoked.
  212. assertEquals(1, addedFrameCallback.invocationCount);
  213. assertEquals(0, removedFrameCallback.invocationCount);
  214. assertTimeDeltaLessThan(addedFrameCallback.frameTimeNanos - postTimeNanos,
  215. (NOMINAL_VSYNC_PERIOD * 3 + DELAY_PERIOD) * NANOS_PER_MS);
  216. } finally {
  217. mChoreographer.removeFrameCallback(addedFrameCallback);
  218. mChoreographer.removeFrameCallback(removedFrameCallback);
  219. }
  220. }
  221. private void assertTimeDeltaLessThan(long deltaNanos, long thresholdNanos) {
  222. if (deltaNanos >= thresholdNanos) {
  223. fail("Expected time delta less than " + thresholdNanos + " nanos, actually "
  224. + " was " + deltaNanos + " nanos.");
  225. }
  226. }
  227. public void testPostFrameCallbackThrowsIfCallbackIsNull() {
  228. try {
  229. mChoreographer.postFrameCallback(null);
  230. fail("Expected IllegalArgumentException");
  231. } catch (IllegalArgumentException ex) {
  232. // expected
  233. }
  234. }
  235. public void testPostFrameCallbackDelayedThrowsIfCallbackIsNull() {
  236. try {
  237. mChoreographer.postFrameCallbackDelayed(null, DELAY_PERIOD);
  238. fail("Expected IllegalArgumentException");
  239. } catch (IllegalArgumentException ex) {
  240. // expected
  241. }
  242. }
  243. public void testRemoveFrameCallbackThrowsIfCallbackIsNull() {
  244. try {
  245. mChoreographer.removeFrameCallback(null);
  246. fail("Expected IllegalArgumentException");
  247. } catch (IllegalArgumentException ex) {
  248. // expected
  249. }
  250. }
  251. private void sleep(long time) {
  252. try {
  253. Thread.sleep(time);
  254. } catch (InterruptedException e) {
  255. fail(e.getMessage());
  256. }
  257. }
  258. private static final class MockRunnable implements Runnable {
  259. public int invocationCount;
  260. @Override
  261. public void run() {
  262. invocationCount += 1;
  263. }
  264. }
  265. private static final class MockFrameCallback implements Choreographer.FrameCallback {
  266. public long frameTimeNanos;
  267. public int invocationCount;
  268. @Override
  269. public void doFrame(long frameTimeNanos) {
  270. this.frameTimeNanos = frameTimeNanos;
  271. invocationCount += 1;
  272. }
  273. }
  274. }