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 18 package com.android.systemui.util 19 20 import android.content.Context 21 import android.content.DialogInterface 22 import com.android.systemui.statusbar.phone.SystemUIDialog 23 import com.android.systemui.util.mockito.any 24 import com.android.systemui.util.mockito.mock 25 import com.android.systemui.util.mockito.whenever 26 import org.mockito.ArgumentCaptor 27 import org.mockito.Mockito.doAnswer 28 import org.mockito.Mockito.verify 29 import org.mockito.stubbing.Stubber 30 31 class FakeSystemUIDialogController(context: Context) { 32 33 val dialog: SystemUIDialog = mock() 34 35 36 private val clickListeners: MutableMap<Int, DialogInterface.OnClickListener> = mutableMapOf() 37 38 init { 39 whenever(dialog.context).thenReturn(context) 40 saveListener(DialogInterface.BUTTON_POSITIVE) 41 .whenever(dialog) 42 .setPositiveButton(any(), any()) 43 saveListener(DialogInterface.BUTTON_POSITIVE) 44 .whenever(dialog) 45 .setPositiveButton(any(), any(), any()) 46 47 saveListener(DialogInterface.BUTTON_NEGATIVE) 48 .whenever(dialog) 49 .setNegativeButton(any(), any()) 50 saveListener(DialogInterface.BUTTON_NEGATIVE) 51 .whenever(dialog) 52 .setNegativeButton(any(), any(), any()) 53 54 saveListener(DialogInterface.BUTTON_NEUTRAL).whenever(dialog).setNeutralButton(any(), any()) 55 saveListener(DialogInterface.BUTTON_NEUTRAL) 56 .whenever(dialog) 57 .setNeutralButton(any(), any(), any()) 58 } 59 clickNegativenull60 fun clickNegative() { 61 performClick(DialogInterface.BUTTON_NEGATIVE, "This dialog has no negative button") 62 } 63 clickPositivenull64 fun clickPositive() { 65 performClick(DialogInterface.BUTTON_POSITIVE, "This dialog has no positive button") 66 } 67 clickNeutralnull68 fun clickNeutral() { 69 performClick(DialogInterface.BUTTON_NEUTRAL, "This dialog has no neutral button") 70 } 71 cancelnull72 fun cancel() { 73 val captor = ArgumentCaptor.forClass(DialogInterface.OnCancelListener::class.java) 74 verify(dialog).setOnCancelListener(captor.capture()) 75 captor.value.onCancel(dialog) 76 } 77 performClicknull78 private fun performClick(which: Int, errorMessage: String) { 79 clickListeners 80 .getOrElse(which) { throw IllegalAccessException(errorMessage) } 81 .onClick(dialog, which) 82 } 83 <lambda>null84 private fun saveListener(which: Int): Stubber = doAnswer { 85 val listener = it.getArgument<DialogInterface.OnClickListener>(1) 86 clickListeners[which] = listener 87 Unit 88 } 89 } 90