PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/components/paint_preview/player/android/javatests/src/org/chromium/components/paintpreview/player/PaintPreviewPlayerTest.java

http://github.com/chromium/chromium
Java | 218 lines | 164 code | 30 blank | 24 comment | 11 complexity | 1e573dd021a0917b9c78b21575ab0acf MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, BSD-2-Clause, LGPL-2.1, MPL-2.0, 0BSD, EPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BitTorrent-1.0, CPL-1.0, LGPL-3.0, Unlicense, BSD-3-Clause, CC0-1.0, JSON, MIT, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. package org.chromium.components.paintpreview.player;
  5. import android.graphics.Rect;
  6. import android.os.SystemClock;
  7. import android.support.test.InstrumentationRegistry;
  8. import android.support.test.filters.MediumTest;
  9. import android.support.test.uiautomator.UiDevice;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import org.junit.Assert;
  14. import org.junit.Rule;
  15. import org.junit.Test;
  16. import org.junit.runner.RunWith;
  17. import org.chromium.base.task.PostTask;
  18. import org.chromium.base.test.BaseJUnit4ClassRunner;
  19. import org.chromium.base.test.util.CallbackHelper;
  20. import org.chromium.base.test.util.DisabledTest;
  21. import org.chromium.base.test.util.ScalableTimeout;
  22. import org.chromium.base.test.util.UrlUtils;
  23. import org.chromium.content_public.browser.UiThreadTaskTraits;
  24. import org.chromium.content_public.browser.test.util.CriteriaHelper;
  25. import org.chromium.ui.test.util.DummyUiActivityTestCase;
  26. import org.chromium.url.GURL;
  27. /**
  28. * Instrumentation tests for the Paint Preview player.
  29. */
  30. @RunWith(BaseJUnit4ClassRunner.class)
  31. public class PaintPreviewPlayerTest extends DummyUiActivityTestCase {
  32. private static final long TIMEOUT_MS = ScalableTimeout.scaleTimeout(5000);
  33. private static final String TEST_DATA_DIR = "components/test/data/";
  34. private static final String TEST_DIRECTORY_KEY = "wikipedia";
  35. private static final String TEST_URL = "https://en.m.wikipedia.org/wiki/Main_Page";
  36. private static final String TEST_MAIN_PICTURE_LINK_URL =
  37. "https://en.m.wikipedia.org/wiki/File:Volc%C3%A1n_Ubinas,_Arequipa,_Per%C3%BA,_2015-08-02,_DD_50.JPG";
  38. private static final String TEST_IN_VIEWPORT_LINK_URL =
  39. "https://en.m.wikipedia.org/wiki/Arequipa";
  40. private static final String TEST_OUT_OF_VIEWPORT_LINK_URL =
  41. "https://foundation.wikimedia.org/wiki/Privacy_policy";
  42. private static final int TEST_PAGE_HEIGHT = 5019;
  43. @Rule
  44. public PaintPreviewTestRule mPaintPreviewTestRule = new PaintPreviewTestRule();
  45. private PlayerManager mPlayerManager;
  46. private TestLinkClickHandler mLinkClickHandler;
  47. /**
  48. * LinkClickHandler implementation for caching the last URL that was clicked.
  49. */
  50. public class TestLinkClickHandler implements LinkClickHandler {
  51. GURL mUrl;
  52. @Override
  53. public void onLinkClicked(GURL url) {
  54. mUrl = url;
  55. }
  56. }
  57. @Override
  58. public void tearDownTest() throws Exception {
  59. super.tearDownTest();
  60. CallbackHelper destroyed = new CallbackHelper();
  61. PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> {
  62. mPlayerManager.destroy();
  63. destroyed.notifyCalled();
  64. });
  65. destroyed.waitForFirst();
  66. }
  67. /**
  68. * Tests the the player correctly initializes and displays a sample paint preview with 1 frame.
  69. */
  70. @Test
  71. @MediumTest
  72. public void singleFrameDisplayTest() {
  73. initPlayerManager();
  74. final View playerHostView = mPlayerManager.getView();
  75. final View activityContentView = getActivity().findViewById(android.R.id.content);
  76. // Assert that the player view has the same dimensions as the content view.
  77. CriteriaHelper.pollUiThread(
  78. () -> {
  79. boolean contentSizeNonZero = activityContentView.getWidth() > 0
  80. && activityContentView.getHeight() > 0;
  81. boolean viewSizeMatchContent =
  82. activityContentView.getWidth() == playerHostView.getWidth()
  83. && activityContentView.getHeight() == playerHostView.getHeight();
  84. return contentSizeNonZero && viewSizeMatchContent;
  85. },
  86. "Player size doesn't match R.id.content", TIMEOUT_MS,
  87. CriteriaHelper.DEFAULT_POLLING_INTERVAL);
  88. }
  89. /**
  90. * Tests that link clicks in the player work correctly.
  91. */
  92. @Test
  93. @MediumTest
  94. @DisabledTest(message = "crbug.com/1065441")
  95. public void linkClickTest() {
  96. initPlayerManager();
  97. final View playerHostView = mPlayerManager.getView();
  98. // Click on the top left picture and assert it directs to the correct link.
  99. assertLinkUrl(playerHostView, 92, 424, TEST_MAIN_PICTURE_LINK_URL);
  100. assertLinkUrl(playerHostView, 67, 527, TEST_MAIN_PICTURE_LINK_URL);
  101. assertLinkUrl(playerHostView, 466, 668, TEST_MAIN_PICTURE_LINK_URL);
  102. assertLinkUrl(playerHostView, 412, 432, TEST_MAIN_PICTURE_LINK_URL);
  103. // Click on a link that is visible in the default viewport.
  104. assertLinkUrl(playerHostView, 732, 698, TEST_IN_VIEWPORT_LINK_URL);
  105. assertLinkUrl(playerHostView, 876, 716, TEST_IN_VIEWPORT_LINK_URL);
  106. assertLinkUrl(playerHostView, 798, 711, TEST_IN_VIEWPORT_LINK_URL);
  107. // Scroll to the bottom, and click on a link.
  108. scrollToBottom();
  109. int playerHeight = playerHostView.getHeight();
  110. assertLinkUrl(playerHostView, 322, playerHeight - (TEST_PAGE_HEIGHT - 4946),
  111. TEST_OUT_OF_VIEWPORT_LINK_URL);
  112. assertLinkUrl(playerHostView, 376, playerHeight - (TEST_PAGE_HEIGHT - 4954),
  113. TEST_OUT_OF_VIEWPORT_LINK_URL);
  114. assertLinkUrl(playerHostView, 422, playerHeight - (TEST_PAGE_HEIGHT - 4965),
  115. TEST_OUT_OF_VIEWPORT_LINK_URL);
  116. }
  117. /**
  118. * Scrolls to the bottom fo the paint preview.
  119. */
  120. private void scrollToBottom() {
  121. UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
  122. int deviceHeight = uiDevice.getDisplayHeight();
  123. Rect visibleContentRect = new Rect();
  124. getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleContentRect);
  125. int statusBarHeight = visibleContentRect.top;
  126. int navigationBarHeight = 100;
  127. int resourceId = getActivity().getResources().getIdentifier(
  128. "navigation_bar_height", "dimen", "android");
  129. if (resourceId > 0) {
  130. navigationBarHeight = getActivity().getResources().getDimensionPixelSize(resourceId);
  131. }
  132. int padding = 20;
  133. int swipeSteps = 5;
  134. int viewPortBottom = deviceHeight - statusBarHeight - navigationBarHeight;
  135. while (viewPortBottom < TEST_PAGE_HEIGHT) {
  136. int fromY = deviceHeight - navigationBarHeight - padding;
  137. int toY = statusBarHeight + padding;
  138. uiDevice.swipe(50, fromY, 50, toY, swipeSteps);
  139. viewPortBottom += fromY - toY;
  140. }
  141. }
  142. private void initPlayerManager() {
  143. mLinkClickHandler = new TestLinkClickHandler();
  144. PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> {
  145. PaintPreviewTestService service =
  146. new PaintPreviewTestService(UrlUtils.getIsolatedTestFilePath(TEST_DATA_DIR));
  147. mPlayerManager = new PlayerManager(new GURL(TEST_URL), getActivity(), service,
  148. TEST_DIRECTORY_KEY, mLinkClickHandler, Assert::assertTrue, 0xffffffff);
  149. getActivity().setContentView(mPlayerManager.getView());
  150. });
  151. // Wait until PlayerManager is initialized.
  152. CriteriaHelper.pollUiThread(() -> mPlayerManager != null,
  153. "PlayerManager was not initialized.", TIMEOUT_MS,
  154. CriteriaHelper.DEFAULT_POLLING_INTERVAL);
  155. // Assert that the player view is added to the player host view.
  156. CriteriaHelper.pollUiThread(
  157. () -> ((ViewGroup) mPlayerManager.getView()).getChildCount() > 0,
  158. "Player view is not added to the host view.", TIMEOUT_MS,
  159. CriteriaHelper.DEFAULT_POLLING_INTERVAL);
  160. }
  161. private void destroyPlayerManager() {}
  162. private void assertLinkUrl(View view, int x, int y, String expectedUrl) {
  163. mLinkClickHandler.mUrl = null;
  164. dispatchTapEvent(view, x, y);
  165. CriteriaHelper.pollUiThread(
  166. ()-> {
  167. GURL url = mLinkClickHandler.mUrl;
  168. if (url == null) return false;
  169. return url.getSpec().equals(expectedUrl);
  170. },
  171. "Link press on (" + x + ", " + y + ") failed. Expected: " + expectedUrl
  172. + ", found: "
  173. + (mLinkClickHandler.mUrl == null ? null
  174. : mLinkClickHandler.mUrl.getSpec()),
  175. TIMEOUT_MS, CriteriaHelper.DEFAULT_POLLING_INTERVAL);
  176. }
  177. private void dispatchTapEvent(View view, int x, int y) {
  178. long downTime = SystemClock.uptimeMillis();
  179. MotionEvent downEvent = MotionEvent.obtain(
  180. downTime, downTime + 100, MotionEvent.ACTION_DOWN, (float) x, (float) y, 0);
  181. MotionEvent upEvent = MotionEvent.obtain(
  182. downTime + 150, downTime + 200, MotionEvent.ACTION_UP, (float) x, (float) y, 0);
  183. PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> {
  184. view.dispatchTouchEvent(downEvent);
  185. view.dispatchTouchEvent(upEvent);
  186. });
  187. }
  188. }