• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 android.nfc.cts;
18 
19 import static org.junit.Assert.assertThrows;
20 
21 import android.content.pm.PackageManager;
22 import android.nfc.NfcServiceManager;
23 import android.nfc.NfcServiceManager.ServiceNotFoundException;
24 import android.nfc.NfcServiceManager.ServiceRegisterer;
25 import android.os.IBinder;
26 import android.os.ServiceManager;
27 import android.test.AndroidTestCase;
28 
29 public class NfcServiceManagerTest extends AndroidTestCase {
30 
31     private boolean mHasNfc;
32 
33     @Override
setUp()34     public void setUp() throws Exception {
35         super.setUp();
36         mHasNfc = getContext().getPackageManager().hasSystemFeature(
37                 PackageManager.FEATURE_NFC);
38     }
39 
test_ServiceRegisterer()40     public void test_ServiceRegisterer() {
41         if (!mHasNfc) {
42             return;
43         }
44         NfcServiceManager serviceManager = new NfcServiceManager();
45         ServiceRegisterer serviceRegisterer =
46                 serviceManager.getNfcManagerServiceRegisterer();
47 
48         assertThrows(SecurityException.class, () ->
49                 serviceRegisterer.register(serviceRegisterer.get()));
50 
51         IBinder nfcServiceBinder = serviceRegisterer.get();
52         assertNotNull(nfcServiceBinder);
53 
54         nfcServiceBinder = serviceRegisterer.tryGet();
55         assertNotNull(nfcServiceBinder);
56 
57         try {
58             nfcServiceBinder = serviceRegisterer.getOrThrow();
59             assertNotNull(nfcServiceBinder);
60         } catch (ServiceNotFoundException exception) {
61             fail("ServiceNotFoundException should not be thrown");
62         }
63     }
64 
test_ServiceNotFoundException()65     public void test_ServiceNotFoundException() {
66         ServiceManager.ServiceNotFoundException baseException =
67                 new ServiceManager.ServiceNotFoundException("");
68         String exceptionDescription = "description test";
69         String baseExceptionDescription = baseException.getMessage();
70         ServiceNotFoundException newException =
71                 new ServiceNotFoundException(exceptionDescription);
72         assertEquals(baseExceptionDescription + exceptionDescription, newException.getMessage());
73         try {
74             throw newException;
75         } catch (ServiceNotFoundException exception) {
76             assertEquals(baseExceptionDescription + exceptionDescription, exception.getMessage());
77         }
78     }
79 }
80