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