1 /* 2 * Copyright (C) 2024 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 android.platform.systemui_tapl.volume.panel.rule 18 19 import android.media.AudioManager 20 import android.platform.systemui_tapl.ui.Root.Companion.get 21 import android.platform.systemui_tapl.volume.panel.ui.VolumePanel 22 import org.junit.rules.TestWatcher 23 import org.junit.runner.Description 24 25 /** 26 * Rule that opens the Volume Panel for the duration of the test and ensures it's closed afterwards. 27 */ 28 class VolumePanelRule(private val mAudioManager: AudioManager) : TestWatcher() { 29 30 /** Returns [VolumePanel] for easy access the opened panel. */ 31 val volumePanel: VolumePanel <lambda>null32 get() = checkNotNull(mutableVolumePanel) { "Volume Panel hasn't been opened yet" } 33 34 private var mutableVolumePanel: VolumePanel? = null 35 startingnull36 override fun starting(description: Description) { 37 super.starting(description) 38 // Open the volume dialog 39 mAudioManager.adjustSuggestedStreamVolume( 40 AudioManager.ADJUST_SAME, 41 AudioManager.STREAM_MUSIC, 42 AudioManager.FLAG_SHOW_UI, 43 ) 44 mutableVolumePanel = get().volumeDialog.openNewVolumePanel() 45 } 46 finishednull47 override fun finished(description: Description) { 48 mutableVolumePanel?.done() 49 super.finished(description) 50 } 51 } 52