VideoSupportFragmentTest.java revision dec38f028e4991e352cb45f0e3c1bc85fd9b973d
1// CHECKSTYLE:OFF Generated code 2/* This file is auto-generated from VideoFragmentTest.java. DO NOT MODIFY. */ 3 4/* 5 * Copyright (C) 2016 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19package android.support.v17.leanback.app; 20 21import static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertFalse; 23import static org.junit.Assert.assertNotNull; 24import static org.junit.Assert.assertNotSame; 25import static org.junit.Assert.assertTrue; 26 27import android.net.Uri; 28import android.os.Bundle; 29import android.os.SystemClock; 30import android.support.test.InstrumentationRegistry; 31import android.support.test.filters.MediumTest; 32import android.support.test.runner.AndroidJUnit4; 33import android.support.v17.leanback.media.MediaPlayerGlue; 34import android.support.v17.leanback.media.PlaybackGlue; 35import android.support.v17.leanback.media.PlaybackGlueHost; 36import android.support.v17.leanback.test.R; 37import android.support.v17.leanback.testutils.PollingCheck; 38import android.view.LayoutInflater; 39import android.view.SurfaceHolder; 40import android.view.View; 41import android.view.ViewGroup; 42 43import junit.framework.Assert; 44 45import org.junit.Test; 46import org.junit.runner.RunWith; 47 48@MediumTest 49@RunWith(AndroidJUnit4.class) 50public class VideoSupportFragmentTest extends SingleSupportFragmentTestBase { 51 52 public static class Fragment_setSurfaceViewCallbackBeforeCreate extends VideoSupportFragment { 53 boolean mSurfaceCreated; 54 @Override 55 public View onCreateView( 56 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 57 58 setSurfaceHolderCallback(new SurfaceHolder.Callback() { 59 @Override 60 public void surfaceCreated(SurfaceHolder holder) { 61 mSurfaceCreated = true; 62 } 63 64 @Override 65 public void surfaceChanged(SurfaceHolder holder, int format, int width, 66 int height) { 67 } 68 69 @Override 70 public void surfaceDestroyed(SurfaceHolder holder) { 71 mSurfaceCreated = false; 72 } 73 }); 74 75 return super.onCreateView(inflater, container, savedInstanceState); 76 } 77 } 78 79 @Test 80 public void setSurfaceViewCallbackBeforeCreate() { 81 final SingleSupportFragmentTestActivity activity = 82 launchAndWaitActivity(Fragment_setSurfaceViewCallbackBeforeCreate.class, 1000); 83 Fragment_setSurfaceViewCallbackBeforeCreate fragment1 = 84 (Fragment_setSurfaceViewCallbackBeforeCreate) activity.getTestFragment(); 85 assertNotNull(fragment1); 86 assertTrue(fragment1.mSurfaceCreated); 87 88 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 89 @Override 90 public void run() { 91 activity.getSupportFragmentManager().beginTransaction() 92 .replace(R.id.main_frame, new Fragment_setSurfaceViewCallbackBeforeCreate()) 93 .commitAllowingStateLoss(); 94 } 95 }); 96 SystemClock.sleep(500); 97 98 assertFalse(fragment1.mSurfaceCreated); 99 100 Fragment_setSurfaceViewCallbackBeforeCreate fragment2 = 101 (Fragment_setSurfaceViewCallbackBeforeCreate) activity.getTestFragment(); 102 assertNotNull(fragment2); 103 assertTrue(fragment2.mSurfaceCreated); 104 assertNotSame(fragment1, fragment2); 105 } 106 107 @Test 108 public void setSurfaceViewCallbackAfterCreate() { 109 SingleSupportFragmentTestActivity activity = launchAndWaitActivity(VideoSupportFragment.class, 1000); 110 VideoSupportFragment fragment = (VideoSupportFragment) activity.getTestFragment(); 111 112 assertNotNull(fragment); 113 114 final boolean[] surfaceCreated = new boolean[1]; 115 fragment.setSurfaceHolderCallback(new SurfaceHolder.Callback() { 116 @Override 117 public void surfaceCreated(SurfaceHolder holder) { 118 surfaceCreated[0] = true; 119 } 120 121 @Override 122 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 123 } 124 125 @Override 126 public void surfaceDestroyed(SurfaceHolder holder) { 127 surfaceCreated[0] = false; 128 } 129 }); 130 assertTrue(surfaceCreated[0]); 131 } 132 133 public static class Fragment_withVideoPlayer extends VideoSupportFragment { 134 MediaPlayerGlue mGlue; 135 int mOnCreateCalled; 136 int mOnCreateViewCalled; 137 int mOnDestroyViewCalled; 138 int mOnDestroyCalled; 139 int mGlueAttachedToHost; 140 int mGlueDetachedFromHost; 141 int mGlueOnReadyForPlaybackCalled; 142 143 public Fragment_withVideoPlayer() { 144 setRetainInstance(true); 145 } 146 147 @Override 148 public void onCreate(Bundle savedInstanceState) { 149 mOnCreateCalled++; 150 super.onCreate(savedInstanceState); 151 mGlue = new MediaPlayerGlue(getActivity()) { 152 @Override 153 protected void onDetachedFromHost() { 154 mGlueDetachedFromHost++; 155 super.onDetachedFromHost(); 156 } 157 158 @Override 159 protected void onAttachedToHost(PlaybackGlueHost host) { 160 super.onAttachedToHost(host); 161 mGlueAttachedToHost++; 162 } 163 }; 164 mGlue.setMode(MediaPlayerGlue.REPEAT_ALL); 165 mGlue.setArtist("Leanback"); 166 mGlue.setTitle("Leanback team at work"); 167 mGlue.setMediaSource( 168 Uri.parse("android.resource://android.support.v17.leanback.test/raw/video")); 169 mGlue.setPlayerCallback(new PlaybackGlue.PlayerCallback() { 170 @Override 171 public void onReadyForPlayback() { 172 mGlueOnReadyForPlaybackCalled++; 173 mGlue.play(); 174 } 175 }); 176 mGlue.setHost(new VideoSupportFragmentGlueHost(this)); 177 } 178 179 @Override 180 public View onCreateView(LayoutInflater inflater, ViewGroup container, 181 Bundle savedInstanceState) { 182 mOnCreateViewCalled++; 183 return super.onCreateView(inflater, container, savedInstanceState); 184 } 185 186 @Override 187 public void onDestroyView() { 188 mOnDestroyViewCalled++; 189 super.onDestroyView(); 190 } 191 192 @Override 193 public void onDestroy() { 194 mOnDestroyCalled++; 195 super.onDestroy(); 196 } 197 } 198 199 @Test 200 public void mediaPlayerGlueInVideoSupportFragment() { 201 final SingleSupportFragmentTestActivity activity = 202 launchAndWaitActivity(Fragment_withVideoPlayer.class, 1000); 203 final Fragment_withVideoPlayer fragment = (Fragment_withVideoPlayer) 204 activity.getTestFragment(); 205 206 PollingCheck.waitFor(5000, new PollingCheck.PollingCheckCondition() { 207 @Override 208 public boolean canProceed() { 209 return fragment.mGlue.isMediaPlaying(); 210 } 211 }); 212 213 assertEquals(1, fragment.mOnCreateCalled); 214 assertEquals(1, fragment.mOnCreateViewCalled); 215 assertEquals(0, fragment.mOnDestroyViewCalled); 216 assertEquals(1, fragment.mGlueOnReadyForPlaybackCalled); 217 View fragmentViewBeforeRecreate = fragment.getView(); 218 219 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 220 @Override 221 public void run() { 222 activity.recreate(); 223 } 224 }); 225 226 PollingCheck.waitFor(5000, new PollingCheck.PollingCheckCondition() { 227 @Override 228 public boolean canProceed() { 229 return fragment.mOnCreateViewCalled == 2 && fragment.mGlue.isMediaPlaying(); 230 } 231 }); 232 View fragmentViewAfterRecreate = fragment.getView(); 233 234 Assert.assertNotSame(fragmentViewBeforeRecreate, fragmentViewAfterRecreate); 235 assertEquals(1, fragment.mOnCreateCalled); 236 assertEquals(2, fragment.mOnCreateViewCalled); 237 assertEquals(1, fragment.mOnDestroyViewCalled); 238 239 assertEquals(1, fragment.mGlueAttachedToHost); 240 assertEquals(0, fragment.mGlueDetachedFromHost); 241 assertEquals(1, fragment.mGlueOnReadyForPlaybackCalled); 242 243 activity.finish(); 244 PollingCheck.waitFor(5000, new PollingCheck.PollingCheckCondition() { 245 @Override 246 public boolean canProceed() { 247 return fragment.mGlueDetachedFromHost == 1; 248 } 249 }); 250 assertEquals(2, fragment.mOnDestroyViewCalled); 251 assertEquals(1, fragment.mOnDestroyCalled); 252 } 253 254} 255