1 /*
2  * Copyright (C) 2019 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 
17 
18 package com.android.settings.panel;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import android.content.Context;
23 import android.net.Uri;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import org.robolectric.RobolectricTestRunner;
30 import org.robolectric.RuntimeEnvironment;
31 
32 @RunWith(RobolectricTestRunner.class)
33 public class PanelSlicesLoaderCountdownLatchTest {
34 
35     private Context mContext;
36     private PanelSlicesLoaderCountdownLatch mSliceCountdownLatch;
37 
38     private static final Uri[] URIS = new Uri[] {
39       Uri.parse("content://testUri"),
40       Uri.parse("content://wowUri"),
41       Uri.parse("content://boxTurtle")
42     };
43 
44     @Before
setUp()45     public void setUp() {
46         mContext = RuntimeEnvironment.application;
47         mSliceCountdownLatch = new PanelSlicesLoaderCountdownLatch(URIS.length);
48     }
49 
50 
51     @Test
hasLoaded_newObject_returnsFalse()52     public void hasLoaded_newObject_returnsFalse() {
53         assertThat(mSliceCountdownLatch.isSliceLoaded(URIS[0])).isFalse();
54     }
55 
56     @Test
hasLoaded_markSliceLoaded_returnsTrue()57     public void hasLoaded_markSliceLoaded_returnsTrue() {
58         mSliceCountdownLatch.markSliceLoaded(URIS[0]);
59 
60         assertThat(mSliceCountdownLatch.isSliceLoaded(URIS[0])).isTrue();
61     }
62 
63     @Test
markSliceLoaded_onlyCountsDownUniqueUris()64     public void markSliceLoaded_onlyCountsDownUniqueUris() {
65         for (int i = 0; i < URIS.length; i++) {
66             mSliceCountdownLatch.markSliceLoaded(URIS[0]);
67         }
68 
69         assertThat(mSliceCountdownLatch.isPanelReadyToLoad()).isFalse();
70     }
71 
72     @Test
areSlicesReadyToLoad_allSlicesLoaded_returnsTrue()73     public void areSlicesReadyToLoad_allSlicesLoaded_returnsTrue() {
74         for (int i = 0; i < URIS.length; i++) {
75             mSliceCountdownLatch.markSliceLoaded(URIS[i]);
76         }
77 
78         assertThat(mSliceCountdownLatch.isPanelReadyToLoad()).isTrue();
79     }
80 
81     @Test
areSlicesReadyToLoad_onlyReturnsTrueOnce()82     public void areSlicesReadyToLoad_onlyReturnsTrueOnce() {
83         for (int i = 0; i < URIS.length; i++) {
84             mSliceCountdownLatch.markSliceLoaded(URIS[i]);
85         }
86 
87         // Verify that it returns true once
88         assertThat(mSliceCountdownLatch.isPanelReadyToLoad()).isTrue();
89         // Verify the second call returns false without external state change
90         assertThat(mSliceCountdownLatch.isPanelReadyToLoad()).isFalse();
91     }
92 }