Merge changes from topic "nfcc_aon_listener" am: 2195e2b514
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1669508 Change-Id: I451ca24cbfecd944ccda61bcb23df26f3c349642
This commit is contained in:
7
core/java/android/nfc/TEST_MAPPING
Normal file
7
core/java/android/nfc/TEST_MAPPING
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"presubmit": [
|
||||||
|
{
|
||||||
|
"name": "NfcManagerTests"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
38
core/tests/nfctests/Android.bp
Normal file
38
core/tests/nfctests/Android.bp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Copyright 2021 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 {
|
||||||
|
// See: http://go/android-license-faq
|
||||||
|
// A large-scale-change added 'default_applicable_licenses' to import
|
||||||
|
// all of the 'license_kinds' from "frameworks_base_license"
|
||||||
|
// to get the below license kinds:
|
||||||
|
// SPDX-license-identifier-Apache-2.0
|
||||||
|
default_applicable_licenses: ["frameworks_base_license"],
|
||||||
|
}
|
||||||
|
|
||||||
|
android_test {
|
||||||
|
name: "NfcManagerTests",
|
||||||
|
static_libs: [
|
||||||
|
"androidx.test.ext.junit",
|
||||||
|
"androidx.test.rules",
|
||||||
|
"mockito-target-minus-junit4",
|
||||||
|
],
|
||||||
|
libs: [
|
||||||
|
"android.test.runner",
|
||||||
|
],
|
||||||
|
srcs: ["src/**/*.java"],
|
||||||
|
platform_apis: true,
|
||||||
|
certificate: "platform",
|
||||||
|
test_suites: ["device-tests"],
|
||||||
|
}
|
||||||
31
core/tests/nfctests/AndroidManifest.xml
Normal file
31
core/tests/nfctests/AndroidManifest.xml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright 2021 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="android.nfc">
|
||||||
|
|
||||||
|
<application>
|
||||||
|
<uses-library android:name="android.test.runner" />
|
||||||
|
</application>
|
||||||
|
|
||||||
|
<!-- This is a self-instrumenting test package. -->
|
||||||
|
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
android:targetPackage="android.nfc"
|
||||||
|
android:label="NFC Manager Tests">
|
||||||
|
</instrumentation>
|
||||||
|
|
||||||
|
</manifest>
|
||||||
|
|
||||||
32
core/tests/nfctests/AndroidTest.xml
Normal file
32
core/tests/nfctests/AndroidTest.xml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright 2021 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.
|
||||||
|
-->
|
||||||
|
<configuration description="Config for NFC Manager test cases">
|
||||||
|
<option name="test-suite-tag" value="apct"/>
|
||||||
|
<option name="test-suite-tag" value="apct-instrumentation"/>
|
||||||
|
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
|
||||||
|
<option name="cleanup-apks" value="true" />
|
||||||
|
<option name="test-file-name" value="NfcManagerTests.apk" />
|
||||||
|
</target_preparer>
|
||||||
|
|
||||||
|
<option name="test-suite-tag" value="apct"/>
|
||||||
|
<option name="test-tag" value="NfcManagerTests"/>
|
||||||
|
|
||||||
|
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
|
||||||
|
<option name="package" value="android.nfc" />
|
||||||
|
<option name="hidden-api-checks" value="false"/>
|
||||||
|
<option name="runner" value="androidx.test.runner.AndroidJUnitRunner"/>
|
||||||
|
</test>
|
||||||
|
</configuration>
|
||||||
1
core/tests/nfctests/OWNERS
Normal file
1
core/tests/nfctests/OWNERS
Normal file
@@ -0,0 +1 @@
|
|||||||
|
include /core/java/android/nfc/OWNERS
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2021 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;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
|
import static org.mockito.Mockito.doThrow;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.nfc.NfcAdapter.ControllerAlwaysOnListener;
|
||||||
|
import android.os.RemoteException;
|
||||||
|
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
import androidx.test.filters.SmallTest;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test of {@link NfcControllerAlwaysOnListener}.
|
||||||
|
*/
|
||||||
|
@SmallTest
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public class NfcControllerAlwaysOnListenerTest {
|
||||||
|
|
||||||
|
private INfcAdapter mNfcAdapter = mock(INfcAdapter.class);
|
||||||
|
|
||||||
|
private Throwable mThrowRemoteException = new RemoteException("RemoteException");
|
||||||
|
|
||||||
|
private static Executor getExecutor() {
|
||||||
|
return new Executor() {
|
||||||
|
@Override
|
||||||
|
public void execute(Runnable command) {
|
||||||
|
command.run();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void verifyListenerInvoked(ControllerAlwaysOnListener listener) {
|
||||||
|
verify(listener, times(1)).onControllerAlwaysOnChanged(anyBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegister_RegisterUnregister() throws RemoteException {
|
||||||
|
NfcControllerAlwaysOnListener mListener =
|
||||||
|
new NfcControllerAlwaysOnListener(mNfcAdapter);
|
||||||
|
ControllerAlwaysOnListener mockListener1 = mock(ControllerAlwaysOnListener.class);
|
||||||
|
ControllerAlwaysOnListener mockListener2 = mock(ControllerAlwaysOnListener.class);
|
||||||
|
|
||||||
|
// Verify that the state listener registered with the NFC Adapter
|
||||||
|
mListener.register(getExecutor(), mockListener1);
|
||||||
|
verify(mNfcAdapter, times(1)).registerControllerAlwaysOnListener(any());
|
||||||
|
|
||||||
|
// Register a second client and no new call to NFC Adapter
|
||||||
|
mListener.register(getExecutor(), mockListener2);
|
||||||
|
verify(mNfcAdapter, times(1)).registerControllerAlwaysOnListener(any());
|
||||||
|
|
||||||
|
// Unregister first listener
|
||||||
|
mListener.unregister(mockListener1);
|
||||||
|
verify(mNfcAdapter, times(1)).registerControllerAlwaysOnListener(any());
|
||||||
|
verify(mNfcAdapter, times(0)).unregisterControllerAlwaysOnListener(any());
|
||||||
|
|
||||||
|
// Unregister second listener and the state listener registered with the NFC Adapter
|
||||||
|
mListener.unregister(mockListener2);
|
||||||
|
verify(mNfcAdapter, times(1)).registerControllerAlwaysOnListener(any());
|
||||||
|
verify(mNfcAdapter, times(1)).unregisterControllerAlwaysOnListener(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegister_FirstRegisterFails() throws RemoteException {
|
||||||
|
NfcControllerAlwaysOnListener mListener =
|
||||||
|
new NfcControllerAlwaysOnListener(mNfcAdapter);
|
||||||
|
ControllerAlwaysOnListener mockListener1 = mock(ControllerAlwaysOnListener.class);
|
||||||
|
ControllerAlwaysOnListener mockListener2 = mock(ControllerAlwaysOnListener.class);
|
||||||
|
|
||||||
|
// Throw a remote exception whenever first registering
|
||||||
|
doThrow(mThrowRemoteException).when(mNfcAdapter).registerControllerAlwaysOnListener(
|
||||||
|
any());
|
||||||
|
|
||||||
|
mListener.register(getExecutor(), mockListener1);
|
||||||
|
verify(mNfcAdapter, times(1)).registerControllerAlwaysOnListener(any());
|
||||||
|
|
||||||
|
// No longer throw an exception, instead succeed
|
||||||
|
doNothing().when(mNfcAdapter).registerControllerAlwaysOnListener(any());
|
||||||
|
|
||||||
|
// Register a different listener
|
||||||
|
mListener.register(getExecutor(), mockListener2);
|
||||||
|
verify(mNfcAdapter, times(2)).registerControllerAlwaysOnListener(any());
|
||||||
|
|
||||||
|
// Ensure first and second listener were invoked
|
||||||
|
mListener.onControllerAlwaysOnChanged(true);
|
||||||
|
verifyListenerInvoked(mockListener1);
|
||||||
|
verifyListenerInvoked(mockListener2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegister_RegisterSameListenerTwice() throws RemoteException {
|
||||||
|
NfcControllerAlwaysOnListener mListener =
|
||||||
|
new NfcControllerAlwaysOnListener(mNfcAdapter);
|
||||||
|
ControllerAlwaysOnListener mockListener = mock(ControllerAlwaysOnListener.class);
|
||||||
|
|
||||||
|
// Register the same listener Twice
|
||||||
|
mListener.register(getExecutor(), mockListener);
|
||||||
|
mListener.register(getExecutor(), mockListener);
|
||||||
|
verify(mNfcAdapter, times(1)).registerControllerAlwaysOnListener(any());
|
||||||
|
|
||||||
|
// Invoke a state change and ensure the listener is only called once
|
||||||
|
mListener.onControllerAlwaysOnChanged(true);
|
||||||
|
verifyListenerInvoked(mockListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotify_AllListenersNotified() throws RemoteException {
|
||||||
|
|
||||||
|
NfcControllerAlwaysOnListener listener = new NfcControllerAlwaysOnListener(mNfcAdapter);
|
||||||
|
List<ControllerAlwaysOnListener> mockListeners = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
ControllerAlwaysOnListener mockListener = mock(ControllerAlwaysOnListener.class);
|
||||||
|
listener.register(getExecutor(), mockListener);
|
||||||
|
mockListeners.add(mockListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invoke a state change and ensure all listeners are invoked
|
||||||
|
listener.onControllerAlwaysOnChanged(true);
|
||||||
|
for (ControllerAlwaysOnListener mListener : mockListeners) {
|
||||||
|
verifyListenerInvoked(mListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStateChange_CorrectValue() {
|
||||||
|
runStateChangeValue(true, true);
|
||||||
|
runStateChangeValue(false, false);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runStateChangeValue(boolean isEnabledIn, boolean isEnabledOut) {
|
||||||
|
NfcControllerAlwaysOnListener listener = new NfcControllerAlwaysOnListener(mNfcAdapter);
|
||||||
|
ControllerAlwaysOnListener mockListener = mock(ControllerAlwaysOnListener.class);
|
||||||
|
listener.register(getExecutor(), mockListener);
|
||||||
|
listener.onControllerAlwaysOnChanged(isEnabledIn);
|
||||||
|
verify(mockListener, times(1)).onControllerAlwaysOnChanged(isEnabledOut);
|
||||||
|
verify(mockListener, times(0)).onControllerAlwaysOnChanged(!isEnabledOut);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user