ProgressBarManagerTest.java revision 1853eab01f421268e2a1de76b660b5577260ce5d
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 */
16package android.support.v17.leanback.app;
17
18import static org.junit.Assert.assertSame;
19
20import android.content.Context;
21import android.support.test.InstrumentationRegistry;
22import android.support.test.filters.SmallTest;
23import android.support.v17.leanback.testutils.PollingCheck;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.FrameLayout;
27
28import org.junit.Before;
29import org.junit.Test;
30
31@SmallTest
32public class ProgressBarManagerTest {
33
34    Context mContext;
35    ProgressBarManager mProgressBarManager;
36    long mWaitShownTimeOutMs;
37    long mWaitHideTimeOutMs;
38
39    @Before
40    public void setUp() {
41        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
42        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
43            @Override
44            public void run() {
45                mProgressBarManager = new ProgressBarManager();
46            }
47        });
48        mWaitShownTimeOutMs = Math.max(2000, mProgressBarManager.getInitialDelay() * 3);
49        mWaitHideTimeOutMs = 2000;
50    }
51
52    @Test
53    public void defaultProgressBarView() {
54        final ViewGroup rootView = new FrameLayout(mContext);
55        mProgressBarManager.setRootView(rootView);
56        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
57            @Override
58            public void run() {
59                mProgressBarManager.show();
60            }
61        });
62        PollingCheck.waitFor(mWaitShownTimeOutMs,
63                new PollingCheck.PollingCheckCondition() {
64                    @Override
65                    public boolean canProceed() {
66                        if (rootView.getChildCount() == 0) return false;
67                        return  rootView.getChildAt(0).getVisibility() == View.VISIBLE;
68                    }
69                });
70        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
71            @Override
72            public void run() {
73                mProgressBarManager.hide();
74            }
75        });
76        PollingCheck.waitFor(mWaitHideTimeOutMs,
77                new PollingCheck.PollingCheckCondition() {
78                    @Override
79                    public boolean canProceed() {
80                        return rootView.getChildCount() == 0;
81                    }
82                });
83        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
84            @Override
85            public void run() {
86                mProgressBarManager.show();
87            }
88        });
89        PollingCheck.waitFor(mWaitShownTimeOutMs,
90                new PollingCheck.PollingCheckCondition() {
91                    @Override
92                    public boolean canProceed() {
93                        if (rootView.getChildCount() == 0) return false;
94                        return  rootView.getChildAt(0).getVisibility() == View.VISIBLE;
95                    }
96                });
97    }
98
99    @Test
100    public void customProgressBarView() {
101        final ViewGroup rootView = new FrameLayout(mContext);
102        View customProgressBar = new View(mContext);
103        rootView.addView(customProgressBar, 100, 100);
104        mProgressBarManager.setProgressBarView(customProgressBar);
105        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
106            @Override
107            public void run() {
108                mProgressBarManager.show();
109            }
110        });
111        PollingCheck.waitFor(mWaitShownTimeOutMs,
112                new PollingCheck.PollingCheckCondition() {
113                    @Override
114                    public boolean canProceed() {
115                        if (rootView.getChildCount() == 0) return false;
116                        return  rootView.getChildAt(0).getVisibility() == View.VISIBLE;
117                    }
118                });
119        assertSame(customProgressBar, rootView.getChildAt(0));
120        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
121            @Override
122            public void run() {
123                mProgressBarManager.hide();
124            }
125        });
126        PollingCheck.waitFor(mWaitHideTimeOutMs,
127                new PollingCheck.PollingCheckCondition() {
128                    @Override
129                    public boolean canProceed() {
130                        return  rootView.getChildAt(0).getVisibility() != View.VISIBLE;
131                    }
132                });
133        assertSame(customProgressBar, rootView.getChildAt(0));
134        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
135            @Override
136            public void run() {
137                mProgressBarManager.show();
138            }
139        });
140        PollingCheck.waitFor(mWaitShownTimeOutMs,
141                new PollingCheck.PollingCheckCondition() {
142                    @Override
143                    public boolean canProceed() {
144                        if (rootView.getChildCount() == 0) return false;
145                        return  rootView.getChildAt(0).getVisibility() == View.VISIBLE;
146                    }
147                });
148    }
149}
150