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 package com.android.emergency.preferences; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.mockito.ArgumentMatchers.eq; 20 import static org.mockito.ArgumentMatchers.same; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.app.Dialog; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.graphics.Bitmap; 31 import android.graphics.drawable.Drawable; 32 import android.os.UserManager; 33 import android.view.View; 34 import android.view.ViewGroup; 35 import android.widget.EditText; 36 import android.widget.ImageView; 37 import android.widget.LinearLayout; 38 import android.widget.TextView; 39 import com.android.emergency.R; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Answers; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.RuntimeEnvironment; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class EmergencyNamePreferenceTest { 51 52 private final Bitmap mBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); 53 @Mock 54 private UserManager mUserManager; 55 @Mock 56 private Dialog mDialog; 57 private Context mContext; 58 private EditText mNameView; 59 private ImageView mIconView; 60 private TestEmergencyNamePreference mTestEmergencyNamePreference; 61 62 @Before setUp()63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mContext = spy(RuntimeEnvironment.application); 66 when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); 67 when(mUserManager.getUserIcon(0)).thenReturn(mBitmap); 68 69 mTestEmergencyNamePreference = spy(new TestEmergencyNamePreference(mContext)); 70 } 71 72 @Test emergencyNamePreference_setDefaultProperties_defaultPropertiesIsSet()73 public void emergencyNamePreference_setDefaultProperties_defaultPropertiesIsSet() { 74 assertThat(mTestEmergencyNamePreference.isEnabled()).isTrue(); 75 assertThat(mTestEmergencyNamePreference.isSelectable()).isTrue(); 76 } 77 78 79 @Test loadDataFromUserManager_setUserNameAndPhoto_preferenceSummaryAndIconIsSet()80 public void loadDataFromUserManager_setUserNameAndPhoto_preferenceSummaryAndIconIsSet() { 81 assertThat(mTestEmergencyNamePreference.getSummary()).isNull(); 82 assertThat(mTestEmergencyNamePreference.getIcon()).isNull(); 83 84 when(mUserManager.getUserName()).thenReturn("Wesley"); 85 mTestEmergencyNamePreference.reloadFromUserManager(); 86 87 assertThat(mTestEmergencyNamePreference.getSummary()).isEqualTo(mUserManager.getUserName()); 88 assertThat(mTestEmergencyNamePreference.getIcon()).isNotNull(); 89 } 90 91 @Test onBindDialogView_setUserName_textViewTitleIsSet()92 public void onBindDialogView_setUserName_textViewTitleIsSet() { 93 View view = createLayout(); 94 when(mUserManager.getUserName()).thenReturn("Wesley"); 95 96 mTestEmergencyNamePreference.onBindDialogView(view); 97 98 TextView textView = view.findViewById(R.id.user_name); 99 assertThat(textView.getText().toString()).isEqualTo(mUserManager.getUserName()); 100 } 101 102 @Test onBindDialogView_setUserPhoto_imageViewDrawableIsSet()103 public void onBindDialogView_setUserPhoto_imageViewDrawableIsSet() { 104 View view = createLayout(); 105 106 mTestEmergencyNamePreference.onBindDialogView(view); 107 108 ImageView imageView = view.findViewById(R.id.user_photo); 109 assertThat(imageView.getDrawable()).isNotNull(); 110 } 111 112 @Test onBindDialogView_setEditUserPhotoController_editUserPhotoControllerIsSet()113 public void onBindDialogView_setEditUserPhotoController_editUserPhotoControllerIsSet() { 114 View view = createLayout(); 115 116 EditUserPhotoController photoController = mTestEmergencyNamePreference.getPhotoController(); 117 assertThat(photoController).isNull(); 118 119 mTestEmergencyNamePreference.onBindDialogView(view); 120 121 photoController = mTestEmergencyNamePreference.getPhotoController(); 122 assertThat(photoController).isNotNull(); 123 } 124 125 @Test onActivityResult_whenWaitingForActivityResult_onActivityResultIsCalled()126 public void onActivityResult_whenWaitingForActivityResult_onActivityResultIsCalled() { 127 View view = createLayout(); 128 129 mTestEmergencyNamePreference.onBindDialogView(view); 130 doReturn(mDialog).when(mTestEmergencyNamePreference).getDialog(); 131 mTestEmergencyNamePreference.startingActivityForResult(); 132 Intent resultData = new Intent(); 133 mTestEmergencyNamePreference.onActivityResult(0, 0, resultData); 134 EditUserPhotoController photoController = mTestEmergencyNamePreference.getPhotoController(); 135 verify(photoController).onActivityResult(eq(0), eq(0), same(resultData)); 136 } 137 createLayout()138 private ViewGroup createLayout() { 139 ViewGroup root = new LinearLayout(mContext); 140 141 mNameView = new EditText(mContext); 142 mNameView.setId(R.id.user_name); 143 root.addView(mNameView); 144 145 mIconView = new ImageView(mContext); 146 mIconView.setId(R.id.user_photo); 147 root.addView(mIconView); 148 149 return root; 150 } 151 152 public class TestEmergencyNamePreference extends EmergencyNamePreference { 153 154 private EditUserPhotoController mPhotoController; 155 TestEmergencyNamePreference(Context context)156 public TestEmergencyNamePreference(Context context) { 157 super(context, null); 158 setSummary(mUserManager.getUserName()); 159 setIcon(null); 160 } 161 getPhotoController()162 public EditUserPhotoController getPhotoController() { 163 return mPhotoController; 164 } 165 166 @Override createEditUserPhotoController(ImageView userPhotoView, Drawable drawable)167 protected EditUserPhotoController createEditUserPhotoController(ImageView userPhotoView, 168 Drawable drawable) { 169 mPhotoController = mock(EditUserPhotoController.class, Answers.RETURNS_DEEP_STUBS); 170 return mPhotoController; 171 } 172 } 173 } 174