2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.panel;
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;
22 import static com.google.common.truth.Truth.assertThat;
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;
30 import android.content.Context;
31 import android.net.Uri;
32 import android.view.ViewGroup;
33 import android.widget.FrameLayout;
35 import androidx.lifecycle.LiveData;
36 import androidx.slice.Slice;
38 import com.android.settings.R;
39 import com.android.settings.slices.CustomSliceRegistry;
40 import com.android.settings.testutils.FakeFeatureFactory;
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;
51 import java.util.ArrayList;
52 import java.util.List;
54 @RunWith(RobolectricTestRunner.class)
55 public class PanelSlicesAdapterTest {
57 private static final Uri DATA_URI = CustomSliceRegistry.DATA_USAGE_SLICE_URI;
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<>();
68 MockitoAnnotations.initMocks(this);
69 mContext = RuntimeEnvironment.application;
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());
77 ActivityController<FakeSettingsPanelActivity> activityController =
78 Robolectric.buildActivity(FakeSettingsPanelActivity.class);
79 activityController.setup();
85 .getSupportFragmentManager()
86 .findFragmentById(R.id.main_content));
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);
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);
108 assertThat(viewHolder.sliceView).isNotNull();
112 public void sizeOfAdapter_shouldNotExceedMaxNum() {
113 for (int i = 0; i < MAX_NUM_OF_SLICES + 2; i++) {
114 addTestLiveData(DATA_URI);
117 assertThat(mData.size()).isEqualTo(MAX_NUM_OF_SLICES + 2);
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);
125 assertThat(adapter.getItemCount()).isEqualTo(MAX_NUM_OF_SLICES);
126 assertThat(adapter.getData().size()).isEqualTo(MAX_NUM_OF_SLICES);
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*/);
139 adapter.onBindViewHolder(viewHolder, position);
141 assertThat(viewHolder.isDividerAllowedAbove()).isTrue();
142 assertThat(viewHolder.isDividerAllowedBelow()).isTrue();
146 public void mediaOutputIndicatorSlice_shouldNotAllowDividerAbove() {
147 addTestLiveData(MEDIA_OUTPUT_INDICATOR_SLICE_URI);
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*/);
156 adapter.onBindViewHolder(viewHolder, position);
158 assertThat(viewHolder.isDividerAllowedAbove()).isFalse();