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.server.pm; 18 19 import static android.content.pm.SuspendDialogInfo.BUTTON_ACTION_MORE_DETAILS; 20 import static android.content.pm.SuspendDialogInfo.BUTTON_ACTION_UNSUSPEND; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertNotEquals; 24 import static org.junit.Assert.assertNull; 25 26 import android.content.pm.SuspendDialogInfo; 27 import android.platform.test.annotations.Presubmit; 28 29 import androidx.test.filters.SmallTest; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 @Presubmit 36 @RunWith(AndroidJUnit4.class) 37 @SmallTest 38 public class SuspendDialogInfoTest { 39 private static final int VALID_TEST_RES_ID_1 = 0x11110001; 40 private static final int VALID_TEST_RES_ID_2 = 0x11110002; 41 createDefaultDialogBuilder()42 private static SuspendDialogInfo.Builder createDefaultDialogBuilder() { 43 return new SuspendDialogInfo.Builder() 44 .setIcon(VALID_TEST_RES_ID_1) 45 .setTitle(VALID_TEST_RES_ID_1) 46 .setMessage(VALID_TEST_RES_ID_1) 47 .setNeutralButtonText(VALID_TEST_RES_ID_1) 48 .setNeutralButtonAction(BUTTON_ACTION_MORE_DETAILS); 49 } 50 51 @Test equalsComparesIcons()52 public void equalsComparesIcons() { 53 final SuspendDialogInfo.Builder dialogBuilder1 = createDefaultDialogBuilder(); 54 final SuspendDialogInfo.Builder dialogBuilder2 = createDefaultDialogBuilder(); 55 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 56 // Only icon is different 57 dialogBuilder2.setIcon(VALID_TEST_RES_ID_2); 58 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 59 } 60 61 @Test equalsComparesTitleIds()62 public void equalsComparesTitleIds() { 63 final SuspendDialogInfo.Builder dialogBuilder1 = createDefaultDialogBuilder(); 64 final SuspendDialogInfo.Builder dialogBuilder2 = createDefaultDialogBuilder(); 65 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 66 // Only title is different 67 dialogBuilder2.setTitle(VALID_TEST_RES_ID_2); 68 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 69 } 70 71 @Test equalsIgnoresTitleStringsWhenIdsSet()72 public void equalsIgnoresTitleStringsWhenIdsSet() { 73 final SuspendDialogInfo.Builder dialogBuilder1 = new SuspendDialogInfo.Builder() 74 .setTitle(VALID_TEST_RES_ID_1) 75 .setTitle("1st title"); 76 final SuspendDialogInfo.Builder dialogBuilder2 = new SuspendDialogInfo.Builder() 77 .setTitle(VALID_TEST_RES_ID_1) 78 .setTitle("2nd title"); 79 // String titles different but should get be ignored when resource ids are set 80 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 81 } 82 83 @Test equalsComparesTitleStringsWhenNoIdsSet()84 public void equalsComparesTitleStringsWhenNoIdsSet() { 85 final SuspendDialogInfo.Builder dialogBuilder1 = new SuspendDialogInfo.Builder() 86 .setTitle("1st title"); 87 final SuspendDialogInfo.Builder dialogBuilder2 = new SuspendDialogInfo.Builder() 88 .setTitle("2nd title"); 89 // Both have different titles, which are not ignored as resource ids aren't set 90 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 91 } 92 93 @Test titleStringClearedWhenResIdSet()94 public void titleStringClearedWhenResIdSet() { 95 final SuspendDialogInfo dialogInfo = new SuspendDialogInfo.Builder() 96 .setTitle(VALID_TEST_RES_ID_2) 97 .setTitle("Should be cleared on build") 98 .build(); 99 assertNull(dialogInfo.getTitle()); 100 assertEquals(VALID_TEST_RES_ID_2, dialogInfo.getTitleResId()); 101 } 102 103 @Test equalsComparesButtonTextIds()104 public void equalsComparesButtonTextIds() { 105 final SuspendDialogInfo.Builder dialogBuilder1 = createDefaultDialogBuilder(); 106 final SuspendDialogInfo.Builder dialogBuilder2 = createDefaultDialogBuilder(); 107 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 108 // Only button text is different 109 dialogBuilder2.setNeutralButtonText(VALID_TEST_RES_ID_2); 110 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 111 } 112 113 @Test equalsIgnoresButtonStringsWhenIdsSet()114 public void equalsIgnoresButtonStringsWhenIdsSet() { 115 final SuspendDialogInfo.Builder dialogBuilder1 = new SuspendDialogInfo.Builder() 116 .setNeutralButtonText(VALID_TEST_RES_ID_1) 117 .setNeutralButtonText("1st button text"); 118 final SuspendDialogInfo.Builder dialogBuilder2 = new SuspendDialogInfo.Builder() 119 .setNeutralButtonText(VALID_TEST_RES_ID_1) 120 .setNeutralButtonText("2nd button text"); 121 // Button strings different but should get be ignored when resource ids are set 122 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 123 } 124 125 @Test equalsComparesButtonStringsWhenNoIdsSet()126 public void equalsComparesButtonStringsWhenNoIdsSet() { 127 final SuspendDialogInfo.Builder dialogBuilder1 = new SuspendDialogInfo.Builder() 128 .setNeutralButtonText("1st button text"); 129 final SuspendDialogInfo.Builder dialogBuilder2 = new SuspendDialogInfo.Builder() 130 .setNeutralButtonText("2nd button text"); 131 // Both have different button texts, which are not ignored as resource ids aren't set 132 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 133 } 134 135 @Test buttonStringClearedWhenResIdSet()136 public void buttonStringClearedWhenResIdSet() { 137 final SuspendDialogInfo dialogInfo = new SuspendDialogInfo.Builder() 138 .setNeutralButtonText(VALID_TEST_RES_ID_2) 139 .setNeutralButtonText("Should be cleared on build") 140 .build(); 141 assertNull(dialogInfo.getNeutralButtonText()); 142 assertEquals(VALID_TEST_RES_ID_2, dialogInfo.getNeutralButtonTextResId()); 143 } 144 145 @Test equalsComparesButtonAction()146 public void equalsComparesButtonAction() { 147 final SuspendDialogInfo.Builder dialogBuilder1 = createDefaultDialogBuilder(); 148 final SuspendDialogInfo.Builder dialogBuilder2 = createDefaultDialogBuilder(); 149 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 150 // Only button action is different 151 dialogBuilder2.setNeutralButtonAction(BUTTON_ACTION_UNSUSPEND); 152 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 153 } 154 155 @Test defaultButtonAction()156 public void defaultButtonAction() { 157 final SuspendDialogInfo.Builder dialogBuilder = new SuspendDialogInfo.Builder() 158 .setIcon(VALID_TEST_RES_ID_1) 159 .setTitle(VALID_TEST_RES_ID_1) 160 .setMessage(VALID_TEST_RES_ID_1); 161 assertEquals(BUTTON_ACTION_MORE_DETAILS, dialogBuilder.build().getNeutralButtonAction()); 162 } 163 164 @Test equalsComparesMessageIds()165 public void equalsComparesMessageIds() { 166 final SuspendDialogInfo.Builder dialogBuilder1 = createDefaultDialogBuilder(); 167 final SuspendDialogInfo.Builder dialogBuilder2 = createDefaultDialogBuilder(); 168 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 169 // Only message is different 170 dialogBuilder2.setMessage(VALID_TEST_RES_ID_2); 171 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 172 } 173 174 @Test equalsIgnoresMessageStringsWhenIdsSet()175 public void equalsIgnoresMessageStringsWhenIdsSet() { 176 final SuspendDialogInfo.Builder dialogBuilder1 = new SuspendDialogInfo.Builder() 177 .setMessage(VALID_TEST_RES_ID_1) 178 .setMessage("1st message"); 179 final SuspendDialogInfo.Builder dialogBuilder2 = new SuspendDialogInfo.Builder() 180 .setMessage(VALID_TEST_RES_ID_1) 181 .setMessage("2nd message"); 182 // String messages different but should get be ignored when resource ids are set 183 assertEquals(dialogBuilder1.build(), dialogBuilder2.build()); 184 } 185 186 @Test equalsComparesMessageStringsWhenNoIdsSet()187 public void equalsComparesMessageStringsWhenNoIdsSet() { 188 final SuspendDialogInfo.Builder dialogBuilder1 = new SuspendDialogInfo.Builder() 189 .setMessage("1st message"); 190 final SuspendDialogInfo.Builder dialogBuilder2 = new SuspendDialogInfo.Builder() 191 .setMessage("2nd message"); 192 // Both have different messages, which are not ignored as resource ids aren't set 193 assertNotEquals(dialogBuilder1.build(), dialogBuilder2.build()); 194 } 195 196 @Test messageStringClearedWhenResIdSet()197 public void messageStringClearedWhenResIdSet() { 198 final SuspendDialogInfo dialogInfo = new SuspendDialogInfo.Builder() 199 .setMessage(VALID_TEST_RES_ID_2) 200 .setMessage("Should be cleared on build") 201 .build(); 202 assertNull(dialogInfo.getDialogMessage()); 203 assertEquals(VALID_TEST_RES_ID_2, dialogInfo.getDialogMessageResId()); 204 } 205 } 206