• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.testutils.shadow;
18 
19 import android.annotation.SuppressLint;
20 import android.view.View;
21 
22 import androidx.appcompat.app.AlertDialog;
23 
24 import org.robolectric.Shadows;
25 import org.robolectric.annotation.Implementation;
26 import org.robolectric.annotation.Implements;
27 import org.robolectric.annotation.RealObject;
28 import org.robolectric.annotation.Resetter;
29 import org.robolectric.shadow.api.Shadow;
30 import org.robolectric.shadows.ShadowDialog;
31 import org.robolectric.util.ReflectionHelpers;
32 
33 import javax.annotation.Nullable;
34 
35 /* Robolectric shadow for the androidx alert dialog. */
36 @Implements(AlertDialog.class)
37 public class ShadowAlertDialogCompat extends ShadowDialog {
38 
39     @SuppressLint("StaticFieldLeak")
40     @Nullable
41     private static ShadowAlertDialogCompat latestSupportAlertDialog;
42     @RealObject
43     private AlertDialog realAlertDialog;
44 
45     @Implementation
show()46     public void show() {
47         super.show();
48         latestSupportAlertDialog = this;
49     }
50 
getMessage()51     public CharSequence getMessage() {
52         final Object alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert");
53         return ReflectionHelpers.getField(alertController, "mMessage");
54     }
55 
getTitle()56     public CharSequence getTitle() {
57         final Object alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert");
58         return ReflectionHelpers.getField(alertController, "mTitle");
59     }
60 
getView()61     public View getView() {
62         final Object alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert");
63         return ReflectionHelpers.getField(alertController, "mView");
64     }
65 
66     @Nullable
getLatestAlertDialog()67     public static AlertDialog getLatestAlertDialog() {
68         return latestSupportAlertDialog == null ? null : latestSupportAlertDialog.realAlertDialog;
69     }
70 
71     @Resetter
reset()72     public static void reset() {
73         latestSupportAlertDialog = null;
74     }
75 
shadowOf(AlertDialog alertDialog)76     public static ShadowAlertDialogCompat shadowOf(AlertDialog alertDialog) {
77         return (ShadowAlertDialogCompat) Shadow.extract(alertDialog);
78     }
79 
clickOnItem(int index)80     public void clickOnItem(int index) {
81         Shadows.shadowOf(realAlertDialog.getListView()).performItemClick(index);
82     }
83 }