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 com.android.nfc.cardemulation;
18 
19 import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
20 import static androidx.test.espresso.Espresso.onView;
21 import static androidx.test.espresso.action.ViewActions.click;
22 import static androidx.test.espresso.assertion.ViewAssertions.matches;
23 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
24 import static androidx.test.espresso.matcher.ViewMatchers.withId;
25 import static com.android.nfc.cardemulation.TapAgainDialog.EXTRA_APDU_SERVICE;
26 import static com.android.nfc.cardemulation.TapAgainDialog.EXTRA_CATEGORY;
27 import static com.google.common.truth.Truth.assertThat;
28 
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.pm.ApplicationInfo;
32 import android.content.pm.PackageManager;
33 import android.content.pm.ResolveInfo;
34 import android.content.pm.ServiceInfo;
35 import android.nfc.cardemulation.ApduServiceInfo;
36 import android.view.Window;
37 
38 import androidx.lifecycle.Lifecycle;
39 import androidx.test.core.app.ActivityScenario;
40 import androidx.test.ext.junit.runners.AndroidJUnit4;
41 import androidx.test.platform.app.InstrumentationRegistry;
42 
43 import com.android.nfc.R;
44 
45 import java.util.ArrayList;
46 
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 
50 @RunWith(AndroidJUnit4.class)
51 public class TapAgainDialogTest {
52   private static final int ALERT_DIALOG_ID = com.android.internal.R.id.parentPanel;
53 
54   @Test
testOnCreate()55   public void testOnCreate() throws Exception {
56     ActivityScenario<TapAgainDialog> scenario = ActivityScenario.launch(getStartIntent());
57 
58     assertThat(scenario.getState()).isAtLeast(Lifecycle.State.CREATED);
59 
60     scenario.onActivity(activity -> {
61       int flags = activity.getWindow().getAttributes().flags;
62       assertThat(flags & FLAG_DISMISS_KEYGUARD).isEqualTo(FLAG_DISMISS_KEYGUARD);
63     });
64   }
65 
66   @Test
testOnClick()67   public void testOnClick() throws Exception {
68     ActivityScenario.launch(getStartIntent());
69 
70     onView(withId(R.id.tap_again_toolbar)).perform(click());
71 
72     onView(withId(ALERT_DIALOG_ID)).check(matches(isDisplayed()));
73     onView(withId(R.id.tap_again_toolbar)).check(matches(isDisplayed()));
74     onView(withId(R.id.tap_again_appicon)).check(matches(isDisplayed()));
75   }
76 
77   @Test
testOnDestroy()78   public void testOnDestroy() throws Exception {
79     ActivityScenario<TapAgainDialog> scenario = ActivityScenario.launch(getStartIntent());
80     scenario.moveToState(Lifecycle.State.DESTROYED);
81 
82     assertThat(scenario.getState()).isEqualTo(Lifecycle.State.DESTROYED);
83   }
84 
85   @Test
testOnStop()86   public void testOnStop() throws Exception {
87     ActivityScenario<TapAgainDialog> scenario = ActivityScenario.launch(getStartIntent());
88 
89     // activity's onPause() and onStop() methods are called
90     scenario.moveToState(Lifecycle.State.CREATED);
91 
92     assertThat(scenario.getState()).isAtLeast(Lifecycle.State.CREATED);
93   }
94 
getStartIntent()95   private Intent getStartIntent() {
96     Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
97     Intent intent = new Intent(context, TapAgainDialog.class);
98     intent.putExtra(EXTRA_CATEGORY, "");
99 
100     ServiceInfo serviceInfo = new ServiceInfo();
101     serviceInfo.packageName = "com.nfc.test";
102     serviceInfo.name = "hce_service";
103     serviceInfo.applicationInfo = new ApplicationInfo();
104     ResolveInfo resolveInfo = new ResolveInfo();
105     resolveInfo.serviceInfo = serviceInfo;
106     ApduServiceInfo service
107         = new ApduServiceInfo(resolveInfo,
108         /* onHost = */ false,
109         /* description = */ "",
110         /* staticAidGroups = */ new ArrayList<>(),
111         /* dynamicAidGroups = */ new ArrayList<>(),
112         /* requiresUnlock = */ false,
113         /* bannerResource = */ 0,
114         /* uid = */ 0,
115         /* settingsActivityName = */ "",
116         /* offHost = */ "",
117         /* staticOffHost = */ "");
118     intent.putExtra(EXTRA_APDU_SERVICE, service);
119     return intent;
120   }
121 }
122