Delete legacy VMS service implementation.
Bug: 148239926
Test: Compiled and flashed system image
Change-Id: Ic3d20db078f48e8b7f49ca97338176e0bc915e41
diff --git a/car-lib/src/android/car/vms/IVmsPublisherClient.aidl b/car-lib/src/android/car/vms/IVmsPublisherClient.aidl
deleted file mode 100644
index 96b993b..0000000
--- a/car-lib/src/android/car/vms/IVmsPublisherClient.aidl
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2017 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.car.vms;
-
-import android.car.vms.IVmsPublisherService;
-import android.car.vms.VmsSubscriptionState;
-
-/**
- * @hide
- */
-interface IVmsPublisherClient {
- /**
- * Once the VmsPublisherService is bound to the client, this callback is used to set the
- * binder that the client can use to invoke publisher services. This also gives the client
- * the token it should use when calling the service.
- */
- oneway void setVmsPublisherService(in IBinder token, IVmsPublisherService service) = 0;
-
- /**
- * The VmsPublisherService uses this callback to notify about subscription changes.
- * @param subscriptionState all the layers that have subscribers and a sequence number,
- * clients should ignore any packet with a sequence number that is less
- * than the highest sequence number they have seen thus far.
- */
- oneway void onVmsSubscriptionChange(in VmsSubscriptionState subscriptionState) = 1;
-}
diff --git a/car-lib/src/android/car/vms/IVmsPublisherService.aidl b/car-lib/src/android/car/vms/IVmsPublisherService.aidl
deleted file mode 100644
index 3c26a1b..0000000
--- a/car-lib/src/android/car/vms/IVmsPublisherService.aidl
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2017 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.car.vms;
-
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsLayersOffering;
-import android.car.vms.VmsSubscriptionState;
-
-/**
- * Exposes publisher services to VMS clients.
- *
- * @hide
- */
-interface IVmsPublisherService {
- /**
- * Client call to publish a message.
- */
- oneway void publish(in IBinder token, in VmsLayer layer, int publisherId, in byte[] message) = 0;
-
- /**
- * Returns the list of VmsLayers that has any clients subscribed to it.
- */
- VmsSubscriptionState getSubscriptions() = 1;
-
- /**
- * Sets which layers the publisher can publish under which dependencties.
- */
- oneway void setLayersOffering(in IBinder token, in VmsLayersOffering offering) = 2;
-
- /**
- * The first time a publisher calls this API it will store the publisher info and assigns the
- * publisher an ID. Between reboots, subsequent calls with the same publisher info will
- * return the same ID so that a restarting process can obtain the same ID as it had before.
- */
- int getPublisherId(in byte[] publisherInfo) = 3;
-}
diff --git a/car-lib/src/android/car/vms/IVmsSubscriberClient.aidl b/car-lib/src/android/car/vms/IVmsSubscriberClient.aidl
deleted file mode 100644
index 8f67cd5..0000000
--- a/car-lib/src/android/car/vms/IVmsSubscriberClient.aidl
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2017 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.car.vms;
-
-import android.car.vms.VmsAvailableLayers;
-import android.car.vms.VmsLayer;
-
-/**
- * @hide
- */
-oneway interface IVmsSubscriberClient {
- /**
- * A VmsService uses this callback to pass messages to subscribers.
- */
- void onVmsMessageReceived(in VmsLayer layer, in byte[] payload) = 0;
-
- void onLayersAvailabilityChanged(in VmsAvailableLayers availableLayers) = 1;
-}
diff --git a/car-lib/src/android/car/vms/IVmsSubscriberService.aidl b/car-lib/src/android/car/vms/IVmsSubscriberService.aidl
deleted file mode 100644
index 661065a..0000000
--- a/car-lib/src/android/car/vms/IVmsSubscriberService.aidl
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2017 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.car.vms;
-
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsAvailableLayers;
-
-/**
- * @hide
- */
-interface IVmsSubscriberService {
- /**
- * Adds a subscriber to notifications only.
- * Should be called when a subscriber registers its callback, and before any subscription to a
- * layer is made.
- */
- void addVmsSubscriberToNotifications(
- in IVmsSubscriberClient subscriber) = 0;
-
- /**
- * Adds a subscriber to a VMS layer.
- */
- void addVmsSubscriber(
- in IVmsSubscriberClient subscriber,
- in VmsLayer layer) = 1;
-
- /**
- * Adds a subscriber to all actively broadcasted layers.
- * Publishers will not be notified regarding this request so the state of the service will not
- * change.
- */
- void addVmsSubscriberPassive(in IVmsSubscriberClient subscriber) = 2;
-
- /**
- * Adds a subscriber to a VMS layer from a specific publisher.
- */
- void addVmsSubscriberToPublisher(
- in IVmsSubscriberClient subscriber,
- in VmsLayer layer,
- int publisherId) = 3;
-
- /**
- * Removes a subscriber to notifications only.
- * Should be called when a subscriber unregisters its callback, and after all subscriptions to
- * layers are removed.
- */
- void removeVmsSubscriberToNotifications(
- in IVmsSubscriberClient subscriber) = 4;
-
- /**
- * Removes a subscriber to a VMS layer.
- */
- void removeVmsSubscriber(
- in IVmsSubscriberClient subscriber,
- in VmsLayer layer) = 5;
-
- /**
- * Removes a subscriber to all actively broadcasted layers.
- * Publishers will not be notified regarding this request so the state of the service will not
- * change.
- */
- void removeVmsSubscriberPassive(
- in IVmsSubscriberClient subscriber) = 6;
-
- /**
- * Removes a subscriber to a VMS layer from a specific publisher.
- */
- void removeVmsSubscriberToPublisher(
- in IVmsSubscriberClient subscriber,
- in VmsLayer layer,
- int publisherId) = 7;
-
- /**
- * Returns a list of available layers from the closure of the publishers offerings.
- */
- VmsAvailableLayers getAvailableLayers() = 8;
-
- /**
- * Returns a the publisher information for a publisher ID.
- */
- byte[] getPublisherInfo(in int publisherId) = 9;
-}
diff --git a/car-test-lib/src/android/car/testapi/FakeCar.java b/car-test-lib/src/android/car/testapi/FakeCar.java
index 78d7e81..932ac76 100644
--- a/car-test-lib/src/android/car/testapi/FakeCar.java
+++ b/car-test-lib/src/android/car/testapi/FakeCar.java
@@ -27,7 +27,6 @@
import android.car.media.ICarAudio;
import android.car.settings.ICarConfigurationManager;
import android.car.storagemonitoring.ICarStorageMonitoring;
-import android.car.vms.IVmsSubscriberService;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
@@ -136,7 +135,6 @@
@Mock ICarDiagnostic.Stub mCarDiagnostic;
@Mock ICarPower.Stub mCarPower;
@Mock IInstrumentClusterManagerService.Stub mClusterService;
- @Mock IVmsSubscriberService.Stub mVmsSubscriberService;
@Mock ICarBluetooth.Stub mCarBluetooth;
@Mock ICarStorageMonitoring.Stub mCarStorageMonitoring;
@Mock ICarDrivingState.Stub mCarDrivingState;
@@ -204,8 +202,6 @@
return mClusterService;
case Car.PROJECTION_SERVICE:
return mCarProjection;
- case Car.VMS_SUBSCRIBER_SERVICE:
- return mVmsSubscriberService;
case Car.BLUETOOTH_SERVICE:
return mCarBluetooth;
case Car.STORAGE_MONITORING_SERVICE:
diff --git a/service/src/com/android/car/VmsPublisherService.java b/service/src/com/android/car/VmsPublisherService.java
deleted file mode 100644
index def10dd..0000000
--- a/service/src/com/android/car/VmsPublisherService.java
+++ /dev/null
@@ -1,249 +0,0 @@
-/*
- * Copyright (C) 2017 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.car;
-
-import android.car.vms.IVmsPublisherClient;
-import android.car.vms.IVmsPublisherService;
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsLayersOffering;
-import android.car.vms.VmsSubscriptionState;
-import android.content.Context;
-import android.os.Binder;
-import android.os.IBinder;
-import android.os.RemoteException;
-import android.util.ArrayMap;
-import android.util.Log;
-
-import com.android.car.stats.CarStatsService;
-import com.android.car.vms.VmsBrokerService;
-import com.android.car.vms.VmsClientManager;
-import com.android.internal.annotations.VisibleForTesting;
-
-import java.io.PrintWriter;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-import java.util.function.IntSupplier;
-
-
-/**
- * Receives HAL updates by implementing VmsHalService.VmsHalListener.
- * Binds to publishers and configures them to use this service.
- * Notifies publishers of subscription changes.
- */
-public class VmsPublisherService implements CarServiceBase {
- private static final boolean DBG = false;
- private static final String TAG = "VmsPublisherService";
-
- private final Context mContext;
- private final CarStatsService mStatsService;
- private final VmsBrokerService mBrokerService;
- private final VmsClientManager mClientManager;
- private final IntSupplier mGetCallingUid;
- private final Map<String, PublisherProxy> mPublisherProxies = Collections.synchronizedMap(
- new ArrayMap<>());
-
- VmsPublisherService(
- Context context,
- CarStatsService statsService,
- VmsBrokerService brokerService,
- VmsClientManager clientManager) {
- this(context, statsService, brokerService, clientManager, Binder::getCallingUid);
- }
-
- @VisibleForTesting
- VmsPublisherService(
- Context context,
- CarStatsService statsService,
- VmsBrokerService brokerService,
- VmsClientManager clientManager,
- IntSupplier getCallingUid) {
- mContext = context;
- mStatsService = statsService;
- mBrokerService = brokerService;
- mClientManager = clientManager;
- mGetCallingUid = getCallingUid;
-
- mClientManager.setPublisherService(this);
- }
-
- @Override
- public void init() {}
-
- @Override
- public void release() {
- mPublisherProxies.values().forEach(PublisherProxy::unregister);
- mPublisherProxies.clear();
- }
-
- @Override
- public void dump(PrintWriter writer) {
- writer.println("*" + getClass().getSimpleName() + "*");
- writer.println("mPublisherProxies: " + mPublisherProxies.size());
- }
-
- /**
- * Called when a client connection is established or re-established.
- *
- * @param publisherName String that uniquely identifies the service and user.
- * @param publisherClient The client's communication channel.
- */
- public void onClientConnected(String publisherName, IVmsPublisherClient publisherClient) {
- if (DBG) Log.d(TAG, "onClientConnected: " + publisherName);
- IBinder publisherToken = new Binder();
-
- PublisherProxy publisherProxy = new PublisherProxy(publisherName, publisherToken,
- publisherClient);
- publisherProxy.register();
- try {
- publisherClient.setVmsPublisherService(publisherToken, publisherProxy);
- } catch (Throwable e) {
- Log.e(TAG, "unable to configure publisher: " + publisherName, e);
- return;
- }
-
- PublisherProxy existingProxy = mPublisherProxies.put(publisherName, publisherProxy);
- if (existingProxy != null) {
- existingProxy.unregister();
- }
- }
-
- /**
- * Called when a client connection is terminated.
- *
- * @param publisherName String that uniquely identifies the service and user.
- */
- public void onClientDisconnected(String publisherName) {
- if (DBG) Log.d(TAG, "onClientDisconnected: " + publisherName);
- PublisherProxy proxy = mPublisherProxies.remove(publisherName);
- if (proxy != null) {
- proxy.unregister();
- }
- }
-
- private class PublisherProxy extends IVmsPublisherService.Stub implements
- VmsBrokerService.PublisherListener {
- private final String mName;
- private final IBinder mToken;
- private final IVmsPublisherClient mPublisherClient;
- private boolean mConnected;
-
- PublisherProxy(String name, IBinder token,
- IVmsPublisherClient publisherClient) {
- this.mName = name;
- this.mToken = token;
- this.mPublisherClient = publisherClient;
- }
-
- void register() {
- if (DBG) Log.d(TAG, "register: " + mName);
- mConnected = true;
- mBrokerService.addPublisherListener(this);
- }
-
- void unregister() {
- if (DBG) Log.d(TAG, "unregister: " + mName);
- mConnected = false;
- mBrokerService.removePublisherListener(this);
- mBrokerService.removeDeadPublisher(mToken);
- }
-
- @Override
- public void setLayersOffering(IBinder token, VmsLayersOffering offering) {
- assertPermission(token);
- mBrokerService.setPublisherLayersOffering(token, offering);
- }
-
- @Override
- public void publish(IBinder token, VmsLayer layer, int publisherId, byte[] payload) {
- assertPermission(token);
- if (DBG) {
- Log.d(TAG, String.format("Publishing to %s as %d (%s)", layer, publisherId, mName));
- }
-
- if (layer == null) {
- return;
- }
-
- int payloadLength = payload != null ? payload.length : 0;
- mStatsService.getVmsClientLogger(mGetCallingUid.getAsInt())
- .logPacketSent(layer, payloadLength);
-
- // Send the message to subscribers
- Set<IVmsSubscriberClient> listeners =
- mBrokerService.getSubscribersForLayerFromPublisher(layer, publisherId);
-
- if (DBG) Log.d(TAG, String.format("Number of subscribers: %d", listeners.size()));
-
- if (listeners.size() == 0) {
- // A negative UID signals that the packet had zero subscribers
- mStatsService.getVmsClientLogger(-1)
- .logPacketDropped(layer, payloadLength);
- }
-
- for (IVmsSubscriberClient listener : listeners) {
- int subscriberUid = mClientManager.getSubscriberUid(listener);
- try {
- listener.onVmsMessageReceived(layer, payload);
- mStatsService.getVmsClientLogger(subscriberUid)
- .logPacketReceived(layer, payloadLength);
- } catch (RemoteException ex) {
- mStatsService.getVmsClientLogger(subscriberUid)
- .logPacketDropped(layer, payloadLength);
- String subscriberName = mClientManager.getPackageName(listener);
- Log.e(TAG, String.format("Unable to publish to listener: %s", subscriberName));
- }
- }
- }
-
- @Override
- public VmsSubscriptionState getSubscriptions() {
- assertPermission();
- return mBrokerService.getSubscriptionState();
- }
-
- @Override
- public int getPublisherId(byte[] publisherInfo) {
- assertPermission();
- return mBrokerService.getPublisherId(publisherInfo);
- }
-
- @Override
- public void onSubscriptionChange(VmsSubscriptionState subscriptionState) {
- try {
- mPublisherClient.onVmsSubscriptionChange(subscriptionState);
- } catch (Throwable e) {
- Log.e(TAG, String.format("Unable to send subscription state to: %s", mName), e);
- }
- }
-
- private void assertPermission(IBinder publisherToken) {
- if (mToken != publisherToken) {
- throw new SecurityException("Invalid publisher token");
- }
- assertPermission();
- }
-
- private void assertPermission() {
- if (!mConnected) {
- throw new SecurityException("Publisher has been disconnected");
- }
- ICarImpl.assertVmsPublisherPermission(mContext);
- }
- }
-}
diff --git a/service/src/com/android/car/VmsRouting.java b/service/src/com/android/car/VmsRouting.java
deleted file mode 100644
index 97a928f..0000000
--- a/service/src/com/android/car/VmsRouting.java
+++ /dev/null
@@ -1,307 +0,0 @@
-/*
- * Copyright (C) 2017 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.car;
-
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsAssociatedLayer;
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsOperationRecorder;
-import android.car.vms.VmsSubscriptionState;
-import android.os.IBinder;
-import android.util.ArrayMap;
-import android.util.ArraySet;
-import android.util.Pair;
-
-import com.android.internal.annotations.GuardedBy;
-
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-/**
- * Manages all the VMS subscriptions:
- * + Subscriptions to data messages of individual layer + version.
- * + Subscriptions to all data messages.
- * + HAL subscriptions to layer + version.
- */
-
-public class VmsRouting {
- private final Object mLock = new Object();
-
- @GuardedBy("mLock")
- private Map<IBinder, IVmsSubscriberClient> mSubscribers = new ArrayMap<>();
-
- @GuardedBy("mLock")
- private Set<IBinder> mPassiveSubscribers = new ArraySet<>();
-
- @GuardedBy("mLock")
- private Map<VmsLayer, Set<IBinder>> mLayerSubscriptions = new ArrayMap<>();
-
- @GuardedBy("mLock")
- private Map<VmsLayer, Map<Integer, Set<IBinder>>> mLayerSubscriptionsToPublishers =
- new ArrayMap<>();
-
- @GuardedBy("mLock")
- private int mSequenceNumber = 0;
-
- /**
- * Add a passive subscription to all data messages.
- *
- * Passive subscribers receive all published data messages, but are not reflected in the
- * subscription state sent to publishers.
- *
- * @param subscriber VMS subscriber to add
- */
- public void addSubscription(IVmsSubscriberClient subscriber) {
- int sequenceNumber;
- synchronized (mLock) {
- if (!mPassiveSubscribers.add(addSubscriber(subscriber))) {
- return;
- }
- sequenceNumber = mSequenceNumber;
- }
- VmsOperationRecorder.get().addPromiscuousSubscription(sequenceNumber);
- }
-
- /**
- * Remove a passive subscription to all data messages.
- *
- * @param subscriber VMS subscriber to remove
- */
- public void removeSubscription(IVmsSubscriberClient subscriber) {
- int sequenceNumber;
- synchronized (mLock) {
- if (!mPassiveSubscribers.remove(subscriber.asBinder())) {
- return;
- }
- sequenceNumber = mSequenceNumber;
- }
- VmsOperationRecorder.get().removePromiscuousSubscription(sequenceNumber);
- }
-
- /**
- * Add a subscription to data messages from a VMS layer.
- *
- * @param subscriber VMS subscriber to add
- * @param layer the layer to subscribe to
- */
- public void addSubscription(IVmsSubscriberClient subscriber, VmsLayer layer) {
- int sequenceNumber;
- synchronized (mLock) {
- Set<IBinder> subscribers =
- mLayerSubscriptions.computeIfAbsent(layer, k -> new ArraySet<>());
- if (!subscribers.add(addSubscriber(subscriber))) {
- return;
- }
- sequenceNumber = ++mSequenceNumber;
- }
- VmsOperationRecorder.get().addSubscription(sequenceNumber, layer);
- }
-
- /**
- * Remove a subscription to data messages from a VMS layer.
- *
- * @param subscriber VMS subscriber to remove
- * @param layer the subscribed layer
- */
- public void removeSubscription(IVmsSubscriberClient subscriber, VmsLayer layer) {
- int sequenceNumber;
- synchronized (mLock) {
- Set<IBinder> subscribers =
- mLayerSubscriptions.getOrDefault(layer, Collections.emptySet());
- if (!subscribers.remove(subscriber.asBinder())) {
- return;
- }
- sequenceNumber = ++mSequenceNumber;
-
- if (subscribers.isEmpty()) {
- // If a layer has no subscribers, remove it
- mLayerSubscriptions.remove(layer);
- }
- }
- VmsOperationRecorder.get().removeSubscription(sequenceNumber, layer);
- }
-
- /**
- * Add a subscription to data messages from a VMS layer and a specific publisher.
- *
- * @param subscriber VMS subscriber to add
- * @param layer the layer to subscribe to
- * @param publisherId the publisher ID
- */
- public void addSubscription(IVmsSubscriberClient subscriber, VmsLayer layer, int publisherId) {
- int sequenceNumber;
- synchronized (mLock) {
- Set<IBinder> subscribers =
- mLayerSubscriptionsToPublishers.computeIfAbsent(layer, k -> new ArrayMap<>())
- .computeIfAbsent(publisherId, k -> new ArraySet<>());
- if (!subscribers.add(addSubscriber(subscriber))) {
- return;
- }
- sequenceNumber = ++mSequenceNumber;
- }
- VmsOperationRecorder.get().addSubscription(sequenceNumber, layer);
- }
-
- /**
- * Remove a subscription to data messages from a VMS layer and a specific publisher.
- *
- * @param subscriber VMS subscriber to remove
- * @param layer the subscribed layer
- * @param publisherId the publisher ID
- */
- public void removeSubscription(IVmsSubscriberClient subscriber,
- VmsLayer layer,
- int publisherId) {
- int sequenceNumber;
- synchronized (mLock) {
- Map<Integer, Set<IBinder>> subscribersToPublishers =
- mLayerSubscriptionsToPublishers.getOrDefault(layer, Collections.emptyMap());
-
- Set<IBinder> subscribers =
- subscribersToPublishers.getOrDefault(publisherId, Collections.emptySet());
- if (!subscribers.remove(subscriber.asBinder())) {
- return;
- }
- sequenceNumber = ++mSequenceNumber;
-
- // If a publisher has no subscribers, remove it
- if (subscribers.isEmpty()) {
- subscribersToPublishers.remove(publisherId);
- }
-
- // If a layer has no subscribers, remove it
- if (subscribersToPublishers.isEmpty()) {
- mLayerSubscriptionsToPublishers.remove(layer);
- }
- }
- VmsOperationRecorder.get().removeSubscription(sequenceNumber, layer);
- }
-
- /**
- * Remove all of a subscriber's subscriptions.
- *
- * @param subscriber VMS subscriber to remove
- * @return {@code true} if the subscription state was modified
- */
- public boolean removeDeadSubscriber(IVmsSubscriberClient subscriber) {
- IBinder subscriberBinder = subscriber.asBinder();
- synchronized (mLock) {
- int startSequenceNumber = mSequenceNumber;
-
- // Remove the subscriber from the loggers.
- removeSubscription(subscriber);
-
- // Remove the subscriber from all layer-based subscriptions.
- mLayerSubscriptions.entrySet().stream()
- .filter(e -> e.getValue().contains(subscriberBinder))
- .map(Map.Entry::getKey)
- .collect(Collectors.toSet())
- .forEach(layer -> removeSubscription(subscriber, layer));
-
- // Remove the subscriber from all publisher-based subscriptions.
- mLayerSubscriptionsToPublishers.entrySet().stream()
- .flatMap(layer -> layer.getValue().entrySet().stream()
- .filter(publisher -> publisher.getValue().contains(subscriberBinder))
- .map(publisher -> Pair.create(layer.getKey(), publisher.getKey())))
- .collect(Collectors.toSet())
- .forEach(layerAndPublisher -> removeSubscription(subscriber,
- layerAndPublisher.first, layerAndPublisher.second));
-
- // Remove the subscriber from the subscriber index
- mSubscribers.remove(subscriberBinder);
-
- // If the sequence number was updated, then the subscription state was modified
- return startSequenceNumber != mSequenceNumber;
- }
- }
-
- /**
- * Returns a list of all the subscribers a data message should be delivered to. This includes
- * subscribers that subscribed to this layer from all publishers, subscribed to this layer
- * from a specific publisher, and passive subscribers.
- *
- * @param layer The layer of the message.
- * @param publisherId the ID of the client that published the message to be routed.
- * @return a list of the subscribers.
- */
- public Set<IVmsSubscriberClient> getSubscribersForLayerFromPublisher(VmsLayer layer,
- int publisherId) {
- Set<IBinder> subscribers = new HashSet<>();
- synchronized (mLock) {
- // Add the passive subscribers
- subscribers.addAll(mPassiveSubscribers);
-
- // Add the subscribers which explicitly subscribed to this layer
- subscribers.addAll(mLayerSubscriptions.getOrDefault(layer, Collections.emptySet()));
-
- // Add the subscribers which explicitly subscribed to this layer and publisher
- subscribers.addAll(
- mLayerSubscriptionsToPublishers.getOrDefault(layer, Collections.emptyMap())
- .getOrDefault(publisherId, Collections.emptySet()));
- }
- return subscribers.stream()
- .map(binder -> mSubscribers.get(binder))
- .filter(Objects::nonNull)
- .collect(Collectors.toSet());
- }
-
- /**
- * @return {@code true} if there is an explicit subscription to the layer
- */
- public boolean hasLayerSubscriptions(VmsLayer layer) {
- synchronized (mLock) {
- return mLayerSubscriptions.containsKey(layer);
- }
- }
-
- /**
- * @return {@code true} if there is an explicit subscription to the layer and publisherId
- */
- public boolean hasLayerFromPublisherSubscriptions(VmsLayer layer, int publisherId) {
- synchronized (mLock) {
- return mLayerSubscriptionsToPublishers.containsKey(layer)
- && mLayerSubscriptionsToPublishers.getOrDefault(layer, Collections.emptyMap())
- .containsKey(publisherId);
- }
- }
-
- /**
- * @return a Set of layers and publishers which VMS clients are subscribed to.
- */
- public VmsSubscriptionState getSubscriptionState() {
- synchronized (mLock) {
- return new VmsSubscriptionState(mSequenceNumber,
- new ArraySet<>(mLayerSubscriptions.keySet()),
- mLayerSubscriptionsToPublishers.entrySet()
- .stream()
- .map(e -> new VmsAssociatedLayer(e.getKey(), e.getValue().keySet()))
- .collect(Collectors.toSet()));
- }
- }
-
- private IBinder addSubscriber(IVmsSubscriberClient subscriber) {
- IBinder subscriberBinder = subscriber.asBinder();
- synchronized (mLock) {
- mSubscribers.putIfAbsent(subscriberBinder, subscriber);
- }
- return subscriberBinder;
- }
-}
diff --git a/service/src/com/android/car/VmsSubscriberService.java b/service/src/com/android/car/VmsSubscriberService.java
deleted file mode 100644
index 864be5c..0000000
--- a/service/src/com/android/car/VmsSubscriberService.java
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * Copyright (C) 2017 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.car;
-
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.IVmsSubscriberService;
-import android.car.vms.VmsAvailableLayers;
-import android.car.vms.VmsLayer;
-import android.content.Context;
-import android.os.RemoteException;
-import android.util.Log;
-
-import com.android.car.hal.VmsHalService;
-import com.android.car.vms.VmsBrokerService;
-import com.android.car.vms.VmsClientManager;
-
-import java.io.PrintWriter;
-
-/**
- * Offers subscriber services by implementing IVmsSubscriberService.Stub.
- */
-public class VmsSubscriberService extends IVmsSubscriberService.Stub implements CarServiceBase,
- VmsBrokerService.SubscriberListener {
- private static final String TAG = "VmsSubscriberService";
-
- private final Context mContext;
- private final VmsBrokerService mBrokerService;
- private final VmsClientManager mClientManager;
-
- /**
- * Constructor for client manager.
- *
- * @param context Context to use for registering receivers and binding services.
- * @param brokerService Service managing the VMS publisher/subscriber state.
- * @param clientManager Service for monitoring VMS subscriber clients.
- * @param hal Service providing the HAL client interface
- */
- VmsSubscriberService(Context context, VmsBrokerService brokerService,
- VmsClientManager clientManager, VmsHalService hal) {
- mContext = context;
- mBrokerService = brokerService;
- mClientManager = clientManager;
- mBrokerService.addSubscriberListener(this);
- }
-
- @Override
- public void init() {}
-
- @Override
- public void release() {}
-
- @Override
- public void dump(PrintWriter writer) {
- }
-
- @Override
- public void addVmsSubscriberToNotifications(IVmsSubscriberClient subscriber) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mClientManager.addSubscriber(subscriber);
- }
-
- @Override
- public void removeVmsSubscriberToNotifications(IVmsSubscriberClient subscriber) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mClientManager.removeSubscriber(subscriber);
- }
-
- @Override
- public void addVmsSubscriber(IVmsSubscriberClient subscriber, VmsLayer layer) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mClientManager.addSubscriber(subscriber);
- mBrokerService.addSubscription(subscriber, layer);
- }
-
- @Override
- public void removeVmsSubscriber(IVmsSubscriberClient subscriber, VmsLayer layer) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mBrokerService.removeSubscription(subscriber, layer);
- }
-
- @Override
- public void addVmsSubscriberToPublisher(IVmsSubscriberClient subscriber,
- VmsLayer layer,
- int publisherId) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mClientManager.addSubscriber(subscriber);
- mBrokerService.addSubscription(subscriber, layer, publisherId);
- }
-
- @Override
- public void removeVmsSubscriberToPublisher(IVmsSubscriberClient subscriber,
- VmsLayer layer,
- int publisherId) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mBrokerService.removeSubscription(subscriber, layer, publisherId);
- }
-
- @Override
- public void addVmsSubscriberPassive(IVmsSubscriberClient subscriber) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mClientManager.addSubscriber(subscriber);
- mBrokerService.addSubscription(subscriber);
- }
-
- @Override
- public void removeVmsSubscriberPassive(IVmsSubscriberClient subscriber) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- mBrokerService.removeSubscription(subscriber);
- }
-
- @Override
- public byte[] getPublisherInfo(int publisherId) {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- return mBrokerService.getPublisherInfo(publisherId);
- }
-
- @Override
- public VmsAvailableLayers getAvailableLayers() {
- ICarImpl.assertVmsSubscriberPermission(mContext);
- return mBrokerService.getAvailableLayers();
- }
-
- @Override
- public void onLayersAvailabilityChange(VmsAvailableLayers availableLayers) {
- for (IVmsSubscriberClient subscriber : mClientManager.getAllSubscribers()) {
- try {
- subscriber.onLayersAvailabilityChanged(availableLayers);
- } catch (RemoteException e) {
- Log.e(TAG, "onLayersAvailabilityChanged failed: "
- + mClientManager.getPackageName(subscriber), e);
- }
- }
- }
-}
diff --git a/service/src/com/android/car/vms/VmsBrokerService.java b/service/src/com/android/car/vms/VmsBrokerService.java
deleted file mode 100644
index ad0bfad..0000000
--- a/service/src/com/android/car/vms/VmsBrokerService.java
+++ /dev/null
@@ -1,375 +0,0 @@
-/*
- * Copyright (C) 2019 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.car.vms;
-
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsAvailableLayers;
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsLayersOffering;
-import android.car.vms.VmsOperationRecorder;
-import android.car.vms.VmsSubscriptionState;
-import android.os.IBinder;
-import android.util.Log;
-
-import com.android.car.VmsLayersAvailability;
-import com.android.car.VmsPublishersInfo;
-import com.android.car.VmsRouting;
-import com.android.internal.annotations.GuardedBy;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.CopyOnWriteArrayList;
-
-/**
- * Broker service facilitating subscription handling and message passing between
- * VmsPublisherService, VmsSubscriberService, and VmsHalService.
- */
-public class VmsBrokerService {
- private static final boolean DBG = false;
- private static final String TAG = "VmsBrokerService";
-
- private CopyOnWriteArrayList<PublisherListener> mPublisherListeners =
- new CopyOnWriteArrayList<>();
- private CopyOnWriteArrayList<SubscriberListener> mSubscriberListeners =
- new CopyOnWriteArrayList<>();
-
- private final Object mLock = new Object();
- @GuardedBy("mLock")
- private final VmsRouting mRouting = new VmsRouting();
- @GuardedBy("mLock")
- private final Map<IBinder, Map<Integer, VmsLayersOffering>> mOfferings = new HashMap<>();
- @GuardedBy("mLock")
- private final VmsLayersAvailability mAvailableLayers = new VmsLayersAvailability();
- @GuardedBy("mLock")
- private final VmsPublishersInfo mPublishersInfo = new VmsPublishersInfo();
-
- /**
- * The VMS publisher service implements this interface to receive publisher callbacks.
- */
- public interface PublisherListener {
- /**
- * Callback triggered when publisher subscription state changes.
- *
- * @param subscriptionState Current subscription state.
- */
- void onSubscriptionChange(VmsSubscriptionState subscriptionState);
- }
-
- /**
- * The VMS subscriber service implements this interface to receive subscriber callbacks.
- */
- public interface SubscriberListener {
- /**
- * Callback triggered when the layers available for subscription changes.
- *
- * @param availableLayers Current layer availability
- */
- void onLayersAvailabilityChange(VmsAvailableLayers availableLayers);
- }
-
- /**
- * Adds a listener for publisher callbacks.
- *
- * @param listener Publisher callback listener
- */
- public void addPublisherListener(PublisherListener listener) {
- mPublisherListeners.add(listener);
- }
-
- /**
- * Adds a listener for subscriber callbacks.
- *
- * @param listener Subscriber callback listener
- */
- public void addSubscriberListener(SubscriberListener listener) {
- mSubscriberListeners.add(listener);
- }
-
- /**
- * Removes a listener for publisher callbacks.
- *
- * @param listener Publisher callback listener
- */
- public void removePublisherListener(PublisherListener listener) {
- mPublisherListeners.remove(listener);
- }
-
- /**
- * Removes a listener for subscriber callbacks.
- *
- * @param listener Subscriber callback listener
- */
- public void removeSubscriberListener(SubscriberListener listener) {
- mSubscriberListeners.remove(listener);
- }
-
- /**
- * Adds a subscription to all layers.
- *
- * @param subscriber Subscriber client to send layer data
- */
- public void addSubscription(IVmsSubscriberClient subscriber) {
- synchronized (mLock) {
- mRouting.addSubscription(subscriber);
- }
- }
-
- /**
- * Removes a subscription to all layers.
- *
- * @param subscriber Subscriber client to remove subscription for
- */
- public void removeSubscription(IVmsSubscriberClient subscriber) {
- synchronized (mLock) {
- mRouting.removeSubscription(subscriber);
- }
- }
-
- /**
- * Adds a layer subscription.
- *
- * @param subscriber Subscriber client to send layer data
- * @param layer Layer to send
- */
- public void addSubscription(IVmsSubscriberClient subscriber, VmsLayer layer) {
- boolean firstSubscriptionForLayer;
- if (DBG) Log.d(TAG, "Checking for first subscription. Layer: " + layer);
- synchronized (mLock) {
- // Check if publishers need to be notified about this change in subscriptions.
- firstSubscriptionForLayer = !mRouting.hasLayerSubscriptions(layer);
-
- // Add the listeners subscription to the layer
- mRouting.addSubscription(subscriber, layer);
- }
- if (firstSubscriptionForLayer) {
- notifyOfSubscriptionChange();
- }
- }
-
- /**
- * Removes a layer subscription.
- *
- * @param subscriber Subscriber client to remove subscription for
- * @param layer Layer to remove
- */
- public void removeSubscription(IVmsSubscriberClient subscriber, VmsLayer layer) {
- boolean layerHasSubscribers;
- synchronized (mLock) {
- if (!mRouting.hasLayerSubscriptions(layer)) {
- if (DBG) Log.d(TAG, "Trying to remove a layer with no subscription: " + layer);
- return;
- }
-
- // Remove the listeners subscription to the layer
- mRouting.removeSubscription(subscriber, layer);
-
- // Check if publishers need to be notified about this change in subscriptions.
- layerHasSubscribers = mRouting.hasLayerSubscriptions(layer);
- }
- if (!layerHasSubscribers) {
- notifyOfSubscriptionChange();
- }
- }
-
- /**
- * Adds a publisher-specific layer subscription.
- *
- * @param subscriber Subscriber client to send layer data
- * @param layer Layer to send
- * @param publisherId Publisher of layer
- */
- public void addSubscription(IVmsSubscriberClient subscriber, VmsLayer layer, int publisherId) {
- boolean firstSubscriptionForLayer;
- synchronized (mLock) {
- // Check if publishers need to be notified about this change in subscriptions.
- firstSubscriptionForLayer = !(mRouting.hasLayerSubscriptions(layer)
- || mRouting.hasLayerFromPublisherSubscriptions(layer, publisherId));
-
- // Add the listeners subscription to the layer
- mRouting.addSubscription(subscriber, layer, publisherId);
- }
- if (firstSubscriptionForLayer) {
- notifyOfSubscriptionChange();
- }
- }
-
- /**
- * Removes a publisher-specific layer subscription.
- *
- * @param subscriber Subscriber client to remove subscription for
- * @param layer Layer to remove
- * @param publisherId Publisher of layer
- */
- public void removeSubscription(IVmsSubscriberClient subscriber, VmsLayer layer,
- int publisherId) {
- boolean layerHasSubscribers;
- synchronized (mLock) {
- if (!mRouting.hasLayerFromPublisherSubscriptions(layer, publisherId)) {
- if (DBG) {
- Log.d(TAG, "Trying to remove a layer with no subscription: "
- + layer + ", publisher ID:" + publisherId);
- }
- return;
- }
-
- // Remove the listeners subscription to the layer
- mRouting.removeSubscription(subscriber, layer, publisherId);
-
- // Check if publishers need to be notified about this change in subscriptions.
- layerHasSubscribers = mRouting.hasLayerSubscriptions(layer)
- || mRouting.hasLayerFromPublisherSubscriptions(layer, publisherId);
- }
- if (!layerHasSubscribers) {
- notifyOfSubscriptionChange();
- }
- }
-
- /**
- * Removes a disconnected subscriber's subscriptions
- *
- * @param subscriber Subscriber that was disconnected
- */
- public void removeDeadSubscriber(IVmsSubscriberClient subscriber) {
- boolean subscriptionStateChanged;
- synchronized (mLock) {
- subscriptionStateChanged = mRouting.removeDeadSubscriber(subscriber);
- }
- if (subscriptionStateChanged) {
- notifyOfSubscriptionChange();
- }
- }
-
- /**
- * Gets all subscribers for a specific layer/publisher combination.
- *
- * @param layer Layer to query
- * @param publisherId Publisher of layer
- */
- public Set<IVmsSubscriberClient> getSubscribersForLayerFromPublisher(VmsLayer layer,
- int publisherId) {
- synchronized (mLock) {
- return mRouting.getSubscribersForLayerFromPublisher(layer, publisherId);
- }
- }
-
- /**
- * Gets the state of all layer subscriptions.
- */
- public VmsSubscriptionState getSubscriptionState() {
- synchronized (mLock) {
- return mRouting.getSubscriptionState();
- }
- }
-
- /**
- * Assigns an idempotent ID for publisherInfo and stores it. The idempotency in this case means
- * that the same publisherInfo will always, within a trip of the vehicle, return the same ID.
- * The publisherInfo should be static for a binary and should only change as part of a software
- * update. The publisherInfo is a serialized proto message which VMS clients can interpret.
- */
- public int getPublisherId(byte[] publisherInfo) {
- if (DBG) Log.i(TAG, "Getting publisher static ID");
- synchronized (mLock) {
- return mPublishersInfo.getIdForInfo(publisherInfo);
- }
- }
-
- /**
- * Gets the publisher information data registered in {@link #getPublisherId(byte[])}
- *
- * @param publisherId Publisher ID to query
- * @return Publisher information
- */
- public byte[] getPublisherInfo(int publisherId) {
- if (DBG) Log.i(TAG, "Getting information for publisher ID: " + publisherId);
- synchronized (mLock) {
- return mPublishersInfo.getPublisherInfo(publisherId);
- }
- }
-
- /**
- * Sets the layers offered by the publisher with the given publisher token.
- *
- * @param publisherToken Identifier token of publisher
- * @param offering Layers offered by publisher
- */
- public void setPublisherLayersOffering(IBinder publisherToken, VmsLayersOffering offering) {
- synchronized (mLock) {
- Map<Integer, VmsLayersOffering> publisherOfferings = mOfferings.computeIfAbsent(
- publisherToken, k -> new HashMap<>());
- publisherOfferings.put(offering.getPublisherId(), offering);
- updateLayerAvailability();
- }
- VmsOperationRecorder.get().setPublisherLayersOffering(offering);
- notifyOfAvailabilityChange();
- }
-
- /**
- * Removes a disconnected publisher's offerings
- *
- * @param publisherToken Identifier token of publisher to be removed
- */
- public void removeDeadPublisher(IBinder publisherToken) {
- synchronized (mLock) {
- mOfferings.remove(publisherToken);
- updateLayerAvailability();
- }
- notifyOfAvailabilityChange();
- }
-
- /**
- * Gets all layers available for subscription.
- *
- * @return All available layers
- */
- public VmsAvailableLayers getAvailableLayers() {
- synchronized (mLock) {
- return mAvailableLayers.getAvailableLayers();
- }
- }
-
- private void updateLayerAvailability() {
- Set<VmsLayersOffering> allPublisherOfferings = new HashSet<>();
- synchronized (mLock) {
- for (Map<Integer, VmsLayersOffering> offerings : mOfferings.values()) {
- allPublisherOfferings.addAll(offerings.values());
- }
- if (DBG) Log.d(TAG, "New layer availability: " + allPublisherOfferings);
- mAvailableLayers.setPublishersOffering(allPublisherOfferings);
- }
- }
-
- private void notifyOfSubscriptionChange() {
- VmsSubscriptionState subscriptionState = getSubscriptionState();
- Log.i(TAG, "Notifying publishers of subscriptions: " + subscriptionState);
- // Notify the App publishers
- for (PublisherListener listener : mPublisherListeners) {
- listener.onSubscriptionChange(subscriptionState);
- }
- }
-
- private void notifyOfAvailabilityChange() {
- VmsAvailableLayers availableLayers = getAvailableLayers();
- Log.i(TAG, "Notifying subscribers of layers availability: " + availableLayers);
- // Notify the App subscribers
- for (SubscriberListener listener : mSubscriberListeners) {
- listener.onLayersAvailabilityChange(availableLayers);
- }
- }
-}
diff --git a/service/src/com/android/car/vms/VmsClientManager.java b/service/src/com/android/car/vms/VmsClientManager.java
deleted file mode 100644
index 30fe1b2..0000000
--- a/service/src/com/android/car/vms/VmsClientManager.java
+++ /dev/null
@@ -1,610 +0,0 @@
-/*
- * Copyright (C) 2019 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.car.vms;
-
-import android.car.Car;
-import android.car.vms.IVmsPublisherClient;
-import android.car.vms.IVmsSubscriberClient;
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.Intent;
-import android.content.ServiceConnection;
-import android.content.pm.PackageManager;
-import android.content.pm.ServiceInfo;
-import android.os.Binder;
-import android.os.Handler;
-import android.os.IBinder;
-import android.os.Looper;
-import android.os.Process;
-import android.os.RemoteException;
-import android.os.UserHandle;
-import android.os.UserManager;
-import android.util.ArrayMap;
-import android.util.Log;
-
-import com.android.car.CarServiceBase;
-import com.android.car.R;
-import com.android.car.VmsPublisherService;
-import com.android.car.hal.VmsHalService;
-import com.android.car.stats.CarStatsService;
-import com.android.car.stats.VmsClientLogger;
-import com.android.car.stats.VmsClientLogger.ConnectionState;
-import com.android.car.user.CarUserService;
-import com.android.internal.annotations.GuardedBy;
-import com.android.internal.annotations.VisibleForTesting;
-
-import java.io.PrintWriter;
-import java.util.Collection;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.function.IntSupplier;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-/**
- * Manages service connections lifecycle for VMS publisher clients.
- *
- * Binds to system-level clients at boot and creates/destroys bindings for userspace clients
- * according to the Android user lifecycle.
- */
-public class VmsClientManager implements CarServiceBase {
- private static final boolean DBG = false;
- private static final String TAG = "VmsClientManager";
- private static final String HAL_CLIENT_NAME = "HalClient";
- private static final String UNKNOWN_PACKAGE = "UnknownPackage";
-
- private final Context mContext;
- private final PackageManager mPackageManager;
- private final UserManager mUserManager;
- private final CarUserService mUserService;
- private final CarStatsService mStatsService;
- private final Handler mHandler;
- private final IntSupplier mGetCallingUid;
- private final int mMillisBeforeRebind;
-
- private final Object mLock = new Object();
-
- @GuardedBy("mLock")
- private final VmsBrokerService mBrokerService;
- @GuardedBy("mLock")
- private VmsPublisherService mPublisherService;
-
- @GuardedBy("mLock")
- private final Map<String, PublisherConnection> mSystemClients = new ArrayMap<>();
- @GuardedBy("mLock")
- private IVmsPublisherClient mHalClient;
- @GuardedBy("mLock")
- private boolean mSystemUserUnlocked;
-
- @GuardedBy("mLock")
- private final Map<String, PublisherConnection> mCurrentUserClients = new ArrayMap<>();
- @GuardedBy("mLock")
- private int mCurrentUser;
-
- @GuardedBy("mLock")
- private final Map<IBinder, SubscriberConnection> mSubscribers = new ArrayMap<>();
-
- private final Runnable mSystemUserUnlockedListener = () -> {
- synchronized (mLock) {
- mSystemUserUnlocked = true;
- }
- bindToSystemClients();
- };
-
- private final CarUserService.UserCallback mUserCallback = new CarUserService.UserCallback() {
- @Override
- public void onSwitchUser(int userId) {
- synchronized (mLock) {
- if (mCurrentUser != userId) {
- mCurrentUser = userId;
- terminate(mCurrentUserClients);
- terminate(mSubscribers.values().stream()
- .filter(subscriber -> subscriber.mUserId != mCurrentUser)
- .filter(subscriber -> subscriber.mUserId != UserHandle.USER_SYSTEM));
- }
- }
- bindToUserClients();
- }
-
- @Override
- public void onUserLockChanged(int userId, boolean unlocked) {
- synchronized (mLock) {
- if (mCurrentUser == userId && unlocked) {
- bindToUserClients();
- }
- }
- }
- };
-
- /**
- * Constructor for client manager.
- *
- * @param context Context to use for registering receivers and binding services.
- * @param statsService Service for logging client metrics.
- * @param userService User service for registering system unlock listener.
- * @param brokerService Service managing the VMS publisher/subscriber state.
- * @param halService Service providing the HAL client interface
- */
- public VmsClientManager(Context context, CarStatsService statsService,
- CarUserService userService, VmsBrokerService brokerService,
- VmsHalService halService) {
- this(context, statsService, userService, brokerService, halService,
- new Handler(Looper.getMainLooper()), Binder::getCallingUid);
- }
-
- @VisibleForTesting
- VmsClientManager(Context context, CarStatsService statsService,
- CarUserService userService, VmsBrokerService brokerService,
- VmsHalService halService, Handler handler, IntSupplier getCallingUid) {
- mContext = context;
- mPackageManager = context.getPackageManager();
- mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
- mStatsService = statsService;
- mUserService = userService;
- mCurrentUser = UserHandle.USER_NULL;
- mBrokerService = brokerService;
- mHandler = handler;
- mGetCallingUid = getCallingUid;
- mMillisBeforeRebind = context.getResources().getInteger(
- com.android.car.R.integer.millisecondsBeforeRebindToVmsPublisher);
- }
-
- /**
- * Registers the publisher service for connection callbacks.
- *
- * @param publisherService Publisher service to register.
- */
- public void setPublisherService(VmsPublisherService publisherService) {
- synchronized (mLock) {
- mPublisherService = publisherService;
- }
- }
-
- @Override
- public void init() {
- mUserService.runOnUser0Unlock(mSystemUserUnlockedListener);
- mUserService.addUserCallback(mUserCallback);
- }
-
- @Override
- public void release() {
- mUserService.removeUserCallback(mUserCallback);
- synchronized (mLock) {
- if (mHalClient != null) {
- mPublisherService.onClientDisconnected(HAL_CLIENT_NAME);
- }
- terminate(mSystemClients);
- terminate(mCurrentUserClients);
- terminate(mSubscribers.values().stream());
- }
- }
-
- @Override
- public void dump(PrintWriter writer) {
- writer.println("*" + getClass().getSimpleName() + "*");
- synchronized (mLock) {
- writer.println("mCurrentUser:" + mCurrentUser);
- writer.println("mHalClient: " + (mHalClient != null ? "connected" : "disconnected"));
- writer.println("mSystemClients:");
- dumpConnections(writer, mSystemClients);
-
- writer.println("mCurrentUserClients:");
- dumpConnections(writer, mCurrentUserClients);
-
- writer.println("mSubscribers:");
- for (SubscriberConnection subscriber : mSubscribers.values()) {
- writer.printf("\t%s\n", subscriber);
- }
- }
- }
-
-
- /**
- * Adds a subscriber for connection tracking.
- *
- * @param subscriberClient Subscriber client to track.
- */
- public void addSubscriber(IVmsSubscriberClient subscriberClient) {
- if (subscriberClient == null) {
- Log.e(TAG, "Trying to add a null subscriber: "
- + getCallingPackage(mGetCallingUid.getAsInt()));
- throw new IllegalArgumentException("subscriber cannot be null.");
- }
-
- synchronized (mLock) {
- IBinder subscriberBinder = subscriberClient.asBinder();
- if (mSubscribers.containsKey(subscriberBinder)) {
- // Already registered
- return;
- }
-
- int callingUid = mGetCallingUid.getAsInt();
- int subscriberUserId = UserHandle.getUserId(callingUid);
- if (subscriberUserId != mCurrentUser && subscriberUserId != UserHandle.USER_SYSTEM) {
- throw new SecurityException("Caller must be foreground user or system");
- }
-
- SubscriberConnection subscriber = new SubscriberConnection(
- subscriberClient, callingUid, getCallingPackage(callingUid), subscriberUserId);
- if (DBG) Log.d(TAG, "Registering subscriber: " + subscriber);
- try {
- subscriberBinder.linkToDeath(subscriber, 0);
- } catch (RemoteException e) {
- throw new IllegalStateException("Subscriber already dead: " + subscriber, e);
- }
- mSubscribers.put(subscriberBinder, subscriber);
- }
- }
-
- /**
- * Removes a subscriber for connection tracking and expires its subscriptions.
- *
- * @param subscriberClient Subscriber client to remove.
- */
- public void removeSubscriber(IVmsSubscriberClient subscriberClient) {
- synchronized (mLock) {
- SubscriberConnection subscriber = mSubscribers.get(subscriberClient.asBinder());
- if (subscriber != null) {
- subscriber.terminate();
- }
- }
- }
-
- /**
- * Returns all active subscriber clients.
- */
- public Collection<IVmsSubscriberClient> getAllSubscribers() {
- synchronized (mLock) {
- return mSubscribers.values().stream()
- .map(subscriber -> subscriber.mClient)
- .collect(Collectors.toList());
- }
- }
-
- /**
- * Gets the application UID associated with a subscriber client.
- */
- public int getSubscriberUid(IVmsSubscriberClient subscriberClient) {
- synchronized (mLock) {
- SubscriberConnection subscriber = mSubscribers.get(subscriberClient.asBinder());
- return subscriber != null ? subscriber.mUid : Process.INVALID_UID;
- }
- }
-
- /**
- * Gets the package name for a given subscriber client.
- */
- public String getPackageName(IVmsSubscriberClient subscriberClient) {
- synchronized (mLock) {
- SubscriberConnection subscriber = mSubscribers.get(subscriberClient.asBinder());
- return subscriber != null ? subscriber.mPackageName : UNKNOWN_PACKAGE;
- }
- }
-
- /**
- * Registers the HAL client connections.
- */
- public void onHalConnected(IVmsPublisherClient publisherClient,
- IVmsSubscriberClient subscriberClient) {
- synchronized (mLock) {
- mHalClient = publisherClient;
- mPublisherService.onClientConnected(HAL_CLIENT_NAME, mHalClient);
- mSubscribers.put(subscriberClient.asBinder(),
- new SubscriberConnection(subscriberClient, Process.myUid(), HAL_CLIENT_NAME,
- UserHandle.USER_SYSTEM));
- }
- mStatsService.getVmsClientLogger(Process.myUid())
- .logConnectionState(ConnectionState.CONNECTED);
- }
-
- /**
- *
- */
- public void onHalDisconnected() {
- synchronized (mLock) {
- if (mHalClient != null) {
- mPublisherService.onClientDisconnected(HAL_CLIENT_NAME);
- mStatsService.getVmsClientLogger(Process.myUid())
- .logConnectionState(ConnectionState.DISCONNECTED);
- }
- mHalClient = null;
- terminate(mSubscribers.values().stream()
- .filter(subscriber -> HAL_CLIENT_NAME.equals(subscriber.mPackageName)));
- }
- }
-
- private void dumpConnections(PrintWriter writer,
- Map<String, PublisherConnection> connectionMap) {
- for (PublisherConnection connection : connectionMap.values()) {
- writer.printf("\t%s: %s\n",
- connection.mName.getPackageName(),
- connection.mIsBound ? "connected" : "disconnected");
- }
- }
-
- private void bindToSystemClients() {
- String[] clientNames = mContext.getResources().getStringArray(
- R.array.vmsPublisherSystemClients);
- synchronized (mLock) {
- if (!mSystemUserUnlocked) {
- return;
- }
- Log.i(TAG, "Attempting to bind " + clientNames.length + " system client(s)");
- for (String clientName : clientNames) {
- bind(mSystemClients, clientName, UserHandle.SYSTEM);
- }
- }
- }
-
- private void bindToUserClients() {
- bindToSystemClients(); // Bind system clients on user switch, if they are not already bound.
- synchronized (mLock) {
- if (mCurrentUser == UserHandle.USER_NULL) {
- Log.e(TAG, "Unknown user in foreground.");
- return;
- }
- // To avoid the risk of double-binding, clients running as the system user must only
- // ever be bound in bindToSystemClients().
- if (mCurrentUser == UserHandle.USER_SYSTEM) {
- Log.e(TAG, "System user in foreground. Userspace clients will not be bound.");
- return;
- }
-
- if (!mUserManager.isUserUnlockingOrUnlocked(mCurrentUser)) {
- Log.i(TAG, "Waiting for foreground user " + mCurrentUser + " to be unlocked.");
- return;
- }
-
- String[] clientNames = mContext.getResources().getStringArray(
- R.array.vmsPublisherUserClients);
- Log.i(TAG, "Attempting to bind " + clientNames.length + " user client(s)");
- UserHandle currentUserHandle = UserHandle.of(mCurrentUser);
- for (String clientName : clientNames) {
- bind(mCurrentUserClients, clientName, currentUserHandle);
- }
- }
- }
-
- private void bind(Map<String, PublisherConnection> connectionMap, String clientName,
- UserHandle userHandle) {
- if (connectionMap.containsKey(clientName)) {
- Log.i(TAG, "Already bound: " + clientName);
- return;
- }
-
- ComponentName name = ComponentName.unflattenFromString(clientName);
- if (name == null) {
- Log.e(TAG, "Invalid client name: " + clientName);
- return;
- }
-
- ServiceInfo serviceInfo;
- try {
- serviceInfo = mContext.getPackageManager().getServiceInfo(name,
- PackageManager.MATCH_DIRECT_BOOT_AUTO);
- } catch (PackageManager.NameNotFoundException e) {
- Log.w(TAG, "Client not installed: " + clientName);
- return;
- }
-
- VmsClientLogger statsLog = mStatsService.getVmsClientLogger(
- UserHandle.getUid(userHandle.getIdentifier(), serviceInfo.applicationInfo.uid));
-
- if (!Car.PERMISSION_BIND_VMS_CLIENT.equals(serviceInfo.permission)) {
- Log.e(TAG, "Client service: " + clientName
- + " does not require " + Car.PERMISSION_BIND_VMS_CLIENT + " permission");
- statsLog.logConnectionState(ConnectionState.CONNECTION_ERROR);
- return;
- }
-
- PublisherConnection connection = new PublisherConnection(name, userHandle, statsLog);
- if (connection.bind()) {
- Log.i(TAG, "Client bound: " + connection);
- connectionMap.put(clientName, connection);
- } else {
- Log.e(TAG, "Binding failed: " + connection);
- }
- }
-
- private void terminate(Map<String, PublisherConnection> connectionMap) {
- connectionMap.values().forEach(PublisherConnection::terminate);
- connectionMap.clear();
- }
-
- class PublisherConnection implements ServiceConnection {
- private final ComponentName mName;
- private final UserHandle mUser;
- private final String mFullName;
- private final VmsClientLogger mStatsLog;
- private boolean mIsBound = false;
- private boolean mIsTerminated = false;
- private boolean mRebindScheduled = false;
- private IVmsPublisherClient mClientService;
-
- PublisherConnection(ComponentName name, UserHandle user, VmsClientLogger statsLog) {
- mName = name;
- mUser = user;
- mFullName = mName.flattenToString() + " U=" + mUser.getIdentifier();
- mStatsLog = statsLog;
- }
-
- synchronized boolean bind() {
- if (mIsBound) {
- return true;
- }
- if (mIsTerminated) {
- return false;
- }
- mStatsLog.logConnectionState(ConnectionState.CONNECTING);
-
- if (DBG) Log.d(TAG, "binding: " + mFullName);
- Intent intent = new Intent();
- intent.setComponent(mName);
- try {
- mIsBound = mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE,
- mHandler, mUser);
- } catch (SecurityException e) {
- Log.e(TAG, "While binding " + mFullName, e);
- }
-
- if (!mIsBound) {
- mStatsLog.logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- return mIsBound;
- }
-
- synchronized void unbind() {
- if (!mIsBound) {
- return;
- }
-
- if (DBG) Log.d(TAG, "unbinding: " + mFullName);
- try {
- mContext.unbindService(this);
- } catch (Throwable t) {
- Log.e(TAG, "While unbinding " + mFullName, t);
- }
- mIsBound = false;
- }
-
- synchronized void scheduleRebind() {
- if (mRebindScheduled) {
- return;
- }
-
- if (DBG) {
- Log.d(TAG,
- String.format("rebinding %s after %dms", mFullName, mMillisBeforeRebind));
- }
- mHandler.postDelayed(this::doRebind, mMillisBeforeRebind);
- mRebindScheduled = true;
- }
-
- synchronized void doRebind() {
- mRebindScheduled = false;
- // Do not rebind if the connection has been terminated, or the client service has
- // reconnected on its own.
- if (mIsTerminated || mClientService != null) {
- return;
- }
-
- Log.i(TAG, "Rebinding: " + mFullName);
- // Ensure that the client is not bound before attempting to rebind.
- // If the client is not currently bound, unbind() will have no effect.
- unbind();
- bind();
- }
-
- synchronized void terminate() {
- if (DBG) Log.d(TAG, "terminating: " + mFullName);
- mIsTerminated = true;
- notifyOnDisconnect(ConnectionState.TERMINATED);
- unbind();
- }
-
- synchronized void notifyOnDisconnect(int connectionState) {
- if (mClientService != null) {
- mPublisherService.onClientDisconnected(mFullName);
- mClientService = null;
- mStatsLog.logConnectionState(connectionState);
- }
- }
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- if (DBG) Log.d(TAG, "onServiceConnected: " + mFullName);
- mClientService = IVmsPublisherClient.Stub.asInterface(service);
- mPublisherService.onClientConnected(mFullName, mClientService);
- mStatsLog.logConnectionState(ConnectionState.CONNECTED);
- }
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- if (DBG) Log.d(TAG, "onServiceDisconnected: " + mFullName);
- notifyOnDisconnect(ConnectionState.DISCONNECTED);
- scheduleRebind();
- }
-
- @Override
- public void onBindingDied(ComponentName name) {
- if (DBG) Log.d(TAG, "onBindingDied: " + mFullName);
- notifyOnDisconnect(ConnectionState.DISCONNECTED);
- scheduleRebind();
- }
-
- @Override
- public String toString() {
- return mFullName;
- }
- }
-
- private void terminate(Stream<SubscriberConnection> subscribers) {
- // Make a copy of the stream, so that terminate() doesn't cause a concurrent modification
- subscribers.collect(Collectors.toList()).forEach(SubscriberConnection::terminate);
- }
-
- // If we're in a binder call, returns back the package name of the caller of the binder call.
- private String getCallingPackage(int uid) {
- String packageName = mPackageManager.getNameForUid(uid);
- if (packageName == null) {
- return UNKNOWN_PACKAGE;
- } else {
- return packageName;
- }
- }
-
- private class SubscriberConnection implements IBinder.DeathRecipient {
- private final IVmsSubscriberClient mClient;
- private final int mUid;
- private final String mPackageName;
- private final int mUserId;
-
- SubscriberConnection(IVmsSubscriberClient subscriberClient, int uid, String packageName,
- int userId) {
- mClient = subscriberClient;
- mUid = uid;
- mPackageName = packageName;
- mUserId = userId;
- }
-
- @Override
- public void binderDied() {
- if (DBG) Log.d(TAG, "Subscriber died: " + this);
- terminate();
- }
-
- @Override
- public String toString() {
- return mPackageName + " U=" + mUserId;
- }
-
- void terminate() {
- if (DBG) Log.d(TAG, "Terminating subscriber: " + this);
- synchronized (mLock) {
- mBrokerService.removeDeadSubscriber(mClient);
- IBinder subscriberBinder = mClient.asBinder();
- try {
- subscriberBinder.unlinkToDeath(this, 0);
- } catch (NoSuchElementException e) {
- if (DBG) Log.d(TAG, "While unlinking subscriber binder for " + this, e);
- }
- mSubscribers.remove(subscriberBinder);
- }
- }
- }
-}
diff --git a/tests/carservice_unit_test/src/com/android/car/VmsPublisherServiceTest.java b/tests/carservice_unit_test/src/com/android/car/VmsPublisherServiceTest.java
deleted file mode 100644
index dc60c1f..0000000
--- a/tests/carservice_unit_test/src/com/android/car/VmsPublisherServiceTest.java
+++ /dev/null
@@ -1,366 +0,0 @@
-/*
- * Copyright (C) 2019 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.car;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
-
-import android.car.Car;
-import android.car.vms.IVmsPublisherClient;
-import android.car.vms.IVmsPublisherService;
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsLayersOffering;
-import android.car.vms.VmsSubscriptionState;
-import android.content.Context;
-import android.content.pm.PackageManager;
-import android.os.Binder;
-import android.os.IBinder;
-import android.os.RemoteException;
-
-import androidx.test.filters.SmallTest;
-
-import com.android.car.stats.CarStatsService;
-import com.android.car.stats.VmsClientLogger;
-import com.android.car.vms.VmsBrokerService;
-import com.android.car.vms.VmsClientManager;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-
-@RunWith(MockitoJUnitRunner.class)
-@SmallTest
-public class VmsPublisherServiceTest {
- private static final VmsSubscriptionState SUBSCRIPTION_STATE = new VmsSubscriptionState(12345,
- Collections.emptySet(), Collections.emptySet());
- private static final VmsLayersOffering OFFERING = new VmsLayersOffering(Collections.emptySet(),
- 54321);
- private static final VmsLayer LAYER = new VmsLayer(1, 2, 3);
-
- private static final int PUBLISHER_ID = 54321;
- private static final byte[] PAYLOAD = new byte[]{1, 2, 3, 4};
-
- private static final int PUBLISHER_UID = 10100;
- private static final int SUBSCRIBER_UID = 10101;
- private static final int SUBSCRIBER_UID2 = 10102;
- private static final int NO_SUBSCRIBERS_UID = -1;
-
- @Mock
- private Context mContext;
- @Mock
- private CarStatsService mStatsService;
- @Mock
- private VmsBrokerService mBrokerService;
- @Captor
- private ArgumentCaptor<VmsBrokerService.PublisherListener> mProxyCaptor;
- @Mock
- private VmsClientManager mClientManager;
-
- @Mock
- private VmsClientLogger mPublisherLog;
- @Mock
- private VmsClientLogger mSubscriberLog;
- @Mock
- private VmsClientLogger mSubscriberLog2;
- @Mock
- private VmsClientLogger mNoSubscribersLog;
-
- @Mock
- private IVmsSubscriberClient mSubscriberClient;
- @Mock
- private IVmsSubscriberClient mSubscriberClient2;
-
- private VmsPublisherService mPublisherService;
- private MockPublisherClient mPublisherClient;
- private MockPublisherClient mPublisherClient2;
-
- @Before
- public void setUp() {
- mPublisherService = new VmsPublisherService(mContext, mStatsService, mBrokerService,
- mClientManager, () -> PUBLISHER_UID);
- verify(mClientManager).setPublisherService(mPublisherService);
-
- when(mClientManager.getSubscriberUid(mSubscriberClient)).thenReturn(SUBSCRIBER_UID);
- when(mClientManager.getSubscriberUid(mSubscriberClient2)).thenReturn(SUBSCRIBER_UID2);
-
- when(mStatsService.getVmsClientLogger(PUBLISHER_UID)).thenReturn(mPublisherLog);
- when(mStatsService.getVmsClientLogger(SUBSCRIBER_UID)).thenReturn(mSubscriberLog);
- when(mStatsService.getVmsClientLogger(SUBSCRIBER_UID2)).thenReturn(mSubscriberLog2);
- when(mStatsService.getVmsClientLogger(NO_SUBSCRIBERS_UID)).thenReturn(mNoSubscribersLog);
-
- mPublisherClient = new MockPublisherClient();
- mPublisherClient2 = new MockPublisherClient();
- when(mBrokerService.getSubscribersForLayerFromPublisher(LAYER, PUBLISHER_ID))
- .thenReturn(new HashSet<>(Arrays.asList(mSubscriberClient, mSubscriberClient2)));
- }
-
- @After
- public void tearDown() {
- verifyNoMoreInteractions(mPublisherLog, mSubscriberLog, mSubscriberLog2, mNoSubscribersLog);
- }
-
- @Test
- public void testInit() {
- mPublisherService.init();
- }
-
- @Test
- public void testOnClientConnected() {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- mPublisherService.onClientConnected("SomeOtherClient", mPublisherClient2);
- verify(mBrokerService, times(2)).addPublisherListener(mProxyCaptor.capture());
-
- assertNotNull(mPublisherClient.mPublisherService);
- assertSame(mPublisherClient.mPublisherService, mProxyCaptor.getAllValues().get(0));
-
- assertNotNull(mPublisherClient2.mPublisherService);
- assertSame(mPublisherClient2.mPublisherService, mProxyCaptor.getAllValues().get(1));
- assertNotSame(mPublisherClient2.mPublisherService, mPublisherClient.mPublisherService);
- }
-
- @Test
- public void testOnClientDisconnected() {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- mPublisherService.onClientConnected("SomeOtherClient", mPublisherClient2);
- verify(mBrokerService, times(2)).addPublisherListener(mProxyCaptor.capture());
-
- reset(mClientManager, mBrokerService);
- mPublisherService.onClientDisconnected("SomeClient");
-
- verify(mBrokerService).removeDeadPublisher(mPublisherClient.mToken);
- verify(mBrokerService).removePublisherListener(mProxyCaptor.getAllValues().get(0));
- verifyNoMoreInteractions(mBrokerService);
- }
-
- @Test
- public void testSetLayersOffering() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
-
- mPublisherClient.mPublisherService.setLayersOffering(mPublisherClient.mToken, OFFERING);
- verify(mBrokerService).setPublisherLayersOffering(mPublisherClient.mToken, OFFERING);
- }
-
- @Test(expected = SecurityException.class)
- public void testSetLayersOffering_InvalidToken() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
-
- mPublisherClient.mPublisherService.setLayersOffering(new Binder(), OFFERING);
- }
-
- @Test(expected = SecurityException.class)
- public void testSetLayersOffering_Disconnected() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- mPublisherService.onClientDisconnected("SomeClient");
-
- mPublisherClient.mPublisherService.setLayersOffering(mPublisherClient.mToken, OFFERING);
- }
-
- @Test(expected = SecurityException.class)
- public void testSetLayersOffering_PermissionDenied() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- when(mContext.checkCallingOrSelfPermission(Car.PERMISSION_VMS_PUBLISHER)).thenReturn(
- PackageManager.PERMISSION_DENIED);
-
- mPublisherClient.mPublisherService.setLayersOffering(mPublisherClient.mToken, OFFERING);
- }
-
- @Test
- public void testPublish() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
-
- mPublisherClient.mPublisherService.publish(mPublisherClient.mToken, LAYER, PUBLISHER_ID,
- PAYLOAD);
- verify(mSubscriberClient).onVmsMessageReceived(LAYER, PAYLOAD);
- verify(mSubscriberClient2).onVmsMessageReceived(LAYER, PAYLOAD);
-
- verify(mPublisherLog).logPacketSent(LAYER, PAYLOAD.length);
- verify(mSubscriberLog).logPacketReceived(LAYER, PAYLOAD.length);
- verify(mSubscriberLog2).logPacketReceived(LAYER, PAYLOAD.length);
- }
-
- @Test
- public void testPublishNullLayerAndNullPayload() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
-
- // We just want to ensure that no exceptions are thrown here.
- mPublisherClient.mPublisherService.publish(mPublisherClient.mToken, null, PUBLISHER_ID,
- null);
- }
-
- @Test
- public void testPublish_NoSubscribers() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- when(mBrokerService.getSubscribersForLayerFromPublisher(LAYER, PUBLISHER_ID))
- .thenReturn(Collections.emptySet());
-
- mPublisherClient.mPublisherService.publish(mPublisherClient.mToken, LAYER, PUBLISHER_ID,
- PAYLOAD);
-
- verify(mPublisherLog).logPacketSent(LAYER, PAYLOAD.length);
- verify(mNoSubscribersLog).logPacketDropped(LAYER, PAYLOAD.length);
- }
-
- @Test
- public void testPublish_ClientError() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- doThrow(new RemoteException()).when(mSubscriberClient).onVmsMessageReceived(LAYER, PAYLOAD);
-
- mPublisherClient.mPublisherService.publish(mPublisherClient.mToken, LAYER, PUBLISHER_ID,
- PAYLOAD);
- verify(mSubscriberClient).onVmsMessageReceived(LAYER, PAYLOAD);
- verify(mSubscriberClient2).onVmsMessageReceived(LAYER, PAYLOAD);
-
- verify(mPublisherLog).logPacketSent(LAYER, PAYLOAD.length);
- verify(mSubscriberLog).logPacketDropped(LAYER, PAYLOAD.length);
- verify(mSubscriberLog2).logPacketReceived(LAYER, PAYLOAD.length);
- }
-
- @Test(expected = SecurityException.class)
- public void testPublish_InvalidToken() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
-
- mPublisherClient.mPublisherService.publish(new Binder(), LAYER, PUBLISHER_ID, PAYLOAD);
- }
-
- @Test(expected = SecurityException.class)
- public void testPublish_Disconnected() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- mPublisherService.onClientDisconnected("SomeClient");
-
- mPublisherClient.mPublisherService.publish(mPublisherClient.mToken, LAYER, PUBLISHER_ID,
- PAYLOAD);
- }
-
- @Test(expected = SecurityException.class)
- public void testPublish_PermissionDenied() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- when(mContext.checkCallingOrSelfPermission(Car.PERMISSION_VMS_PUBLISHER)).thenReturn(
- PackageManager.PERMISSION_DENIED);
-
- mPublisherClient.mPublisherService.publish(mPublisherClient.mToken, LAYER, PUBLISHER_ID,
- PAYLOAD);
- }
-
- @Test
- public void testGetSubscriptions() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- when(mBrokerService.getSubscriptionState()).thenReturn(SUBSCRIPTION_STATE);
-
- assertEquals(SUBSCRIPTION_STATE, mPublisherClient.mPublisherService.getSubscriptions());
- }
-
- @Test(expected = SecurityException.class)
- public void testGetSubscriptions_Disconnected() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- mPublisherService.onClientDisconnected("SomeClient");
-
- mPublisherClient.mPublisherService.getSubscriptions();
- }
-
- @Test(expected = SecurityException.class)
- public void testGetSubscriptions_PermissionDenied() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- when(mContext.checkCallingOrSelfPermission(Car.PERMISSION_VMS_PUBLISHER)).thenReturn(
- PackageManager.PERMISSION_DENIED);
-
- mPublisherClient.mPublisherService.getSubscriptions();
- }
-
- @Test
- public void testGetPublisherId() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- when(mBrokerService.getPublisherId(PAYLOAD)).thenReturn(PUBLISHER_ID);
-
- assertEquals(PUBLISHER_ID, mPublisherClient.mPublisherService.getPublisherId(PAYLOAD));
- }
-
- @Test(expected = SecurityException.class)
- public void testGetPublisherId_Disconnected() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- mPublisherService.onClientDisconnected("SomeClient");
-
- mPublisherClient.mPublisherService.getPublisherId(PAYLOAD);
- }
-
- @Test(expected = SecurityException.class)
- public void testGetPublisherId_PermissionDenied() throws Exception {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- when(mContext.checkCallingOrSelfPermission(Car.PERMISSION_VMS_PUBLISHER)).thenReturn(
- PackageManager.PERMISSION_DENIED);
-
- mPublisherClient.mPublisherService.getPublisherId(PAYLOAD);
- }
-
- @Test
- public void testOnSubscriptionChange() {
- mPublisherService.onClientConnected("SomeClient", mPublisherClient);
- mPublisherService.onClientConnected("SomeOtherClient", mPublisherClient2);
- verify(mBrokerService, times(2)).addPublisherListener(mProxyCaptor.capture());
-
- mProxyCaptor.getAllValues().get(0).onSubscriptionChange(SUBSCRIPTION_STATE);
-
- assertEquals(SUBSCRIPTION_STATE, mPublisherClient.mSubscriptionState);
- assertNull(mPublisherClient2.mSubscriptionState);
- }
-
- @Test
- public void testRelease() {
- mPublisherService.release();
- }
-
- private class MockPublisherClient extends IVmsPublisherClient.Stub {
- private IBinder mToken;
- private IVmsPublisherService mPublisherService;
- private VmsSubscriptionState mSubscriptionState;
-
- @Override
- public void setVmsPublisherService(IBinder token, IVmsPublisherService service) {
- assertNotNull(token);
- assertNotNull(service);
- if (mToken != null) {
- throw new IllegalArgumentException("Publisher service set multiple times");
- }
- this.mToken = token;
- this.mPublisherService = service;
- }
-
- @Override
- public void onVmsSubscriptionChange(VmsSubscriptionState subscriptionState) {
- assertNotNull(subscriptionState);
- this.mSubscriptionState = subscriptionState;
- }
- }
-}
diff --git a/tests/carservice_unit_test/src/com/android/car/VmsRoutingTest.java b/tests/carservice_unit_test/src/com/android/car/VmsRoutingTest.java
deleted file mode 100644
index 9e516a8..0000000
--- a/tests/carservice_unit_test/src/com/android/car/VmsRoutingTest.java
+++ /dev/null
@@ -1,892 +0,0 @@
-/*
- * Copyright (C) 2017 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.car;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsAssociatedLayer;
-import android.car.vms.VmsAvailableLayers;
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsSubscriptionState;
-
-import androidx.test.filters.SmallTest;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-
-@SmallTest
-public class VmsRoutingTest {
- private static final VmsLayer LAYER_1 = new VmsLayer(1, 1, 2);
- private static final VmsLayer LAYER_2 = new VmsLayer(1, 3, 3);
- private static final VmsLayer[] LAYERS = {LAYER_1, LAYER_2};
- private static final int PUBLISHER_ID_1 = 123;
- private static final int PUBLISHER_ID_2 = 456;
- private static final int[] PUBLISHER_IDS = {PUBLISHER_ID_1, PUBLISHER_ID_2};
-
- private VmsRouting mRouting;
- private IVmsSubscriberClient mSubscriber;
- private IVmsSubscriberClient mSubscriberRewrapped;
- private IVmsSubscriberClient mSubscriber2;
-
- @Before
- public void setUp() throws Exception {
- mRouting = new VmsRouting();
- mSubscriber = new MockVmsSubscriber();
- mSubscriberRewrapped = IVmsSubscriberClient.Stub.asInterface(mSubscriber.asBinder());
- mSubscriber2 = new MockVmsSubscriber();
- }
-
- @Test
- public void testDefaultSubscriptionState() {
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPassiveSubscriber() {
- mRouting.addSubscription(mSubscriber);
-
- // Receives messages for all layers and publishers
- assertSubscribers(LAYER_1, mSubscriber);
- assertSubscribers(LAYER_2, mSubscriber);
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPassiveSubscriber_MultipleTimes() {
- mRouting.addSubscription(mSubscriber);
- mRouting.addSubscription(mSubscriber);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertSubscribers(LAYER_2, mSubscriber);
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPassiveSubscriber_MultipleTimes_Rewrapped() {
- mRouting.addSubscription(mSubscriber);
- mRouting.addSubscription(mSubscriberRewrapped);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertSubscribers(LAYER_2, mSubscriber);
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPassiveSubscriber_MultipleSubscribers() {
- mRouting.addSubscription(mSubscriber);
- mRouting.addSubscription(mSubscriber2);
-
- assertSubscribers(LAYER_1, mSubscriber, mSubscriber2);
- assertSubscribers(LAYER_2, mSubscriber, mSubscriber2);
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePassiveSubscriber() {
- mRouting.addSubscription(mSubscriber);
- mRouting.removeSubscription(mSubscriber);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePassiveSubscriber_NoSubscriptions() {
- mRouting.removeSubscription(mSubscriber);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePassiveSubscriber_MultipleTimes() {
- mRouting.addSubscription(mSubscriber);
- mRouting.removeSubscription(mSubscriber);
- mRouting.removeSubscription(mSubscriber);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePassiveSubscriber_Rewrapped() {
- mRouting.addSubscription(mSubscriber);
- mRouting.removeSubscription(mSubscriberRewrapped);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePassiveSubscriber_UnknownSubscriber() {
- mRouting.addSubscription(mSubscriber);
- mRouting.removeSubscription(mSubscriber2);
-
- assertSubscribers(mSubscriber);
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePassiveSubscriber_MultipleSubscribers() {
- mRouting.addSubscription(mSubscriber);
- mRouting.addSubscription(mSubscriber2);
- mRouting.removeSubscription(mSubscriber2);
-
- assertSubscribers(mSubscriber);
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddLayerSubscriber() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddLayerSubscriber_MultipleTimes() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriber, LAYER_1);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddLayerSubscriber_MultipleTimes_Rewrapped() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriberRewrapped, LAYER_1);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddLayerSubscriber_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriber, LAYER_2);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertSubscribers(LAYER_2, mSubscriber);
-
- assertEquals(
- new VmsSubscriptionState(2,
- new HashSet<>(Arrays.asList(LAYER_1, LAYER_2)), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddLayerSubscriber_MultipleSubscribers() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1);
-
- assertSubscribers(LAYER_1, mSubscriber, mSubscriber2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddLayerSubscriber_MultipleSubscribers_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriber2, LAYER_2);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertSubscribers(LAYER_2, mSubscriber2);
-
- assertEquals(
- new VmsSubscriptionState(2,
- new HashSet<>(Arrays.asList(LAYER_1, LAYER_2)), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.removeSubscription(mSubscriber, LAYER_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber_NoSubscriptions() {
- mRouting.removeSubscription(mSubscriber, LAYER_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber_MultipleTimes() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.removeSubscription(mSubscriber, LAYER_1);
- mRouting.removeSubscription(mSubscriber, LAYER_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber_Rewrapped() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.removeSubscription(mSubscriberRewrapped, LAYER_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber_UnknownSubscriber() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.removeSubscription(mSubscriber2, LAYER_1);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriber, LAYER_2);
- mRouting.removeSubscription(mSubscriber, LAYER_2);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber_MultipleSubscribers() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1);
- mRouting.removeSubscription(mSubscriber2, LAYER_1);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveLayerSubscriber_MultipleSubscribers_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
- mRouting.addSubscription(mSubscriber2, LAYER_2);
- mRouting.removeSubscription(mSubscriber2, LAYER_2);
-
- assertSubscribers(LAYER_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.singleton(LAYER_1), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber_MultipleTimes() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber_MultipleTimes_Rewrapped() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriberRewrapped, LAYER_1, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber_MultipleSubscribers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber, mSubscriber2);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber, LAYER_2, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertSubscribers(LAYER_2, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2, PUBLISHER_ID_2);
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(),
- new HashSet<>(Arrays.asList(
- new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)),
- new VmsAssociatedLayer(
- LAYER_2, Collections.singleton(PUBLISHER_ID_1))
- ))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber_MultiplePublishers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_2);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertSubscribers(LAYER_1, PUBLISHER_ID_2, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1,
- new HashSet<>(Arrays.asList(PUBLISHER_ID_1, PUBLISHER_ID_2))
- ))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber_MultipleSubscribers_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertSubscribers(LAYER_2, PUBLISHER_ID_1, mSubscriber2);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2, PUBLISHER_ID_2);
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(),
- new HashSet<>(Arrays.asList(
- new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)),
- new VmsAssociatedLayer(
- LAYER_2, Collections.singleton(PUBLISHER_ID_1))
- ))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddPublisherSubscriber_MultipleSubscribers_MultiplePublishers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_2);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertSubscribers(LAYER_1, PUBLISHER_ID_2, mSubscriber2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1,
- new HashSet<>(Arrays.asList(PUBLISHER_ID_1, PUBLISHER_ID_2))
- ))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_NoSubscribers() {
- mRouting.removeSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(0, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_MultipleTimes() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_Rewrapped() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriberRewrapped, LAYER_1, PUBLISHER_ID_1);
-
- assertNoSubscribers();
-
- assertEquals(
- new VmsSubscriptionState(2, Collections.emptySet(), Collections.emptySet()),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_UnknownSubscriber() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(1, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_MultiplePublishers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_2);
- mRouting.removeSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_2);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_MultipleSubscribers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_MultipleSubscribers_MultipleLayers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_1);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemovePublisherSubscriber_MultipleSubscribers_MultiplePublishers() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_2);
- mRouting.removeSubscription(mSubscriber2, LAYER_1, PUBLISHER_ID_2);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber);
- assertNoSubscribers(LAYER_1, PUBLISHER_ID_2);
- assertNoSubscribers(LAYER_2);
-
- assertEquals(
- new VmsSubscriptionState(3, Collections.emptySet(),
- Collections.singleton(new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddingAllTypesOfSubscribers() {
- IVmsSubscriberClient passiveSubscriber = new MockVmsSubscriber();
- mRouting.addSubscription(passiveSubscriber);
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1);
- mRouting.addSubscription(mSubscriber, LAYER_2);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_2);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, passiveSubscriber, mSubscriber, mSubscriber2);
- assertSubscribers(LAYER_1, PUBLISHER_ID_2, passiveSubscriber, mSubscriber2);
- assertSubscribers(LAYER_2, PUBLISHER_ID_1, passiveSubscriber, mSubscriber);
- assertSubscribers(LAYER_2, PUBLISHER_ID_2, passiveSubscriber, mSubscriber, mSubscriber2);
-
- assertEquals(
- new VmsSubscriptionState(4,
- new HashSet<>(Arrays.asList(LAYER_1, LAYER_2)),
- new HashSet<>(Arrays.asList(
- new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)),
- new VmsAssociatedLayer(
- LAYER_2, Collections.singleton(PUBLISHER_ID_2))
- ))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testAddingAndRemovingAllTypesOfSubscribers() {
- IVmsSubscriberClient passiveSubscriber = new MockVmsSubscriber();
- mRouting.addSubscription(passiveSubscriber);
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1);
- mRouting.addSubscription(mSubscriber, LAYER_2);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_2);
- mRouting.removeSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeSubscription(mSubscriber, LAYER_2);
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, passiveSubscriber, mSubscriber2);
- assertSubscribers(LAYER_1, PUBLISHER_ID_2, passiveSubscriber, mSubscriber2);
- assertSubscribers(LAYER_2, PUBLISHER_ID_1, passiveSubscriber);
- assertSubscribers(LAYER_2, PUBLISHER_ID_2, passiveSubscriber, mSubscriber2);
-
- assertEquals(
- new VmsSubscriptionState(6,
- Collections.singleton(LAYER_1),
- Collections.singleton(
- new VmsAssociatedLayer(
- LAYER_2, Collections.singleton(PUBLISHER_ID_2))
- )),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveDeadSubscriber() {
- IVmsSubscriberClient passiveSubscriber = new MockVmsSubscriber();
- mRouting.addSubscription(passiveSubscriber);
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1);
- mRouting.addSubscription(mSubscriber, LAYER_2);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_2);
- assertTrue(mRouting.removeDeadSubscriber(mSubscriber));
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, passiveSubscriber, mSubscriber2);
- assertSubscribers(LAYER_1, PUBLISHER_ID_2, passiveSubscriber, mSubscriber2);
- assertSubscribers(LAYER_2, PUBLISHER_ID_1, passiveSubscriber);
- assertSubscribers(LAYER_2, PUBLISHER_ID_2, passiveSubscriber, mSubscriber2);
-
- assertEquals(
- new VmsSubscriptionState(6,
- Collections.singleton(LAYER_1),
- Collections.singleton(
- new VmsAssociatedLayer(
- LAYER_2, Collections.singleton(PUBLISHER_ID_2))
- )),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveDeadSubscriber_NoSubscriptions() {
- IVmsSubscriberClient passiveSubscriber = new MockVmsSubscriber();
- mRouting.addSubscription(passiveSubscriber);
- mRouting.addSubscription(mSubscriber2, LAYER_1);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_2);
- assertFalse(mRouting.removeDeadSubscriber(mSubscriber));
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, passiveSubscriber, mSubscriber2);
- assertSubscribers(LAYER_1, PUBLISHER_ID_2, passiveSubscriber, mSubscriber2);
- assertSubscribers(LAYER_2, PUBLISHER_ID_1, passiveSubscriber);
- assertSubscribers(LAYER_2, PUBLISHER_ID_2, passiveSubscriber, mSubscriber2);
-
- assertEquals(
- new VmsSubscriptionState(2,
- Collections.singleton(LAYER_1),
- Collections.singleton(
- new VmsAssociatedLayer(
- LAYER_2, Collections.singleton(PUBLISHER_ID_2))
- )),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testRemoveDeadSubscriber_PassiveSubscriber() {
- IVmsSubscriberClient passiveSubscriber = new MockVmsSubscriber();
- mRouting.addSubscription(passiveSubscriber);
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.addSubscription(mSubscriber2, LAYER_1);
- mRouting.addSubscription(mSubscriber, LAYER_2);
- mRouting.addSubscription(mSubscriber2, LAYER_2, PUBLISHER_ID_2);
- assertFalse(mRouting.removeDeadSubscriber(passiveSubscriber));
-
- assertSubscribers(LAYER_1, PUBLISHER_ID_1, mSubscriber, mSubscriber2);
- assertSubscribers(LAYER_1, PUBLISHER_ID_2, mSubscriber2);
- assertSubscribers(LAYER_2, PUBLISHER_ID_1, mSubscriber);
- assertSubscribers(LAYER_2, PUBLISHER_ID_2, mSubscriber, mSubscriber2);
-
- assertEquals(
- new VmsSubscriptionState(4,
- new HashSet<>(Arrays.asList(LAYER_1, LAYER_2)),
- new HashSet<>(Arrays.asList(
- new VmsAssociatedLayer(
- LAYER_1, Collections.singleton(PUBLISHER_ID_1)),
- new VmsAssociatedLayer(
- LAYER_2, Collections.singleton(PUBLISHER_ID_2))
- ))),
- mRouting.getSubscriptionState());
- }
-
- @Test
- public void testHasSubscriptions_Default() {
- assertFalse(mRouting.hasLayerSubscriptions(LAYER_1));
- assertFalse(mRouting.hasLayerSubscriptions(LAYER_2));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_1, PUBLISHER_ID_1));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_1, PUBLISHER_ID_2));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_2, PUBLISHER_ID_1));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_2, PUBLISHER_ID_2));
- }
-
- @Test
- public void testHasSubscriptions_PassiveSubscriber() {
- mRouting.addSubscription(mSubscriber);
-
- testHasSubscriptions_Default();
- }
-
- @Test
- public void testHasSubscriptions_DeadSubscriber() {
- mRouting.addSubscription(mSubscriber);
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
- mRouting.removeDeadSubscriber(mSubscriber);
-
- testHasSubscriptions_Default();
- }
-
- @Test
- public void testHasSubscriptions_Layer() {
- mRouting.addSubscription(mSubscriber, LAYER_1);
-
- assertTrue(mRouting.hasLayerSubscriptions(LAYER_1));
- assertFalse(mRouting.hasLayerSubscriptions(LAYER_2));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_1, PUBLISHER_ID_1));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_1, PUBLISHER_ID_2));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_2, PUBLISHER_ID_1));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_2, PUBLISHER_ID_2));
-
- mRouting.removeSubscription(mSubscriber, LAYER_1);
-
- assertFalse(mRouting.hasLayerSubscriptions(LAYER_1));
- }
-
- @Test
- public void testHasSubscriptions_LayerFromPublisher() {
- mRouting.addSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
-
- assertFalse(mRouting.hasLayerSubscriptions(LAYER_1));
- assertFalse(mRouting.hasLayerSubscriptions(LAYER_2));
- assertTrue(mRouting.hasLayerFromPublisherSubscriptions(LAYER_1, PUBLISHER_ID_1));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_1, PUBLISHER_ID_2));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_2, PUBLISHER_ID_1));
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_2, PUBLISHER_ID_2));
-
- mRouting.removeSubscription(mSubscriber, LAYER_1, PUBLISHER_ID_1);
-
- assertFalse(mRouting.hasLayerFromPublisherSubscriptions(LAYER_1, PUBLISHER_ID_1));
- }
-
- class MockVmsSubscriber extends IVmsSubscriberClient.Stub {
- @Override
- public void onVmsMessageReceived(VmsLayer layer, byte[] payload) {
- throw new RuntimeException("Should not be accessed");
- }
-
- @Override
- public void onLayersAvailabilityChanged(VmsAvailableLayers availableLayers) {
- throw new RuntimeException("Should not be accessed");
- }
- }
-
- private void assertNoSubscribers() {
- assertSubscribers(); /* subscribers is empty */
- }
-
- private void assertNoSubscribers(VmsLayer layer) {
- assertSubscribers(layer); /* subscribers is empty */
- }
-
- private void assertNoSubscribers(VmsLayer layer, int publisherId) {
- assertSubscribers(layer, publisherId); /* subscribers is empty */
- }
-
- private void assertSubscribers(IVmsSubscriberClient... subscribers) {
- for (VmsLayer layer : LAYERS) {
- assertSubscribers(layer, subscribers);
- }
- }
-
- private void assertSubscribers(VmsLayer layer, IVmsSubscriberClient... subscribers) {
- for (int publisherId : PUBLISHER_IDS) {
- assertSubscribers(layer, publisherId, subscribers);
- }
- }
-
- private void assertSubscribers(VmsLayer layer, int publisherId,
- IVmsSubscriberClient... subscribers) {
- assertEquals(
- new HashSet<>(Arrays.asList(subscribers)),
- mRouting.getSubscribersForLayerFromPublisher(layer, publisherId));
- }
-}
\ No newline at end of file
diff --git a/tests/carservice_unit_test/src/com/android/car/VmsSubscriberServiceTest.java b/tests/carservice_unit_test/src/com/android/car/VmsSubscriberServiceTest.java
deleted file mode 100644
index 26945c1..0000000
--- a/tests/carservice_unit_test/src/com/android/car/VmsSubscriberServiceTest.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- * Copyright (C) 2019 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.car;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
-
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsAvailableLayers;
-import android.car.vms.VmsLayer;
-import android.content.Context;
-
-import androidx.test.filters.SmallTest;
-
-import com.android.car.hal.VmsHalService;
-import com.android.car.vms.VmsBrokerService;
-import com.android.car.vms.VmsClientManager;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.util.Arrays;
-import java.util.Collections;
-
-@RunWith(MockitoJUnitRunner.class)
-@SmallTest
-public class VmsSubscriberServiceTest {
- private static final VmsLayer LAYER = new VmsLayer(1, 2, 3);
- private static final int PUBLISHER_ID = 54321;
- private static final byte[] PUBLISHER_INFO = new byte[]{1, 2, 3, 4};
- private static final VmsAvailableLayers AVAILABLE_LAYERS =
- new VmsAvailableLayers(Collections.emptySet(), 0);
-
- @Mock
- private Context mContext;
- @Mock
- private VmsBrokerService mBrokerService;
- @Mock
- private VmsClientManager mClientManager;
- @Mock
- private VmsHalService mHal;
-
- @Mock
- private IVmsSubscriberClient mSubscriberClient;
- @Mock
- private IVmsSubscriberClient mSubscriberClient2;
-
- private VmsSubscriberService mSubscriberService;
-
- @Before
- public void setUp() {
- mSubscriberService = new VmsSubscriberService(mContext, mBrokerService, mClientManager,
- mHal);
- verify(mBrokerService).addSubscriberListener(eq(mSubscriberService));
- }
-
- @After
- public void tearDown() {
- verifyNoMoreInteractions(mBrokerService, mClientManager);
- }
-
- @Test
- public void testAddVmsSubscriberToNotifications() {
- mSubscriberService.addVmsSubscriberToNotifications(mSubscriberClient);
- verify(mClientManager).addSubscriber(mSubscriberClient);
- }
-
- @Test
- public void testRemoveVmsSubscriberToNotifications() {
- mSubscriberService.removeVmsSubscriberToNotifications(mSubscriberClient);
- verify(mClientManager).removeSubscriber(mSubscriberClient);
- }
-
- @Test
- public void testAddVmsSubscriber() {
- mSubscriberService.addVmsSubscriber(mSubscriberClient, LAYER);
- verify(mClientManager).addSubscriber(mSubscriberClient);
- verify(mBrokerService).addSubscription(mSubscriberClient, LAYER);
- }
-
- @Test
- public void testRemoveVmsSubscriber() {
- mSubscriberService.removeVmsSubscriber(mSubscriberClient, LAYER);
- verify(mBrokerService).removeSubscription(mSubscriberClient, LAYER);
- }
-
-
- @Test
- public void testAddVmsSubscriberToPublisher() {
- mSubscriberService.addVmsSubscriberToPublisher(mSubscriberClient, LAYER, PUBLISHER_ID);
- verify(mClientManager).addSubscriber(mSubscriberClient);
- verify(mBrokerService).addSubscription(mSubscriberClient, LAYER, PUBLISHER_ID);
- }
-
- @Test
- public void testRemoveVmsSubscriberToPublisher() {
- testAddVmsSubscriberToPublisher();
-
- mSubscriberService.removeVmsSubscriberToPublisher(mSubscriberClient, LAYER, PUBLISHER_ID);
- verify(mBrokerService).removeSubscription(mSubscriberClient, LAYER, PUBLISHER_ID);
- }
-
- @Test
- public void testAddVmsSubscriberPassive() {
- mSubscriberService.addVmsSubscriberPassive(mSubscriberClient);
- verify(mClientManager).addSubscriber(mSubscriberClient);
- verify(mBrokerService).addSubscription(mSubscriberClient);
- }
-
- @Test
- public void testRemoveVmsSubscriberPassive() {
- mSubscriberService.removeVmsSubscriberPassive(mSubscriberClient);
- verify(mBrokerService).removeSubscription(mSubscriberClient);
- }
-
- @Test
- public void testGetPublisherInfo() {
- when(mBrokerService.getPublisherInfo(PUBLISHER_ID)).thenReturn(PUBLISHER_INFO);
- assertThat(mSubscriberService.getPublisherInfo(PUBLISHER_ID)).isSameAs(PUBLISHER_INFO);
- verify(mBrokerService).getPublisherInfo(PUBLISHER_ID);
- }
-
- @Test
- public void testGetAvailableLayers() {
- when(mBrokerService.getAvailableLayers()).thenReturn(AVAILABLE_LAYERS);
- assertThat(mSubscriberService.getAvailableLayers()).isSameAs(AVAILABLE_LAYERS);
- verify(mBrokerService).getAvailableLayers();
- }
-
- @Test
- public void testOnLayersAvailabilityChange() throws Exception {
- when(mClientManager.getAllSubscribers())
- .thenReturn(Arrays.asList(mSubscriberClient, mSubscriberClient2));
- mSubscriberService.onLayersAvailabilityChange(AVAILABLE_LAYERS);
- verify(mClientManager).getAllSubscribers();
- verify(mSubscriberClient).onLayersAvailabilityChanged(AVAILABLE_LAYERS);
- verify(mSubscriberClient2).onLayersAvailabilityChanged(AVAILABLE_LAYERS);
- }
-}
diff --git a/tests/carservice_unit_test/src/com/android/car/vms/VmsClientManagerTest.java b/tests/carservice_unit_test/src/com/android/car/vms/VmsClientManagerTest.java
deleted file mode 100644
index 8177a10..0000000
--- a/tests/carservice_unit_test/src/com/android/car/vms/VmsClientManagerTest.java
+++ /dev/null
@@ -1,1126 +0,0 @@
-/*
- * Copyright (C) 2019 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.car.vms;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.argThat;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.ArgumentMatchers.same;
-import static org.mockito.Mockito.atLeast;
-import static org.mockito.Mockito.atMost;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
-
-import android.car.Car;
-import android.car.vms.IVmsPublisherClient;
-import android.car.vms.IVmsPublisherService;
-import android.car.vms.IVmsSubscriberClient;
-import android.car.vms.VmsAvailableLayers;
-import android.car.vms.VmsLayer;
-import android.car.vms.VmsSubscriptionState;
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.Intent;
-import android.content.ServiceConnection;
-import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageManager;
-import android.content.pm.ServiceInfo;
-import android.content.res.Resources;
-import android.os.Binder;
-import android.os.Handler;
-import android.os.IBinder;
-import android.os.Process;
-import android.os.UserHandle;
-import android.os.UserManager;
-
-import androidx.test.filters.FlakyTest;
-import androidx.test.filters.SmallTest;
-
-import com.android.car.VmsPublisherService;
-import com.android.car.hal.VmsHalService;
-import com.android.car.stats.CarStatsService;
-import com.android.car.stats.VmsClientLogger;
-import com.android.car.stats.VmsClientLogger.ConnectionState;
-import com.android.car.user.CarUserService;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-@SmallTest
-public class VmsClientManagerTest {
- private static final String SYSTEM_CLIENT = "com.google.android.apps.vms.test/.VmsSystemClient";
- private static final ComponentName SYSTEM_CLIENT_COMPONENT =
- ComponentName.unflattenFromString(SYSTEM_CLIENT);
- private static final String SYSTEM_CLIENT_NAME =
- "com.google.android.apps.vms.test/com.google.android.apps.vms.test.VmsSystemClient U=0";
-
- private static final String USER_CLIENT = "com.google.android.apps.vms.test/.VmsUserClient";
- private static final ComponentName USER_CLIENT_COMPONENT =
- ComponentName.unflattenFromString(USER_CLIENT);
- private static final int USER_ID = 10;
- private static final String USER_CLIENT_NAME =
- "com.google.android.apps.vms.test/com.google.android.apps.vms.test.VmsUserClient U=10";
- private static final int USER_ID_U11 = 11;
-
- private static final String TEST_PACKAGE = "test.package1";
- private static final String HAL_CLIENT_NAME = "HalClient";
- private static final String UNKNOWN_PACKAGE = "UnknownPackage";
-
- private static final int TEST_APP_ID = 12345;
- private static final int TEST_SYSTEM_UID = 12345;
- private static final int TEST_USER_UID = 1012345;
- private static final int TEST_USER_UID_U11 = 1112345;
-
- private static final long MILLIS_BEFORE_REBIND = 100;
-
- @Mock
- private Context mContext;
- @Mock
- private PackageManager mPackageManager;
- @Mock
- private Resources mResources;
- @Mock
- private CarStatsService mStatsService;
- @Mock
- private UserManager mUserManager;
- @Mock
- private CarUserService mUserService;
-
- @Mock
- private VmsBrokerService mBrokerService;
-
- @Mock
- private VmsHalService mHal;
-
- @Mock
- private Handler mHandler;
-
- @Captor
- private ArgumentCaptor<Runnable> mRebindCaptor;
-
- @Mock
- private VmsPublisherService mPublisherService;
-
- @Mock
- private IVmsSubscriberClient mSubscriberClient1;
- @Mock
- private Binder mSubscriberBinder1;
-
- @Captor
- private ArgumentCaptor<IBinder.DeathRecipient> mDeathRecipient;
-
- @Mock
- private IVmsSubscriberClient mSubscriberClient2;
- @Mock
- private Binder mSubscriberBinder2;
-
- @Captor
- private ArgumentCaptor<ServiceConnection> mConnectionCaptor;
-
- @Captor
- private ArgumentCaptor<Runnable> mSystemUserUnlockedListenerCaptor;
-
- @Captor
- private ArgumentCaptor<CarUserService.UserCallback> mUserCallbackCaptor;
-
- @Mock
- private VmsClientLogger mSystemClientLog;
- @Mock
- private VmsClientLogger mUserClientLog;
- @Mock
- private VmsClientLogger mUserClientLog2;
- @Mock
- private VmsClientLogger mHalClientLog;
-
- private VmsClientManager mClientManager;
-
-
- private int mForegroundUserId;
- private int mCallingAppUid;
-
- private ServiceInfo mSystemServiceInfo;
- private ServiceInfo mUserServiceInfo;
-
- @Before
- public void setUp() throws Exception {
- resetContext();
- mSystemServiceInfo = new ServiceInfo();
- mSystemServiceInfo.permission = Car.PERMISSION_BIND_VMS_CLIENT;
- mSystemServiceInfo.applicationInfo = new ApplicationInfo();
- mSystemServiceInfo.applicationInfo.uid = TEST_APP_ID;
- when(mPackageManager.getServiceInfo(eq(SYSTEM_CLIENT_COMPONENT), anyInt()))
- .thenReturn(mSystemServiceInfo);
- when(mStatsService.getVmsClientLogger(TEST_SYSTEM_UID)).thenReturn(mSystemClientLog);
-
- mUserServiceInfo = new ServiceInfo();
- mUserServiceInfo.permission = Car.PERMISSION_BIND_VMS_CLIENT;
- mUserServiceInfo.applicationInfo = new ApplicationInfo();
- mUserServiceInfo.applicationInfo.uid = TEST_APP_ID;
- when(mPackageManager.getServiceInfo(eq(USER_CLIENT_COMPONENT), anyInt()))
- .thenReturn(mUserServiceInfo);
- when(mStatsService.getVmsClientLogger(TEST_USER_UID)).thenReturn(mUserClientLog);
- when(mStatsService.getVmsClientLogger(TEST_USER_UID_U11)).thenReturn(mUserClientLog2);
-
- when(mStatsService.getVmsClientLogger(Process.myUid())).thenReturn(mHalClientLog);
-
- when(mResources.getInteger(
- com.android.car.R.integer.millisecondsBeforeRebindToVmsPublisher)).thenReturn(
- (int) MILLIS_BEFORE_REBIND);
- when(mResources.getStringArray(
- com.android.car.R.array.vmsPublisherSystemClients)).thenReturn(
- new String[]{ SYSTEM_CLIENT });
- when(mResources.getStringArray(
- com.android.car.R.array.vmsPublisherUserClients)).thenReturn(
- new String[]{ USER_CLIENT });
-
- when(mContext.getSystemService(eq(Context.USER_SERVICE))).thenReturn(mUserManager);
-
- mClientManager = new VmsClientManager(mContext, mStatsService, mUserService,
- mBrokerService, mHal, mHandler, () -> mCallingAppUid);
-
- // call init() so the listener and callback are captured
- verifyInit();
-
- mClientManager.setPublisherService(mPublisherService);
-
- notifyUserSwitched(USER_ID, false);
- mCallingAppUid = UserHandle.getUid(USER_ID, 0);
-
- when(mSubscriberClient1.asBinder()).thenReturn(mSubscriberBinder1);
- when(mSubscriberClient2.asBinder()).thenReturn(mSubscriberBinder2);
-
- when(mPackageManager.getNameForUid(mCallingAppUid)).thenReturn(TEST_PACKAGE);
- }
-
- @After
- public void tearDown() {
- verify(mContext, atLeast(0)).getSystemService(eq(Context.USER_SERVICE));
- verify(mContext, atLeast(0)).getResources();
- verify(mContext, atLeast(0)).getPackageManager();
- verifyNoMoreInteractions(mContext, mBrokerService, mHal, mPublisherService, mHandler);
- verifyNoMoreInteractions(mSystemClientLog, mUserClientLog, mUserClientLog2, mHalClientLog);
- }
-
- private void verifyInit() {
- mClientManager.init();
-
- // Verify registration of system user unlock listener
- verify(mUserService).runOnUser0Unlock(mSystemUserUnlockedListenerCaptor.capture());
- // Verify user callback is added
- verify(mUserService).addUserCallback(mUserCallbackCaptor.capture());
- }
-
- @Test
- public void testRelease() {
- mClientManager.release();
-
- // Verify user switch receiver is unregistered
- verify(mUserService).removeUserCallback(mUserCallbackCaptor.getValue());
- }
-
- @Test
- public void testSystemUserUnlocked() {
- notifySystemUserUnlocked();
- notifySystemUserUnlocked();
-
- // Multiple events should only trigger a single bind, when successful
- verifySystemBind(1);
- }
-
- @Test
- public void testSystemUserUnlocked_ClientNotFound() throws Exception {
- when(mPackageManager.getServiceInfo(eq(SYSTEM_CLIENT_COMPONENT), anyInt()))
- .thenThrow(new PackageManager.NameNotFoundException());
- notifySystemUserUnlocked();
-
- // Process will not be bound
- verifySystemBind(0);
- }
-
- @Test
- public void testSystemUserUnlocked_WrongPermission() throws Exception {
- mSystemServiceInfo.permission = Car.PERMISSION_VMS_PUBLISHER;
- notifySystemUserUnlocked();
-
- // Process will not be bound
- verifySystemBind(0);
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- @Test
- public void testSystemUserUnlocked_BindFailed() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenReturn(false);
- notifySystemUserUnlocked();
- notifySystemUserUnlocked();
-
- // Failure state will trigger another attempt on event
- verifySystemBind(2);
- verify(mSystemClientLog, times(2)).logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- @Test
- public void testSystemUserUnlocked_BindException() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenThrow(
- new SecurityException());
- notifySystemUserUnlocked();
- notifySystemUserUnlocked();
-
- // Failure state will trigger another attempt on event
- verifySystemBind(2);
- verify(mSystemClientLog, times(2)).logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- @Test
- public void testUserUnlocked() {
- notifyUserUnlocked(USER_ID, true);
- notifyUserUnlocked(USER_ID, true);
-
- // Multiple events should only trigger a single bind, when successful
- verifyUserBind(1);
- }
-
- @Test
- public void testUserUnlocked_ForegroundUserNotUnlocked() {
- notifyUserUnlocked(USER_ID, false);
-
- // Process will not be bound
- verifyUserBind(0);
- }
-
- @Test
- public void testUserUnlocked_OtherUserUnlocked() {
- notifyUserUnlocked(USER_ID_U11, true);
-
- // Process will not be bound
- verifyUserBind(0);
- }
-
- @Test
- public void testUserUnlocked_ClientNotFound() throws Exception {
- when(mPackageManager.getServiceInfo(eq(USER_CLIENT_COMPONENT), anyInt()))
- .thenThrow(new PackageManager.NameNotFoundException());
- notifyUserUnlocked(USER_ID, true);
-
- // Process will not be bound
- verifyUserBind(0);
- }
-
- @Test
- public void testUserUnlocked_WrongPermission() throws Exception {
- mUserServiceInfo.permission = Car.PERMISSION_VMS_PUBLISHER;
- notifyUserUnlocked(USER_ID, true);
-
- // Process will not be bound
- verifyUserBind(0);
- verify(mUserClientLog).logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- @Test
- public void testUserUnlocked_BindFailed() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any()))
- .thenReturn(false);
- notifyUserUnlocked(USER_ID, true);
- notifyUserUnlocked(USER_ID, true);
-
- // Failure state will trigger another attempt
- verifyUserBind(2);
- verify(mUserClientLog, times(2)).logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- @Test
- public void testUserUnlocked_UserBindFailed() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), eq(UserHandle.of(USER_ID))))
- .thenReturn(false);
- notifyUserUnlocked(USER_ID, true);
- notifyUserUnlocked(USER_ID, true);
-
- // Failure state will trigger another attempt
- verifyUserBind(2);
- verify(mUserClientLog, times(2)).logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- @Test
- public void testUserUnlocked_BindException() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any()))
- .thenThrow(new SecurityException());
- notifyUserUnlocked(USER_ID, true);
- notifyUserUnlocked(USER_ID, true);
-
- // Failure state will trigger another attempt
- verifyUserBind(2);
- verify(mUserClientLog, times(2)).logConnectionState(ConnectionState.CONNECTION_ERROR);
- }
-
- @Test
- public void testUserUnlocked_SystemRebind() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), eq(UserHandle.SYSTEM)))
- .thenReturn(false);
- notifySystemUserUnlocked();
- verifySystemBind(1);
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTION_ERROR);
- resetContext();
-
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), eq(UserHandle.SYSTEM)))
- .thenReturn(true);
- notifyUserUnlocked(USER_ID, true);
- verifySystemBind(1);
- verifyUserBind(1);
- }
-
- @Test
- public void testUserUnlocked_SystemRebind_BindFailed() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), eq(UserHandle.SYSTEM)))
- .thenReturn(false);
- notifySystemUserUnlocked();
- verifySystemBind(1);
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTION_ERROR);
- resetContext();
-
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), eq(UserHandle.SYSTEM)))
- .thenReturn(false);
- notifyUserUnlocked(USER_ID, true);
- notifyUserUnlocked(USER_ID, true);
-
- verifySystemBind(2); // Failure state will trigger another attempt
- verify(mSystemClientLog, times(2)).logConnectionState(ConnectionState.CONNECTION_ERROR);
- verifyUserBind(1);
- }
-
- @Test
- public void testUserUnlocked_SystemRebind_BindException() {
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), eq(UserHandle.SYSTEM)))
- .thenThrow(new SecurityException());
- notifySystemUserUnlocked();
- verifySystemBind(1);
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTION_ERROR);
- resetContext();
-
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), eq(UserHandle.SYSTEM)))
- .thenThrow(new SecurityException());
- notifyUserUnlocked(USER_ID, true);
- notifyUserUnlocked(USER_ID, true);
-
- verifySystemBind(2); // Failure state will trigger another attempt
- verify(mSystemClientLog, times(2)).logConnectionState(ConnectionState.CONNECTION_ERROR);
- verifyUserBind(1);
- }
-
- @Test
- public void testUserSwitched() {
- notifyUserSwitched(USER_ID, true);
- notifyUserSwitched(USER_ID, true);
-
- // Multiple events should only trigger a single bind, when successful
- verifyUserBind(1);
- }
-
- @Test
- public void testUserSwitchedAndUnlocked() {
- notifyUserSwitched(USER_ID, true);
- notifyUserUnlocked(USER_ID, true);
-
- // Multiple events should only trigger a single bind, when successful
- verifyUserBind(1);
- }
-
- @Test
- public void testUserSwitched_ForegroundUserNotUnlocked() {
- notifyUserSwitched(USER_ID, false);
-
- // Process will not be bound
- verifyUserBind(0);
- }
-
- @Test
- public void testUserSwitchedToSystemUser() {
- notifyUserSwitched(UserHandle.USER_SYSTEM, true);
-
- // Neither user nor system processes will be bound for system user intent
- verifySystemBind(0);
- verifyUserBind(0);
- }
-
- @Test
- public void testOnSystemServiceConnected() {
- IBinder binder = bindSystemClient();
- verifyOnClientConnected(SYSTEM_CLIENT_NAME, binder);
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTED);
- }
-
- private IBinder bindSystemClient() {
- notifySystemUserUnlocked();
- verifySystemBind(1);
- resetContext();
-
- IBinder binder = createPublisherBinder();
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, binder);
- return binder;
- }
-
- @Test
- public void testOnUserServiceConnected() {
- IBinder binder = bindUserClient();
- verifyOnClientConnected(USER_CLIENT_NAME, binder);
- verify(mUserClientLog).logConnectionState(ConnectionState.CONNECTED);
- }
-
- private IBinder bindUserClient() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- resetContext();
-
- IBinder binder = createPublisherBinder();
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, binder);
- return binder;
- }
-
- @Test
- public void testOnSystemServiceDisconnected() throws Exception {
- notifySystemUserUnlocked();
- verifySystemBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService);
-
- connection.onServiceDisconnected(null);
- verify(mPublisherService).onClientDisconnected(eq(SYSTEM_CLIENT_NAME));
- verify(mSystemClientLog).logConnectionState(ConnectionState.DISCONNECTED);
-
- verifyAndRunRebindTask();
- verify(mContext).unbindService(connection);
- verifySystemBind(1);
- }
-
- @Test
- public void testOnSystemServiceDisconnected_ServiceReboundByAndroid() throws Exception {
- notifySystemUserUnlocked();
- verifySystemBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- IBinder binder = createPublisherBinder();
- connection.onServiceConnected(null, binder);
- verifyOnClientConnected(SYSTEM_CLIENT_NAME, binder);
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService, mSystemClientLog);
-
- connection.onServiceDisconnected(null);
- verify(mPublisherService).onClientDisconnected(eq(SYSTEM_CLIENT_NAME));
- verify(mSystemClientLog).logConnectionState(ConnectionState.DISCONNECTED);
-
- binder = createPublisherBinder();
- connection.onServiceConnected(null, binder);
- verifyOnClientConnected(SYSTEM_CLIENT_NAME, binder);
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTED);
-
- verifyAndRunRebindTask();
- // No more interactions (verified by tearDown)
- }
-
-
- @Test
- public void testOnSystemServiceBindingDied() throws Exception {
- notifySystemUserUnlocked();
- verifySystemBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- verify(mSystemClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService);
-
- connection.onBindingDied(null);
- verify(mPublisherService).onClientDisconnected(eq(SYSTEM_CLIENT_NAME));
- verify(mSystemClientLog).logConnectionState(ConnectionState.DISCONNECTED);
-
- verifyAndRunRebindTask();
- verify(mContext).unbindService(connection);
- verifySystemBind(1);
- }
-
- @Test
- public void testOnSystemServiceBindingDied_ServiceNotConnected() throws Exception {
- notifySystemUserUnlocked();
- verifySystemBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onBindingDied(null);
-
- verifyZeroInteractions(mPublisherService);
-
- verifyAndRunRebindTask();
- verify(mContext).unbindService(connection);
- verifySystemBind(1);
- }
-
- @Test
- @FlakyTest
- public void testOnUserServiceDisconnected() throws Exception {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- verify(mUserClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService);
-
- connection.onServiceDisconnected(null);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.DISCONNECTED);
-
- verifyAndRunRebindTask();
- verify(mContext).unbindService(connection);
- verifyUserBind(1);
- }
-
- @Test
- public void testOnUserServiceDisconnected_ServiceReboundByAndroid() throws Exception {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- IBinder binder = createPublisherBinder();
- connection.onServiceConnected(null, binder);
- verifyOnClientConnected(USER_CLIENT_NAME, binder);
- verify(mUserClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService, mUserClientLog);
-
- connection.onServiceDisconnected(null);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.DISCONNECTED);
-
- binder = createPublisherBinder();
- connection.onServiceConnected(null, binder);
- verifyOnClientConnected(USER_CLIENT_NAME, binder);
- verify(mUserClientLog).logConnectionState(ConnectionState.CONNECTED);
-
- verifyAndRunRebindTask();
- // No more interactions (verified by tearDown)
- }
-
- @Test
- public void testOnUserServiceBindingDied() throws Exception {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- verify(mUserClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService);
-
- connection.onBindingDied(null);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.DISCONNECTED);
-
- verifyAndRunRebindTask();
- verify(mContext).unbindService(connection);
- verifyUserBind(1);
- }
-
- @Test
- public void testOnUserServiceBindingDied_ServiceNotConnected() throws Exception {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onBindingDied(null);
-
- verifyZeroInteractions(mPublisherService);
-
- verifyAndRunRebindTask();
- verify(mContext).unbindService(connection);
- verifyUserBind(1);
- }
-
- @Test
- public void testOnUserSwitched_UserChange() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- resetContext();
-
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- verify(mUserClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService);
-
- notifyUserSwitched(USER_ID_U11, true);
-
- verify(mContext).unbindService(connection);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.TERMINATED);
- verifyUserBind(1);
- }
-
- @Test
- public void testOnUserSwitched_UserChange_ForegroundUserNotUnlocked() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- resetContext();
- reset(mPublisherService);
-
- notifyUserSwitched(USER_ID_U11, false);
-
- verify(mContext).unbindService(connection);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.TERMINATED);
- verifyUserBind(0);
- }
-
- @Test
- public void testOnUserSwitched_UserChange_ToSystemUser() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- resetContext();
- reset(mPublisherService);
-
- notifyUserSwitched(UserHandle.USER_SYSTEM, true);
-
- verify(mContext).unbindService(connection);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.TERMINATED);
- verifyUserBind(0);
- }
-
- @Test
- public void testOnUserSwitched_UserChange_ServiceNotConnected() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- ServiceConnection connection = mConnectionCaptor.getValue();
- resetContext();
-
- notifyUserSwitched(USER_ID_U11, true);
-
- verify(mContext).unbindService(connection);
- verifyUserBind(1);
- }
-
- @Test
- public void testOnUserSwitched_UserChange_ServiceNotConnected_ForegroundUserNotUnlocked() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- ServiceConnection connection = mConnectionCaptor.getValue();
- resetContext();
-
- notifyUserSwitched(USER_ID_U11, false);
-
- verify(mContext).unbindService(connection);
- verifyUserBind(0);
- }
-
- @Test
- public void testOnUserUnlocked_UserChange() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- resetContext();
- reset(mPublisherService);
-
- notifyUserSwitched(USER_ID_U11, false);
- notifyUserUnlocked(USER_ID_U11, true);
-
- verify(mContext).unbindService(connection);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.TERMINATED);
- verifyUserBind(1);
- }
-
- @Test
- public void testOnUserUnlocked_UserChange_ToSystemUser() {
- notifySystemUserUnlocked();
- verifySystemBind(1);
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- ServiceConnection connection = mConnectionCaptor.getValue();
- connection.onServiceConnected(null, createPublisherBinder());
- resetContext();
- reset(mPublisherService);
-
- notifyUserSwitched(USER_ID_U11, false);
- notifyUserUnlocked(UserHandle.USER_SYSTEM, true);
-
- verify(mContext).unbindService(connection);
- verify(mPublisherService).onClientDisconnected(eq(USER_CLIENT_NAME));
- verify(mUserClientLog).logConnectionState(ConnectionState.TERMINATED);
- // User processes will not be bound for system user
- verifyUserBind(0);
- }
-
- @Test
- public void testOnUserUnlocked_UserChange_ServiceNotConnected() {
- notifyUserUnlocked(USER_ID, true);
- verifyUserBind(1);
- ServiceConnection connection = mConnectionCaptor.getValue();
- resetContext();
-
- notifyUserSwitched(USER_ID_U11, false);
- notifyUserUnlocked(USER_ID_U11, true);
-
- verify(mContext).unbindService(connection);
- verifyUserBind(1);
- }
-
- @Test
- public void testAddSubscriber() {
- mClientManager.addSubscriber(mSubscriberClient1);
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient2));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient2));
- }
-
- @Test
- public void testAddSubscriber_SystemUser() {
- mCallingAppUid = UserHandle.getUid(UserHandle.USER_SYSTEM, 0);
- when(mPackageManager.getNameForUid(mCallingAppUid)).thenReturn(TEST_PACKAGE);
-
- mClientManager.addSubscriber(mSubscriberClient1);
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient2));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient2));
- }
-
- @Test
- public void testAddSubscriber_NotForegroundUser() {
- mCallingAppUid = UserHandle.getUid(USER_ID_U11, 0);
-
- try {
- mClientManager.addSubscriber(mSubscriberClient1);
- fail("Expected client to be rejected");
- } catch (SecurityException expected) {
- // expected
- }
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient1));
- }
-
- @Test
- public void testAddSubscriber_MultipleCalls() {
- mClientManager.addSubscriber(mSubscriberClient1);
- mClientManager.addSubscriber(mSubscriberClient1);
- verify(mPackageManager, atMost(1)).getNameForUid(anyInt());
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient2));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient2));
- }
-
- @Test
- public void testAddSubscriber_MultipleClients_SamePackage() {
- mClientManager.addSubscriber(mSubscriberClient1);
- mClientManager.addSubscriber(mSubscriberClient2);
- verify(mPackageManager, atMost(2)).getNameForUid(anyInt());
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient2));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient2));
- }
-
- @Test
- public void testAddSubscriber_MultipleClients_ForegroundAndSystemUsers_SamePackage() {
- int clientUid1 = mCallingAppUid;
- mClientManager.addSubscriber(mSubscriberClient1);
-
- mCallingAppUid = UserHandle.getUid(UserHandle.USER_SYSTEM, 0);
- when(mPackageManager.getNameForUid(mCallingAppUid)).thenReturn(TEST_PACKAGE);
- mClientManager.addSubscriber(mSubscriberClient2);
-
- verify(mPackageManager, atMost(2)).getNameForUid(anyInt());
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(clientUid1, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient2));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient2));
- }
-
-
- @Test
- public void testAddSubscriber_MultipleClients_MultiplePackages() {
- int clientUid1 = mCallingAppUid;
- mClientManager.addSubscriber(mSubscriberClient1);
-
- mCallingAppUid = UserHandle.getUid(mForegroundUserId, 1);
- when(mPackageManager.getNameForUid(mCallingAppUid)).thenReturn("test.package2");
- mClientManager.addSubscriber(mSubscriberClient2);
-
- verify(mPackageManager, times(2)).getNameForUid(anyInt());
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(clientUid1, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertEquals("test.package2", mClientManager.getPackageName(mSubscriberClient2));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient2));
- }
-
- @Test
- public void testRemoveSubscriber() {
- mClientManager.addSubscriber(mSubscriberClient1);
- mClientManager.removeSubscriber(mSubscriberClient1);
- verify(mBrokerService).removeDeadSubscriber(mSubscriberClient1);
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient1));
- }
-
- @Test
- public void testRemoveSubscriber_NotRegistered() {
- mClientManager.removeSubscriber(mSubscriberClient1);
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient1));
- }
-
- @Test
- public void testRemoveSubscriber_OnDeath() {
- mClientManager.addSubscriber(mSubscriberClient1);
-
- verify(mSubscriberBinder1).linkToDeath(mDeathRecipient.capture(), eq(0));
- mDeathRecipient.getValue().binderDied();
-
- verify(mBrokerService).removeDeadSubscriber(mSubscriberClient1);
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient1));
- }
-
- @Test
- public void testOnUserSwitch_RemoveSubscriber() {
- mClientManager.addSubscriber(mSubscriberClient1);
-
- notifyUserSwitched(USER_ID_U11, false);
- verify(mBrokerService).removeDeadSubscriber(mSubscriberClient1);
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertTrue(mClientManager.getAllSubscribers().isEmpty());
- }
-
- @Test
- public void testOnUserSwitch_RemoveSubscriber_AddNewSubscriber() {
- mClientManager.addSubscriber(mSubscriberClient1);
-
- notifyUserSwitched(USER_ID_U11, false);
- verify(mBrokerService).removeDeadSubscriber(mSubscriberClient1);
-
- mCallingAppUid = UserHandle.getUid(USER_ID_U11, 0);
- when(mPackageManager.getNameForUid(mCallingAppUid)).thenReturn(TEST_PACKAGE);
- mClientManager.addSubscriber(mSubscriberClient2);
-
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(-1, mClientManager.getSubscriberUid(mSubscriberClient1));
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient2));
- assertEquals(mCallingAppUid, mClientManager.getSubscriberUid(mSubscriberClient2));
- assertFalse(mClientManager.getAllSubscribers().contains(mSubscriberClient1));
- assertTrue(mClientManager.getAllSubscribers().contains(mSubscriberClient2));
- }
-
- @Test
- public void testOnUserSwitch_RemoveSubscriber_RetainSystemClient() {
- mClientManager.addSubscriber(mSubscriberClient1);
-
- mCallingAppUid = UserHandle.getUid(UserHandle.USER_SYSTEM, 0);
- when(mPackageManager.getNameForUid(mCallingAppUid)).thenReturn(TEST_PACKAGE);
-
- mClientManager.addSubscriber(mSubscriberClient2);
-
- notifyUserSwitched(USER_ID_U11, false);
-
- verify(mBrokerService).removeDeadSubscriber(mSubscriberClient1);
- verify(mBrokerService, never()).removeDeadSubscriber(mSubscriberClient2);
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(mSubscriberClient1));
- assertEquals(TEST_PACKAGE, mClientManager.getPackageName(mSubscriberClient2));
- }
-
- @Test
- public void testOnUserSwitch_RemoveSubscriber_RetainHalClient() {
- IVmsPublisherClient publisherClient = createPublisherClient();
- IVmsSubscriberClient subscriberClient = createSubscriberClient();
- mClientManager.onHalConnected(publisherClient, subscriberClient);
- verify(mHalClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService);
-
- notifyUserSwitched(USER_ID_U11, false);
-
- verify(mBrokerService, never()).removeDeadSubscriber(subscriberClient);
- assertEquals(HAL_CLIENT_NAME, mClientManager.getPackageName(subscriberClient));
- }
-
- @Test
- public void testHalClientConnected() {
- IVmsPublisherClient publisherClient = createPublisherClient();
- IVmsSubscriberClient subscriberClient = createSubscriberClient();
- mClientManager.onHalConnected(publisherClient, subscriberClient);
- verify(mPublisherService).onClientConnected(eq(HAL_CLIENT_NAME), same(publisherClient));
- verify(mHalClientLog).logConnectionState(ConnectionState.CONNECTED);
- assertTrue(mClientManager.getAllSubscribers().contains(subscriberClient));
- assertEquals(HAL_CLIENT_NAME, mClientManager.getPackageName(subscriberClient));
- }
-
- @Test
- public void testHalClientConnected_AfterAddSubscriber() {
- IVmsPublisherClient publisherClient = createPublisherClient();
- IVmsSubscriberClient subscriberClient = createSubscriberClient();
- mClientManager.addSubscriber(subscriberClient);
-
- mClientManager.onHalConnected(publisherClient, subscriberClient);
- verify(mPublisherService).onClientConnected(eq(HAL_CLIENT_NAME), same(publisherClient));
- verify(mHalClientLog).logConnectionState(ConnectionState.CONNECTED);
- assertTrue(mClientManager.getAllSubscribers().contains(subscriberClient));
- assertEquals(HAL_CLIENT_NAME, mClientManager.getPackageName(subscriberClient));
- }
-
- @Test
- public void testOnHalClientDisconnected() {
- IVmsPublisherClient publisherClient = createPublisherClient();
- IVmsSubscriberClient subscriberClient = createSubscriberClient();
- mClientManager.onHalConnected(publisherClient, subscriberClient);
- verify(mHalClientLog).logConnectionState(ConnectionState.CONNECTED);
- reset(mPublisherService);
-
- mClientManager.onHalDisconnected();
- verify(mPublisherService).onClientDisconnected(eq(HAL_CLIENT_NAME));
- verify(mBrokerService).removeDeadSubscriber(eq(subscriberClient));
- verify(mHalClientLog).logConnectionState(ConnectionState.DISCONNECTED);
- assertFalse(mClientManager.getAllSubscribers().contains(subscriberClient));
- assertEquals(UNKNOWN_PACKAGE, mClientManager.getPackageName(subscriberClient));
- }
-
- @Test
- public void testOnHalClientDisconnected_NotConnected() {
- mClientManager.onHalDisconnected();
- verify(mPublisherService, never()).onClientDisconnected(eq(HAL_CLIENT_NAME));
- assertTrue(mClientManager.getAllSubscribers().isEmpty());
- }
-
- private void resetContext() {
- reset(mContext, mSystemClientLog, mUserClientLog);
- when(mContext.getPackageManager()).thenReturn(mPackageManager);
- when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenReturn(true);
- when(mContext.getResources()).thenReturn(mResources);
- }
-
- private void notifySystemUserUnlocked() {
- mSystemUserUnlockedListenerCaptor.getValue().run();
- }
-
- private void notifyUserSwitched(int foregroundUserId, boolean isForegroundUserUnlocked) {
- when(mUserManager.isUserUnlockingOrUnlocked(foregroundUserId))
- .thenReturn(isForegroundUserUnlocked);
- mForegroundUserId = foregroundUserId; // Member variable used by verifyUserBind()
- mUserCallbackCaptor.getValue().onSwitchUser(foregroundUserId);
- }
-
- private void notifyUserUnlocked(int foregroundUserId, boolean isForegroundUserUnlocked) {
- when(mUserManager.isUserUnlockingOrUnlocked(foregroundUserId))
- .thenReturn(isForegroundUserUnlocked);
- mUserCallbackCaptor.getValue().onUserLockChanged(foregroundUserId,
- isForegroundUserUnlocked);
- }
-
- private void verifySystemBind(int times) {
- verify(mSystemClientLog, times(times)).logConnectionState(ConnectionState.CONNECTING);
- verifyBind(times, SYSTEM_CLIENT_COMPONENT, UserHandle.SYSTEM);
- }
-
- private void verifyUserBind(int times) {
- if (mForegroundUserId == USER_ID) {
- verify(mUserClientLog, times(times)).logConnectionState(ConnectionState.CONNECTING);
- } else if (mForegroundUserId == USER_ID_U11) {
- verify(mUserClientLog2, times(times)).logConnectionState(ConnectionState.CONNECTING);
- }
- verifyBind(times, USER_CLIENT_COMPONENT, UserHandle.of(mForegroundUserId));
- }
-
- private void verifyBind(int times, ComponentName componentName, UserHandle user) {
- Intent expectedService = new Intent();
- expectedService.setComponent(componentName);
- verify(mContext, times(times)).bindServiceAsUser(
- argThat((service) -> service.filterEquals(expectedService)),
- mConnectionCaptor.capture(),
- eq(Context.BIND_AUTO_CREATE), any(Handler.class), eq(user));
- }
-
- private void verifyAndRunRebindTask() {
- verify(mHandler).postDelayed(mRebindCaptor.capture(), eq(MILLIS_BEFORE_REBIND));
- mRebindCaptor.getValue().run();
- }
-
- private void verifyOnClientConnected(String publisherName, IBinder binder) {
- ArgumentCaptor<IVmsPublisherClient> clientCaptor =
- ArgumentCaptor.forClass(IVmsPublisherClient.class);
- verify(mPublisherService).onClientConnected(eq(publisherName), clientCaptor.capture());
- assertSame(binder, clientCaptor.getValue().asBinder());
- }
-
- private IBinder createPublisherBinder() {
- return createPublisherClient().asBinder();
- }
-
- private IVmsPublisherClient createPublisherClient() {
- return new IVmsPublisherClient.Stub() {
- @Override
- public void setVmsPublisherService(IBinder token, IVmsPublisherService service) {
- throw new RuntimeException("Unexpected call");
- }
-
- @Override
- public void onVmsSubscriptionChange(VmsSubscriptionState subscriptionState) {
- throw new RuntimeException("Unexpected call");
- }
- };
- }
-
- private IVmsSubscriberClient createSubscriberClient() {
- return new IVmsSubscriberClient.Stub() {
- @Override
- public void onVmsMessageReceived(VmsLayer layer, byte[] payload) {
- throw new RuntimeException("Unexpected call");
- }
-
- @Override
- public void onLayersAvailabilityChanged(VmsAvailableLayers availableLayers) {
- throw new RuntimeException("Unexpected call");
- }
- };
- }
-}