OSDN Git Service

Merge "Added horizontal seperator in Face Settings" into qt-qpr1-dev
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / panel / PanelSlicesAdapterTest.java
1 /*
2  * Copyright (C) 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
17 package com.android.settings.panel;
18
19 import static com.android.settings.panel.PanelSlicesAdapter.MAX_NUM_OF_SLICES;
20 import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_INDICATOR_SLICE_URI;
21
22 import static com.google.common.truth.Truth.assertThat;
23
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
29
30 import android.content.Context;
31 import android.net.Uri;
32 import android.view.ViewGroup;
33 import android.widget.FrameLayout;
34
35 import androidx.lifecycle.LiveData;
36 import androidx.slice.Slice;
37
38 import com.android.settings.R;
39 import com.android.settings.slices.CustomSliceRegistry;
40 import com.android.settings.testutils.FakeFeatureFactory;
41
42 import org.junit.Before;
43 import org.junit.runner.RunWith;
44 import org.junit.Test;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.Robolectric;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.RuntimeEnvironment;
49 import org.robolectric.android.controller.ActivityController;
50
51 import java.util.ArrayList;
52 import java.util.List;
53
54 @RunWith(RobolectricTestRunner.class)
55 public class PanelSlicesAdapterTest {
56
57     private static final Uri DATA_URI = CustomSliceRegistry.DATA_USAGE_SLICE_URI;
58
59     private Context mContext;
60     private PanelFragment mPanelFragment;
61     private PanelFeatureProvider mPanelFeatureProvider;
62     private FakeFeatureFactory mFakeFeatureFactory;
63     private FakePanelContent mFakePanelContent;
64     private List<LiveData<Slice>> mData = new ArrayList<>();
65
66     @Before
67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         mContext = RuntimeEnvironment.application;
70
71         mPanelFeatureProvider = spy(new PanelFeatureProviderImpl());
72         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
73         mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider;
74         mFakePanelContent = new FakePanelContent();
75         doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any(), any());
76
77         ActivityController<FakeSettingsPanelActivity> activityController =
78                 Robolectric.buildActivity(FakeSettingsPanelActivity.class);
79         activityController.setup();
80
81         mPanelFragment =
82                 spy((PanelFragment)
83                         activityController
84                                 .get()
85                                 .getSupportFragmentManager()
86                                 .findFragmentById(R.id.main_content));
87
88     }
89
90     private void addTestLiveData(Uri uri) {
91         // Create a slice to return for the LiveData
92         final Slice slice = spy(new Slice());
93         doReturn(uri).when(slice).getUri();
94         final LiveData<Slice> liveData = mock(LiveData.class);
95         when(liveData.getValue()).thenReturn(slice);
96         mData.add(liveData);
97     }
98
99     @Test
100     public void onCreateViewHolder_returnsSliceRowViewHolder() {
101         addTestLiveData(DATA_URI);
102         final PanelSlicesAdapter adapter =
103                 new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);
104         final ViewGroup view = new FrameLayout(mContext);
105         final PanelSlicesAdapter.SliceRowViewHolder viewHolder =
106                 adapter.onCreateViewHolder(view, 0);
107
108         assertThat(viewHolder.sliceView).isNotNull();
109     }
110
111     @Test
112     public void sizeOfAdapter_shouldNotExceedMaxNum() {
113         for (int i = 0; i < MAX_NUM_OF_SLICES + 2; i++) {
114             addTestLiveData(DATA_URI);
115         }
116
117         assertThat(mData.size()).isEqualTo(MAX_NUM_OF_SLICES + 2);
118
119         final PanelSlicesAdapter adapter =
120                 new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);
121         final ViewGroup view = new FrameLayout(mContext);
122         final PanelSlicesAdapter.SliceRowViewHolder viewHolder =
123                 adapter.onCreateViewHolder(view, 0);
124
125         assertThat(adapter.getItemCount()).isEqualTo(MAX_NUM_OF_SLICES);
126         assertThat(adapter.getData().size()).isEqualTo(MAX_NUM_OF_SLICES);
127     }
128
129     @Test
130     public void nonMediaOutputIndicatorSlice_shouldAllowDividerAboveAndBelow() {
131         addTestLiveData(DATA_URI);
132         final PanelSlicesAdapter adapter =
133                 new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);
134         final int position = 0;
135         final ViewGroup view = new FrameLayout(mContext);
136         final PanelSlicesAdapter.SliceRowViewHolder viewHolder =
137                 adapter.onCreateViewHolder(view, 0 /* view type*/);
138
139         adapter.onBindViewHolder(viewHolder, position);
140
141         assertThat(viewHolder.isDividerAllowedAbove()).isTrue();
142         assertThat(viewHolder.isDividerAllowedBelow()).isTrue();
143     }
144
145     @Test
146     public void mediaOutputIndicatorSlice_shouldNotAllowDividerAbove() {
147         addTestLiveData(MEDIA_OUTPUT_INDICATOR_SLICE_URI);
148
149         final PanelSlicesAdapter adapter =
150                 new PanelSlicesAdapter(mPanelFragment, mData, 0 /* metrics category */);
151         final int position = 0;
152         final ViewGroup view = new FrameLayout(mContext);
153         final PanelSlicesAdapter.SliceRowViewHolder viewHolder =
154                 adapter.onCreateViewHolder(view, 0 /* view type*/);
155
156         adapter.onBindViewHolder(viewHolder, position);
157
158         assertThat(viewHolder.isDividerAllowedAbove()).isFalse();
159     }
160 }