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.addPlayerCallback(new PlaybackGlue.PlayerCallback() { 170 @Override 171 public void onPreparedStateChanged(PlaybackGlue glue) { 172 if (glue.isPrepared()) { 173 mGlueOnReadyForPlaybackCalled++; 174 mGlue.play(); 175 } 176 } 177 }); 178 mGlue.setHost(new VideoSupportFragmentGlueHost(this)); 179 } 180 181 @Override 182 public View onCreateView(LayoutInflater inflater, ViewGroup container, 183 Bundle savedInstanceState) { 184 mOnCreateViewCalled++; 185 return super.onCreateView(inflater, container, savedInstanceState); 186 } 187 188 @Override 189 public void onDestroyView() { 190 mOnDestroyViewCalled++; 191 super.onDestroyView(); 192 } 193 194 @Override 195 public void onDestroy() { 196 mOnDestroyCalled++; 197 super.onDestroy(); 198 } 199 } 200 201 @Test 202 public void mediaPlayerGlueInVideoSupportFragment() { 203 final SingleSupportFragmentTestActivity activity = 204 launchAndWaitActivity(Fragment_withVideoPlayer.class, 1000); 205 final Fragment_withVideoPlayer fragment = (Fragment_withVideoPlayer) 206 activity.getTestFragment(); 207 208 PollingCheck.waitFor(5000, new PollingCheck.PollingCheckCondition() { 209 @Override 210 public boolean canProceed() { 211 return fragment.mGlue.isMediaPlaying(); 212 } 213 }); 214 215 assertEquals(1, fragment.mOnCreateCalled); 216 assertEquals(1, fragment.mOnCreateViewCalled); 217 assertEquals(0, fragment.mOnDestroyViewCalled); 218 assertEquals(1, fragment.mGlueOnReadyForPlaybackCalled); 219 View fragmentViewBeforeRecreate = fragment.getView(); 220 221 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 222 @Override 223 public void run() { 224 activity.recreate(); 225 } 226 }); 227 228 PollingCheck.waitFor(5000, new PollingCheck.PollingCheckCondition() { 229 @Override 230 public boolean canProceed() { 231 return fragment.mOnCreateViewCalled == 2 && fragment.mGlue.isMediaPlaying(); 232 } 233 }); 234 View fragmentViewAfterRecreate = fragment.getView(); 235 236 Assert.assertNotSame(fragmentViewBeforeRecreate, fragmentViewAfterRecreate); 237 assertEquals(1, fragment.mOnCreateCalled); 238 assertEquals(2, fragment.mOnCreateViewCalled); 239 assertEquals(1, fragment.mOnDestroyViewCalled); 240 241 assertEquals(1, fragment.mGlueAttachedToHost); 242 assertEquals(0, fragment.mGlueDetachedFromHost); 243 assertEquals(1, fragment.mGlueOnReadyForPlaybackCalled); 244 245 activity.finish(); 246 PollingCheck.waitFor(5000, new PollingCheck.PollingCheckCondition() { 247 @Override 248 public boolean canProceed() { 249 return fragment.mGlueDetachedFromHost == 1; 250 } 251 }); 252 assertEquals(2, fragment.mOnDestroyViewCalled); 253 assertEquals(1, fragment.mOnDestroyCalled); 254 } 255 256} 257