1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car; 18 19 import android.car.vms.VmsLayer; 20 import android.car.vms.VmsLayerDependency; 21 import android.car.vms.VmsLayersOffering; 22 import android.car.vms.VmsPublisherClientService; 23 import android.car.vms.VmsSubscriptionState; 24 import android.util.Log; 25 26 import java.util.HashSet; 27 import java.util.Set; 28 29 /** 30 * This service is launched during the tests in VmsPublisherSubscriberTest. It publishes a property 31 * that is going to be verified in the test. 32 * 33 * The service makes offering for pre-defined layers which verifies availability notifications for 34 * subscribers without them actively being subscribed to a layer, and also echos all the 35 * subscription requests with an offering for that layer. 36 * For example, without any subscription request from any client, this service will make offering 37 * to layer X. If a client will subscribe later to layer Y, this service will respond with offering 38 * to both layers X and Y. 39 * 40 * Note that the subscriber can subscribe before the publisher finishes initialization. To cover 41 * both potential scenarios, this service publishes the test message in onVmsSubscriptionChange 42 * and in onVmsPublisherServiceReady. See comments below. 43 */ 44 public class VmsPublisherClientMockService extends VmsPublisherClientService { 45 private static final String TAG = "VmsPublisherClientMockService"; 46 47 @Override onVmsSubscriptionChange(VmsSubscriptionState subscriptionState)48 public void onVmsSubscriptionChange(VmsSubscriptionState subscriptionState) { 49 // Case when the publisher finished initialization before the subscription request. 50 initializeMockPublisher(subscriptionState); 51 } 52 53 @Override onVmsPublisherServiceReady()54 public void onVmsPublisherServiceReady() { 55 // Case when the subscription request was sent before the publisher was ready. 56 VmsSubscriptionState subscriptionState = getSubscriptions(); 57 initializeMockPublisher(subscriptionState); 58 } 59 initializeMockPublisher(VmsSubscriptionState subscriptionState)60 private void initializeMockPublisher(VmsSubscriptionState subscriptionState) { 61 Log.d(TAG, "Initializing Mock publisher"); 62 int publisherId = getPublisherId(VmsPublisherSubscriberTest.PAYLOAD); 63 publishIfNeeded(subscriptionState); 64 declareOffering(subscriptionState, publisherId); 65 } 66 publishIfNeeded(VmsSubscriptionState subscriptionState)67 private void publishIfNeeded(VmsSubscriptionState subscriptionState) { 68 for (VmsLayer layer : subscriptionState.getLayers()) { 69 if (layer.equals(VmsPublisherSubscriberTest.LAYER)) { 70 publish(VmsPublisherSubscriberTest.LAYER, 71 VmsPublisherSubscriberTest.EXPECTED_PUBLISHER_ID, 72 VmsPublisherSubscriberTest.PAYLOAD); 73 } 74 } 75 } 76 declareOffering(VmsSubscriptionState subscriptionState, int publisherId)77 private void declareOffering(VmsSubscriptionState subscriptionState, int publisherId) { 78 Set<VmsLayerDependency> dependencies = new HashSet<>(); 79 80 // Add all layers from the subscription state. 81 for( VmsLayer layer : subscriptionState.getLayers()) { 82 dependencies.add(new VmsLayerDependency(layer)); 83 } 84 85 // Add default test layers. 86 dependencies.add(new VmsLayerDependency(VmsPublisherSubscriberTest.LAYER)); 87 88 VmsLayersOffering offering = new VmsLayersOffering(dependencies, publisherId); 89 setLayersOffering(offering); 90 } 91 } 92