1 /* 2 * Copyright (C) 2023 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.notification; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.os.Parcel; 24 import android.platform.test.flag.junit.SetFlagsRule; 25 import android.service.notification.ZenDeviceEffects; 26 27 import androidx.test.runner.AndroidJUnit4; 28 29 import com.android.server.UiServiceTestCase; 30 31 import com.google.common.collect.ImmutableSet; 32 33 import org.junit.Rule; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @RunWith(AndroidJUnit4.class) 38 public class ZenDeviceEffectsTest extends UiServiceTestCase { 39 40 @Rule 41 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 42 43 @Test builder()44 public void builder() { 45 ZenDeviceEffects deviceEffects = 46 new ZenDeviceEffects.Builder() 47 .setShouldDimWallpaper(true) 48 .setShouldDisableTapToWake(true) 49 .setShouldDisableTapToWake(false) 50 .setShouldDisableTiltToWake(true) 51 .setShouldMaximizeDoze(true) 52 .setShouldUseNightMode(false) 53 .setShouldUseNightLight(true) 54 .setShouldSuppressAmbientDisplay(false) 55 .setShouldSuppressAmbientDisplay(true) 56 .addExtraEffect("WILL BE GONE") 57 .setExtraEffects(ImmutableSet.of("1", "2")) 58 .addExtraEffects(ImmutableSet.of("3", "4")) 59 .addExtraEffect("5") 60 .build(); 61 62 assertThat(deviceEffects.shouldDimWallpaper()).isTrue(); 63 assertThat(deviceEffects.shouldDisableAutoBrightness()).isFalse(); 64 assertThat(deviceEffects.shouldDisableTapToWake()).isFalse(); 65 assertThat(deviceEffects.shouldDisableTiltToWake()).isTrue(); 66 assertThat(deviceEffects.shouldDisableTouch()).isFalse(); 67 assertThat(deviceEffects.shouldDisplayGrayscale()).isFalse(); 68 assertThat(deviceEffects.shouldUseNightLight()).isTrue(); 69 assertThat(deviceEffects.shouldMaximizeDoze()).isTrue(); 70 assertThat(deviceEffects.shouldMinimizeRadioUsage()).isFalse(); 71 assertThat(deviceEffects.shouldUseNightMode()).isFalse(); 72 assertThat(deviceEffects.shouldSuppressAmbientDisplay()).isTrue(); 73 assertThat(deviceEffects.getExtraEffects()).containsExactly("1", "2", "3", "4", "5"); 74 } 75 76 @Test builder_fromInstance()77 public void builder_fromInstance() { 78 ZenDeviceEffects original = new ZenDeviceEffects.Builder() 79 .setShouldDimWallpaper(true) 80 .setShouldDisableTiltToWake(true) 81 .setShouldUseNightMode(true) 82 .setShouldSuppressAmbientDisplay(true) 83 .addExtraEffect("1") 84 .build(); 85 86 ZenDeviceEffects modified = 87 new ZenDeviceEffects.Builder(original) 88 .setShouldDisplayGrayscale(true) 89 .setShouldUseNightMode(false) 90 .setShouldUseNightLight(true) 91 .addExtraEffect("2") 92 .build(); 93 94 assertThat(modified.shouldDimWallpaper()).isTrue(); // from original 95 assertThat(modified.shouldDisableTiltToWake()).isTrue(); // from original 96 assertThat(modified.shouldDisplayGrayscale()).isTrue(); // updated 97 assertThat(modified.shouldUseNightLight()).isTrue(); // updated 98 assertThat(modified.shouldUseNightMode()).isFalse(); // updated 99 assertThat(modified.shouldSuppressAmbientDisplay()).isTrue(); // from original 100 assertThat(modified.getExtraEffects()).containsExactly("1", "2"); // updated 101 } 102 103 @Test builder_add_merges()104 public void builder_add_merges() { 105 ZenDeviceEffects zde1 = new ZenDeviceEffects.Builder() 106 .setShouldDimWallpaper(true) 107 .addExtraEffect("one") 108 .build(); 109 ZenDeviceEffects zde2 = new ZenDeviceEffects.Builder() 110 .setShouldDisableTouch(true) 111 .addExtraEffect("two") 112 .build(); 113 ZenDeviceEffects zde3 = new ZenDeviceEffects.Builder() 114 .setShouldMinimizeRadioUsage(true) 115 .addExtraEffect("three") 116 .build(); 117 118 ZenDeviceEffects add = new ZenDeviceEffects.Builder().add(zde1).add(zde2).add(zde3).build(); 119 120 assertThat(add).isEqualTo(new ZenDeviceEffects.Builder() 121 .setShouldDimWallpaper(true) 122 .setShouldDisableTouch(true) 123 .setShouldMinimizeRadioUsage(true) 124 .setExtraEffects(ImmutableSet.of("one", "two", "three")) 125 .build()); 126 } 127 128 @Test writeToParcel_parcelsAndUnparcels()129 public void writeToParcel_parcelsAndUnparcels() { 130 ZenDeviceEffects source = new ZenDeviceEffects.Builder() 131 .setShouldDimWallpaper(true) 132 .setShouldDisableTouch(true) 133 .setShouldMinimizeRadioUsage(true) 134 .setShouldUseNightMode(true) 135 .setShouldSuppressAmbientDisplay(true) 136 .setExtraEffects(ImmutableSet.of("1", "2", "3")) 137 .build(); 138 139 Parcel parcel = Parcel.obtain(); 140 ZenDeviceEffects copy; 141 try { 142 source.writeToParcel(parcel, 0); 143 parcel.setDataPosition(0); 144 copy = ZenDeviceEffects.CREATOR.createFromParcel(parcel); 145 } finally { 146 parcel.recycle(); 147 } 148 149 assertThat(copy.shouldDimWallpaper()).isTrue(); 150 assertThat(copy.shouldDisableTouch()).isTrue(); 151 assertThat(copy.shouldMinimizeRadioUsage()).isTrue(); 152 assertThat(copy.shouldUseNightMode()).isTrue(); 153 assertThat(copy.shouldSuppressAmbientDisplay()).isTrue(); 154 assertThat(copy.shouldDisplayGrayscale()).isFalse(); 155 assertThat(copy.getExtraEffects()).containsExactly("1", "2", "3"); 156 } 157 158 @Test hasEffects_none_returnsFalse()159 public void hasEffects_none_returnsFalse() { 160 ZenDeviceEffects effects = new ZenDeviceEffects.Builder().build(); 161 assertThat(effects.hasEffects()).isFalse(); 162 } 163 164 @Test hasEffects_some_returnsTrue()165 public void hasEffects_some_returnsTrue() { 166 ZenDeviceEffects effects = new ZenDeviceEffects.Builder() 167 .setShouldDimWallpaper(true) 168 .build(); 169 assertThat(effects.hasEffects()).isTrue(); 170 } 171 172 @Test hasEffects_extras_returnsTrue()173 public void hasEffects_extras_returnsTrue() { 174 ZenDeviceEffects effects = new ZenDeviceEffects.Builder() 175 .addExtraEffect("extra") 176 .build(); 177 assertThat(effects.hasEffects()).isTrue(); 178 } 179 180 @Test validate_extrasLength()181 public void validate_extrasLength() { 182 ZenDeviceEffects okay = new ZenDeviceEffects.Builder() 183 .addExtraEffect("short") 184 .addExtraEffect("anotherShort") 185 .build(); 186 187 ZenDeviceEffects pushingIt = new ZenDeviceEffects.Builder() 188 .addExtraEffect("0123456789".repeat(60)) 189 .addExtraEffect("1234567890".repeat(60)) 190 .build(); 191 192 ZenDeviceEffects excessive = new ZenDeviceEffects.Builder() 193 .addExtraEffect("0123456789".repeat(60)) 194 .addExtraEffect("1234567890".repeat(60)) 195 .addExtraEffect("2345678901".repeat(60)) 196 .addExtraEffect("3456789012".repeat(30)) 197 .build(); 198 199 okay.validate(); // No exception. 200 pushingIt.validate(); // No exception. 201 assertThrows(Exception.class, () -> excessive.validate()); 202 } 203 } 204