summaryrefslogtreecommitdiff
path: root/tests/tests/nfc/src/android/nfc/cts/NfcPreferredPaymentTest.java
blob: 85810eb3a3f3417a14a7ede630d19d1f284dd0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.nfc.cts;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;

import android.content.pm.PackageManager;
import android.content.ComponentName;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.cardemulation.CardEmulation;
import android.provider.Settings;
import android.util.Log;

import androidx.test.InstrumentationRegistry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class NfcPreferredPaymentTest {
    private final static String mTag = "Nfc";

    private final static String mRouteDestination = "Host";
    private final static String mDescription = "CTS Nfc Test Service";
    private final static String NFC_PAYMENT_DEFAULT_COMPONENT = "nfc_payment_default_component";
    private final static List<String> mAids = Arrays.asList("A000000004101011",
                                                            "A000000004101012",
                                                            "A000000004101013");
    private static final ComponentName CtsNfcTestService =
            new ComponentName("android.nfc.cts", "android.nfc.cts.CtsMyHostApduService");

    private static final int MAX_TIMEOUT_MS = 5000;
    private static final int TEST_DURATION_MS = 100;

    private NfcAdapter mAdapter;
    private CardEmulation mCardEmulation;
    private Context mContext;

    private boolean supportsHardware() {
        final PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
        boolean existAnyReqFeature =
                pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
                || pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF)
                || pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE)
                || pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC);
        return existAnyReqFeature;
    }

    @Before
    public void setUp() throws Exception {
        assumeTrue(supportsHardware());
        mContext = InstrumentationRegistry.getContext();
        mAdapter = NfcAdapter.getDefaultAdapter(mContext);
        assertNotNull(mAdapter);
        mCardEmulation = CardEmulation.getInstance(mAdapter);
        Settings.Secure.putString(mContext.getContentResolver(),
                NFC_PAYMENT_DEFAULT_COMPONENT,
                CtsNfcTestService.flattenToString());
        waitPreferredPaymentSettingDone();
    }

    @After
    public void tearDown() throws Exception {
    }

    /** Tests getAidsForPreferredPaymentService API */
    @Test
    public void testAidsForPreferredPaymentService() {
        try {
            List<String> aids = mCardEmulation.getAidsForPreferredPaymentService();
            for (String aid :aids) {
                Log.i(mTag, "AidsForPreferredPaymentService: " + aid);
            }

            assertTrue("Retrieve incorrect preferred payment aid list", mAids.equals(aids));
        } catch (Exception e) {
            fail("Unexpected Exception " + e);
        }
    }

    /** Tests getRouteDestinationForPreferredPaymentService API */
    @Test
    public void testRouteDestinationForPreferredPaymentService() {
        try {
            String routeDestination =
                    mCardEmulation.getRouteDestinationForPreferredPaymentService();
            Log.i(mTag, "RouteDestinationForPreferredPaymentService: " + routeDestination);

            assertTrue("Retrieve incorrect preferred payment route destination",
                    routeDestination.equals(mRouteDestination));
        } catch (Exception e) {
            fail("Unexpected Exception " + e);
        }
    }

    /** Tests getDescriptionForPreferredPaymentService API */
    @Test
    public void testDescriptionForPreferredPaymentService() {
        try {
            CharSequence description = mCardEmulation.getDescriptionForPreferredPaymentService();
            Log.i(mTag, "DescriptionForPreferredPaymentService: " + description.toString());

            assertTrue("Retrieve incorrect preferred payment description",
                description.toString().equals(mDescription.toString()));
        } catch (Exception e) {
            fail("Unexpected Exception " + e);
        }
    }

    /** Tests getSelectionModeForCategory API
     *  CardEmulation.CATEGORY_PAYMENT */
    @Test
    public void testGetSelectionModeForCategoryPayment() {
        try {
            int mode = mCardEmulation.getSelectionModeForCategory(CardEmulation.CATEGORY_PAYMENT);
            Log.i(mTag, "getSelectionModeForCategory for Payment: " + mode);

            assertTrue("Retrieve incorrect SelectionMode for Payment",
                    CardEmulation.SELECTION_MODE_PREFER_DEFAULT == mode);
        } catch (Exception e) {
            fail("Unexpected Exception " + e);
        }
    }

    /** Tests getSelectionModeForCategory API
     *  CardEmulation.CATEGORY_OTHER */
    @Test
    public void testGetSelectionModeForCategoryOther() {
        try {
            int mode = mCardEmulation.getSelectionModeForCategory(CardEmulation.CATEGORY_OTHER);
            Log.i(mTag, "getSelectionModeForCategory for Other: " + mode);

            assertTrue("Retrieve incorrect SelectionMode for Other",
                    CardEmulation.SELECTION_MODE_ASK_IF_CONFLICT == mode);
        } catch (Exception e) {
            fail("Unexpected Exception " + e);
        }
    }

    public void waitPreferredPaymentSettingDone() {
        try {
            for (int i = 0; i < MAX_TIMEOUT_MS / TEST_DURATION_MS; i++) {
                CharSequence description =
                        mCardEmulation.getDescriptionForPreferredPaymentService();

                if (description != null && description.toString().equals(mDescription)) return;

                msleep(TEST_DURATION_MS);
            }

            fail("Unable to set the preferred payment service");
        } catch (Exception e) {
            fail("Unexpected Exception " + e);
        }
    }

    private void msleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            fail("Unexpected Exception " + e);
        }
    }
}