1 /* 2 * Copyright (C) 2021 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.car.qc.view; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.verify; 25 import static org.testng.Assert.assertThrows; 26 27 import android.app.PendingIntent; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.graphics.drawable.Drawable; 31 import android.graphics.drawable.Icon; 32 import android.graphics.drawable.LayerDrawable; 33 import android.widget.TextView; 34 35 import androidx.test.annotation.UiThreadTest; 36 import androidx.test.core.app.ApplicationProvider; 37 import androidx.test.ext.junit.runners.AndroidJUnit4; 38 39 import com.android.car.qc.QCRow; 40 import com.android.car.qc.QCTile; 41 import com.android.car.qc.R; 42 import com.android.car.ui.uxr.DrawableStateToggleButton; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 48 @RunWith(AndroidJUnit4.class) 49 public class QCTileViewTest { 50 51 private final Context mContext = ApplicationProvider.getApplicationContext(); 52 private QCTileView mView; 53 54 @Before setUp()55 public void setUp() { 56 mView = new QCTileView(mContext); 57 } 58 59 @Test onChanged_null_noViews()60 public void onChanged_null_noViews() { 61 mView.onChanged(null); 62 assertThat(mView.getChildCount()).isEqualTo(0); 63 } 64 65 @Test onChanged_invalidType_throwsIllegalArgumentException()66 public void onChanged_invalidType_throwsIllegalArgumentException() { 67 QCRow row = new QCRow.Builder().build(); 68 assertThrows(IllegalArgumentException.class, 69 () -> mView.onChanged(row)); 70 } 71 72 @Test 73 @UiThreadTest onChanged_setsSubtitleView()74 public void onChanged_setsSubtitleView() { 75 String subtitle = "TEST_SUBTITLE"; 76 QCTile tile = new QCTile.Builder().setSubtitle(subtitle).build(); 77 mView.onChanged(tile); 78 TextView subtitleView = mView.findViewById(android.R.id.summary); 79 assertThat(subtitleView.getText().toString()).isEqualTo(subtitle); 80 } 81 82 @Test 83 @UiThreadTest onChanged_setsButtonState()84 public void onChanged_setsButtonState() { 85 QCTile tile = new QCTile.Builder().setChecked(true).setEnabled(true).build(); 86 mView.onChanged(tile); 87 DrawableStateToggleButton button = mView.findViewById(R.id.qc_tile_toggle_button); 88 assertThat(button.isEnabled()).isTrue(); 89 assertThat(button.isChecked()).isTrue(); 90 } 91 92 @Test 93 @UiThreadTest onChanged_setsIcon()94 public void onChanged_setsIcon() { 95 Icon icon = Icon.createWithResource(mContext, android.R.drawable.btn_star); 96 QCTile tile = new QCTile.Builder().setIcon(icon).build(); 97 mView.onChanged(tile); 98 DrawableStateToggleButton button = mView.findViewById(R.id.qc_tile_toggle_button); 99 Drawable buttonDrawable = button.getButtonDrawable(); 100 assertThat(buttonDrawable).isNotNull(); 101 assertThat(buttonDrawable instanceof LayerDrawable).isTrue(); 102 assertThat(((LayerDrawable) buttonDrawable).getNumberOfLayers()).isEqualTo(2); 103 } 104 105 @Test 106 @UiThreadTest onClick_firesAction()107 public void onClick_firesAction() throws PendingIntent.CanceledException { 108 PendingIntent action = mock(PendingIntent.class); 109 QCTile tile = new QCTile.Builder().setChecked(false).setAction(action).build(); 110 mView.onChanged(tile); 111 mView.findViewById(R.id.qc_tile_wrapper).performClick(); 112 DrawableStateToggleButton button = mView.findViewById(R.id.qc_tile_toggle_button); 113 assertThat(button.isChecked()).isTrue(); 114 verify(action).send(any(Context.class), anyInt(), any(Intent.class)); 115 } 116 } 117