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