USB firmware update for stylus in Stylus USI Device Details
Bug: 288850921 Test: StylusUsbFirmwareControllerTest, UsbStylusBroadcastReceiverTest Change-Id: Ic7cb35799ff00f9998ca569eb4ae091f8010780a
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 com.android.settings.connecteddevice.stylus;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class StylusUsbFirmwareControllerTest {
|
||||
|
||||
private Context mContext;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private Lifecycle mLifecycle;
|
||||
private PreferenceScreen mScreen;
|
||||
|
||||
private StylusUsbFirmwareController mController;
|
||||
@Mock
|
||||
private StylusUsiDetailsFragment mFragment;
|
||||
@Mock
|
||||
private UsbManager mUsbManager;
|
||||
private PreferenceCategory mPreferenceCategory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mLifecycle = new Lifecycle(() -> mLifecycle);
|
||||
|
||||
when(mFragment.getContext()).thenReturn(mContext);
|
||||
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mController = new StylusUsbFirmwareController(mContext, "stylus_usb_firmware");
|
||||
|
||||
PreferenceManager preferenceManager = new PreferenceManager(mContext);
|
||||
mScreen = preferenceManager.createPreferenceScreen(mContext);
|
||||
|
||||
mPreferenceCategory = new PreferenceCategory(mContext);
|
||||
mPreferenceCategory.setKey(mController.getPreferenceKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_featurePresentUsbStylusAttached_preferenceAdded() {
|
||||
attachUsbDevice();
|
||||
enableFullStylusFeature();
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
assertNotNull(mScreen.findPreference("stylus_usb_firmware"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_featureAbsentUsbStylusAttached_preferenceNotAdded() {
|
||||
attachUsbDevice();
|
||||
mController.mUsbConnectionListener.onUsbStylusConnectionChanged(
|
||||
mock(UsbDevice.class), true);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
assertNull(mScreen.findPreference(mController.getPreferenceKey()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onUsbStylusConnectionChanged_featurePresentUsbStylusAttached_preferenceAdded() {
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
attachUsbDevice();
|
||||
enableFullStylusFeature();
|
||||
mController.mUsbConnectionListener.onUsbStylusConnectionChanged(
|
||||
mock(UsbDevice.class), true);
|
||||
|
||||
assertNotNull(mScreen.findPreference(mController.getPreferenceKey()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onUsbStylusConnectionChanged_featureAbsentUsbStylusAttached_preferenceRemoved() {
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
attachUsbDevice();
|
||||
mController.mUsbConnectionListener.onUsbStylusConnectionChanged(
|
||||
mock(UsbDevice.class), true);
|
||||
|
||||
assertNull(mScreen.findPreference(mController.getPreferenceKey()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasUsbStylusFirmwareUpdateFeature_featurePresent_true() {
|
||||
when(mFeatureFactory.getStylusFeatureProvider()
|
||||
.isUsbFirmwareUpdateEnabled(any())).thenReturn(true);
|
||||
attachUsbDevice();
|
||||
|
||||
assertTrue(StylusUsbFirmwareController
|
||||
.hasUsbStylusFirmwareUpdateFeature(mock(UsbDevice.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasUsbStylusFirmwareUpdateFeature_featureNotPresent_false() {
|
||||
when(mFeatureFactory.getStylusFeatureProvider()
|
||||
.isUsbFirmwareUpdateEnabled(any())).thenReturn(false);
|
||||
attachUsbDevice();
|
||||
|
||||
assertFalse(StylusUsbFirmwareController
|
||||
.hasUsbStylusFirmwareUpdateFeature(mock(UsbDevice.class)));
|
||||
}
|
||||
|
||||
private void attachUsbDevice() {
|
||||
when(mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager);
|
||||
HashMap<String, UsbDevice> deviceList = new HashMap<>();
|
||||
deviceList.put("0", mock(UsbDevice.class));
|
||||
when(mUsbManager.getDeviceList()).thenReturn(deviceList);
|
||||
}
|
||||
|
||||
private void enableFullStylusFeature() {
|
||||
when(mFeatureFactory.getStylusFeatureProvider()
|
||||
.isUsbFirmwareUpdateEnabled(any())).thenReturn(true);
|
||||
when(mFeatureFactory.getStylusFeatureProvider()
|
||||
.getUsbFirmwareUpdatePreferences(any()))
|
||||
.thenReturn(Collections.singletonList(mock(Preference.class)));
|
||||
}
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 com.android.settings.connecteddevice.stylus;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class UsbStylusBroadcastReceiverTest {
|
||||
private Context mContext;
|
||||
private UsbStylusBroadcastReceiver mReceiver;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
@Mock
|
||||
private UsbStylusBroadcastReceiver.UsbStylusConnectionListener mListener;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mReceiver = new UsbStylusBroadcastReceiver(mContext, mListener);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_usbDeviceAttachedStylus_invokeCallback() {
|
||||
when(mFeatureFactory.mStylusFeatureProvider.isUsbFirmwareUpdateEnabled(any()))
|
||||
.thenReturn(true);
|
||||
final UsbDevice usbDevice = mock(UsbDevice.class);
|
||||
final Intent intent = new Intent();
|
||||
intent.setAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
|
||||
intent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
|
||||
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
|
||||
verify(mListener).onUsbStylusConnectionChanged(usbDevice, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_usbDeviceDetachedStylus_invokeCallback() {
|
||||
when(mFeatureFactory.mStylusFeatureProvider.isUsbFirmwareUpdateEnabled(any()))
|
||||
.thenReturn(true);
|
||||
final UsbDevice usbDevice = mock(UsbDevice.class);
|
||||
final Intent intent = new Intent();
|
||||
intent.setAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
|
||||
intent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
|
||||
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
|
||||
verify(mListener).onUsbStylusConnectionChanged(usbDevice, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_usbDeviceAttachedNotStylus_doesNotInvokeCallback() {
|
||||
when(mFeatureFactory.mStylusFeatureProvider.isUsbFirmwareUpdateEnabled(any()))
|
||||
.thenReturn(false);
|
||||
final UsbDevice usbDevice = mock(UsbDevice.class);
|
||||
final Intent intent = new Intent();
|
||||
intent.setAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
|
||||
intent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
|
||||
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
|
||||
verifyNoMoreInteractions(mListener);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user