/leanback/src/androidTest/java/androidx/leanback/media/MediaPlayerGlueTest.java

https://gitlab.com/SkyDragon-OSP/platform_frameworks_support · Java · 142 lines · 106 code · 15 blank · 21 comment · 1 complexity · 37df12819a0e873f5a40bd184ceca545 MD5 · raw file

  1. /*
  2. * Copyright (C) 2017 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 androidx.leanback.media;
  17. import static org.junit.Assert.assertNull;
  18. import static org.mockito.Mockito.atLeastOnce;
  19. import static org.mockito.Mockito.never;
  20. import static org.mockito.Mockito.times;
  21. import android.content.Context;
  22. import android.net.Uri;
  23. import android.os.Build;
  24. import android.os.SystemClock;
  25. import android.support.test.InstrumentationRegistry;
  26. import android.support.test.filters.LargeTest;
  27. import android.support.test.filters.SdkSuppress;
  28. import android.support.test.runner.AndroidJUnit4;
  29. import androidx.leanback.testutils.PollingCheck;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.mockito.Mockito;
  33. @RunWith(AndroidJUnit4.class)
  34. @LargeTest
  35. public class MediaPlayerGlueTest {
  36. /**
  37. * Mockito spy not working on API 19 if class has package private method (b/35387610)
  38. */
  39. @SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
  40. @Test
  41. public void mediaPlayer() {
  42. // create a MediaPlayerGlue with updatePeriod = 100ms
  43. final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
  44. final MediaPlayerGlue[] result = new MediaPlayerGlue[1];
  45. InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
  46. @Override
  47. public void run() {
  48. result[0] = new MediaPlayerGlue(context);
  49. }
  50. });
  51. final MediaPlayerGlue glue = Mockito.spy(result[0]);
  52. Mockito.when(glue.getUpdatePeriod()).thenReturn(100);
  53. final PlaybackGlueHostImpl host = new PlaybackGlueHostImpl();
  54. glue.setHost(host);
  55. glue.setMode(MediaPlayerGlue.REPEAT_ALL);
  56. final boolean[] ready = new boolean[] {false};
  57. glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
  58. @Override
  59. public void onPreparedStateChanged(PlaybackGlue glue) {
  60. if (glue.isPrepared()) {
  61. glue.play();
  62. ready[0] = true;
  63. }
  64. }
  65. });
  66. InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
  67. @Override
  68. public void run() {
  69. glue.setMediaSource(Uri.parse(
  70. "android.resource://androidx.leanback.test/raw/track_01"));
  71. }
  72. });
  73. PollingCheck.waitFor(new PollingCheck.PollingCheckCondition() {
  74. @Override
  75. public boolean canProceed() {
  76. return ready[0];
  77. }
  78. });
  79. // Test setProgressUpdatingEnabled(true) and setProgressUpdatingEnabled(false);
  80. InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
  81. @Override
  82. public void run() {
  83. glue.enableProgressUpdating(true);
  84. }
  85. });
  86. Mockito.reset(glue);
  87. SystemClock.sleep(1000);
  88. Mockito.verify(glue, atLeastOnce()).updateProgress();
  89. InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
  90. @Override
  91. public void run() {
  92. glue.enableProgressUpdating(false);
  93. }
  94. });
  95. Mockito.reset(glue);
  96. SystemClock.sleep(1000);
  97. Mockito.verify(glue, never()).updateProgress();
  98. // Test onStart()/onStop() will pause the updateProgress.
  99. InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
  100. @Override
  101. public void run() {
  102. host.notifyOnStart();
  103. }
  104. });
  105. Mockito.reset(glue);
  106. SystemClock.sleep(1000);
  107. Mockito.verify(glue, atLeastOnce()).updateProgress();
  108. InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
  109. @Override
  110. public void run() {
  111. host.notifyOnStop();
  112. }
  113. });
  114. Mockito.reset(glue);
  115. SystemClock.sleep(1000);
  116. Mockito.verify(glue, never()).updateProgress();
  117. InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
  118. @Override
  119. public void run() {
  120. host.notifyOnDestroy();
  121. }
  122. });
  123. assertNull(glue.getHost());
  124. Mockito.verify(glue, times(1)).onDetachedFromHost();
  125. Mockito.verify(glue, times(1)).release();
  126. }
  127. }