1 /*
2 * Copyright 2013 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 
19 /*
20 * Copyright (C) 2013 The Android Open Source Project
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 *      http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 */
34 package com.example.android.cardreader.tests;
35 
36 import com.example.android.cardreader.*;
37 
38 import android.test.ActivityInstrumentationTestCase2;
39 
40 /**
41 * Tests for CardReader sample.
42 */
43 public class SampleTests extends ActivityInstrumentationTestCase2<MainActivity> {
44 
45     private MainActivity mTestActivity;
46     private CardReaderFragment mTestFragment;
47 
SampleTests()48     public SampleTests() {
49         super(MainActivity.class);
50     }
51 
52     @Override
setUp()53     protected void setUp() throws Exception {
54         super.setUp();
55 
56         // Starts the activity under test using the default Intent with:
57         // action = {@link Intent#ACTION_MAIN}
58         // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
59         // All other fields are null or empty.
60         mTestActivity = getActivity();
61         mTestFragment = (CardReaderFragment)
62             mTestActivity.getSupportFragmentManager().getFragments().get(1);
63     }
64 
65     /**
66     * Test if the test fixture has been set up correctly.
67     */
testPreconditions()68     public void testPreconditions() {
69         //Try to add a message to add context to your assertions. These messages will be shown if
70         //a tests fails and make it easy to understand why a test failed
71         assertNotNull("mTestActivity is null", mTestActivity);
72         assertNotNull("mTestFragment is null", mTestFragment);
73     }
74 
75     /**
76      * Test building SELECT APDU from AID string.
77      */
testBuildSelectApdu()78     public void testBuildSelectApdu() {
79         final String aid = "1234";
80         final byte[] expectedResult = {(byte) 0x00, (byte) 0xA4, 04, (byte) 0x00, (byte) 0x02,
81                 (byte) 0x12, (byte) 0x34};
82         final byte[] result = LoyaltyCardReader.BuildSelectApdu(aid);
83 
84         assertEquals(expectedResult.length, result.length);
85         for (int i = 0; i < expectedResult.length; i++) {
86             assertEquals(expectedResult[i], result[i]);
87         }
88     }
89 
90     /**
91      * Test converting from a hex string to binary.
92      */
testHexToBinary()93     public void testHexToBinary() {
94         final byte[] testData = {(byte) 0xc0, (byte) 0xff, (byte) 0xee};
95         final byte[] output = LoyaltyCardReader.HexStringToByteArray("C0FFEE");
96         for (int i = 0; i < testData.length; i++) {
97             assertEquals(testData[i], output[i]);
98         }
99     }
100 
101     /**
102      * Test converting from binary to a hex string
103      */
testBinaryToHex()104     public void testBinaryToHex() {
105         final byte[] input = {(byte) 0xc0, (byte) 0xff, (byte) 0xee};
106         final String output = LoyaltyCardReader.ByteArrayToHexString(input);
107         assertEquals("C0FFEE", output);
108     }
109 
110 }