Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright 2018 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.fragment.app;
     17 
     18 import static org.mockito.Matchers.any;
     19 import static org.mockito.Mockito.mock;
     20 import static org.mockito.Mockito.never;
     21 import static org.mockito.Mockito.reset;
     22 import static org.mockito.Mockito.verify;
     23 
     24 import android.os.Bundle;
     25 import android.os.SystemClock;
     26 import android.transition.Transition;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 
     31 /**
     32  * A fragment that has transitions that can be tracked.
     33  */
     34 public class TransitionFragment extends StrictViewFragment {
     35     public final TrackingVisibility enterTransition = new TrackingVisibility();
     36     public final TrackingVisibility reenterTransition = new TrackingVisibility();
     37     public final TrackingVisibility exitTransition = new TrackingVisibility();
     38     public final TrackingVisibility returnTransition = new TrackingVisibility();
     39     public final TrackingTransition sharedElementEnter = new TrackingTransition();
     40     public final TrackingTransition sharedElementReturn = new TrackingTransition();
     41 
     42     private Transition.TransitionListener mListener = mock(Transition.TransitionListener.class);
     43 
     44     public TransitionFragment() {
     45         setEnterTransition(enterTransition);
     46         setReenterTransition(reenterTransition);
     47         setExitTransition(exitTransition);
     48         setReturnTransition(returnTransition);
     49         setSharedElementEnterTransition(sharedElementEnter);
     50         setSharedElementReturnTransition(sharedElementReturn);
     51         enterTransition.addListener(mListener);
     52         sharedElementEnter.addListener(mListener);
     53         reenterTransition.addListener(mListener);
     54         exitTransition.addListener(mListener);
     55         returnTransition.addListener(mListener);
     56         sharedElementReturn.addListener(mListener);
     57     }
     58 
     59     @Override
     60     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     61             Bundle savedInstanceState) {
     62         checkGetActivity();
     63         checkState("onCreateView", CREATED);
     64         mOnCreateViewCalled = true;
     65         return super.onCreateView(inflater, container, savedInstanceState);
     66     }
     67 
     68     void waitForTransition() throws InterruptedException {
     69         verify(mListener, CtsMockitoUtils.within(300)).onTransitionEnd((Transition) any());
     70         reset(mListener);
     71     }
     72 
     73     void waitForNoTransition() throws InterruptedException {
     74         SystemClock.sleep(250);
     75         verify(mListener, never()).onTransitionStart((Transition) any());
     76     }
     77 }
     78