Skip to content

Commit b3b66af

Browse files
Nick PellyAndroid (Google) Code Review
authored andcommitted
Merge "Move nfc-extras tests into frameworks/base/nfc-extras/tests." into ics-mr1
2 parents 539bcca + 3142523 commit b3b66af

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

nfc-extras/tests/Android.mk

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2011, The Android Open Source Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
LOCAL_PATH:= $(call my-dir)
16+
include $(CLEAR_VARS)
17+
18+
# We only want this apk build for tests.
19+
LOCAL_MODULE_TAGS := tests
20+
21+
LOCAL_JAVA_LIBRARIES := \
22+
android.test.runner \
23+
com.android.nfc_extras
24+
25+
# Include all test java files.
26+
LOCAL_SRC_FILES := $(call all-java-files-under, src)
27+
28+
LOCAL_PACKAGE_NAME := NfcExtrasTests
29+
30+
LOCAL_SDK_VERSION := current
31+
32+
include $(BUILD_PACKAGE)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2011 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 name must be unique so suffix with "tests" so package loader doesn't ignore us -->
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.android.nfc_extras.tests">
20+
21+
<!-- We add an application tag here just so that we can indicate that
22+
this package needs to link against the android.test library,
23+
which is needed when building test cases. -->
24+
<application>
25+
<uses-library android:name="android.test.runner" />
26+
<uses-library android:name="com.android.nfc_extras" />
27+
</application>
28+
29+
<uses-permission android:name="android.permission.NFC"/>
30+
31+
<!--
32+
Run all tests with
33+
adb shell am instrument -w com.android.nfc_extras.tests/android.test.InstrumentationTestRunner
34+
-->
35+
<instrumentation android:name="android.test.InstrumentationTestRunner"
36+
android:targetPackage="com.android.nfc_extras.tests"
37+
android:label="Tests for NFC Extras library"/>
38+
39+
</manifest>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (C) 2011 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_extras;
18+
19+
import android.content.Context;
20+
import android.nfc.NfcAdapter;
21+
import android.test.InstrumentationTestCase;
22+
import android.util.Log;
23+
24+
import com.android.nfc_extras.NfcAdapterExtras;
25+
import com.android.nfc_extras.NfcAdapterExtras.CardEmulationRoute;
26+
import com.android.nfc_extras.NfcExecutionEnvironment;
27+
28+
import java.io.IOException;
29+
import java.util.Arrays;
30+
31+
public class BasicNfcEeTest extends InstrumentationTestCase {
32+
private Context mContext;
33+
private NfcAdapterExtras mAdapterExtras;
34+
private NfcExecutionEnvironment mEe;
35+
36+
public static final byte[] SELECT_CARD_MANAGER_COMMAND = new byte[] {
37+
(byte)0x00, (byte)0xA4, (byte)0x04, (byte)0x00, // command
38+
(byte)0x08, // data length
39+
(byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00,
40+
(byte)0x00, // card manager AID
41+
(byte)0x00 // trailer
42+
};
43+
44+
public static final byte[] SELECT_CARD_MANAGER_RESPONSE = new byte[] {
45+
(byte)0x90, (byte)0x00,
46+
};
47+
48+
@Override
49+
protected void setUp() throws Exception {
50+
super.setUp();
51+
mContext = getInstrumentation().getContext();
52+
mAdapterExtras = NfcAdapterExtras.get(NfcAdapter.getDefaultAdapter(mContext));
53+
mEe = mAdapterExtras.getEmbeddedExecutionEnvironment();
54+
}
55+
56+
public void testSendCardManagerApdu() throws IOException {
57+
mEe.open();
58+
59+
try {
60+
byte[] out = mEe.transceive(SELECT_CARD_MANAGER_COMMAND);
61+
assertTrue(out.length >= SELECT_CARD_MANAGER_RESPONSE.length);
62+
byte[] trailing = Arrays.copyOfRange(out,
63+
out.length - SELECT_CARD_MANAGER_RESPONSE.length,
64+
out.length);
65+
assertByteArrayEquals(SELECT_CARD_MANAGER_RESPONSE, trailing);
66+
67+
} finally {
68+
mEe.close();
69+
}
70+
71+
}
72+
73+
public void testSendCardManagerApduMultiple() throws IOException {
74+
for (int i=0; i<10; i++) {
75+
try {
76+
mEe.open();
77+
78+
try {
79+
byte[] out = mEe.transceive(SELECT_CARD_MANAGER_COMMAND);
80+
byte[] trailing = Arrays.copyOfRange(out,
81+
out.length - SELECT_CARD_MANAGER_RESPONSE.length,
82+
out.length);
83+
84+
} finally {
85+
try {Thread.sleep(1000);} catch (InterruptedException e) {}
86+
mEe.close();
87+
}
88+
} catch (IOException e) {}
89+
}
90+
91+
testSendCardManagerApdu();
92+
93+
}
94+
95+
public void testEnableEe() {
96+
mAdapterExtras.setCardEmulationRoute(
97+
new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, mEe));
98+
CardEmulationRoute newRoute = mAdapterExtras.getCardEmulationRoute();
99+
assertEquals(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, newRoute.route);
100+
assertEquals(mEe, newRoute.nfcEe);
101+
}
102+
103+
public void testDisableEe() {
104+
mAdapterExtras.setCardEmulationRoute(
105+
new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null));
106+
CardEmulationRoute newRoute = mAdapterExtras.getCardEmulationRoute();
107+
assertEquals(CardEmulationRoute.ROUTE_OFF, newRoute.route);
108+
assertNull(newRoute.nfcEe);
109+
}
110+
111+
private static void assertByteArrayEquals(byte[] b1, byte[] b2) {
112+
assertEquals(b1.length, b2.length);
113+
for (int i = 0; i < b1.length; i++) {
114+
assertEquals(b1[i], b2[i]);
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)