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 package com.android.settings.slices; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 23 import android.content.Context; 24 import android.net.Uri; 25 import android.view.View; 26 27 import androidx.slice.Slice; 28 import androidx.slice.widget.SliceView; 29 30 import com.android.settings.R; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.Robolectric; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class SlicePreferenceTest { 42 43 private SlicePreference mSlicePreference; 44 private Context mContext; 45 46 @Before setUp()47 public void setUp() { 48 MockitoAnnotations.initMocks(this); 49 mContext = spy(RuntimeEnvironment.application); 50 51 mSlicePreference = new SlicePreference(mContext, Robolectric.buildAttributeSet() 52 .setStyleAttribute("@style/SlicePreference") 53 .build()); 54 } 55 56 @Test onSliceUpdated_null_hideSliceView()57 public void onSliceUpdated_null_hideSliceView() { 58 final SliceView sliceView = mSlicePreference.findViewById(R.id.slice_view); 59 60 mSlicePreference.onSliceUpdated(null); 61 62 assertThat(sliceView.getVisibility()).isEqualTo(View.GONE); 63 } 64 65 @Test onSliceUpdated_notNull_showSliceView()66 public void onSliceUpdated_notNull_showSliceView() { 67 final SliceView sliceView = mSlicePreference.findViewById(R.id.slice_view); 68 69 mSlicePreference.onSliceUpdated(new Slice.Builder(Uri.parse("uri")).build()); 70 71 assertThat(sliceView.getVisibility()).isEqualTo(View.VISIBLE); 72 } 73 }