Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright (C) 2018 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 static com.android.car.CarConfigurationService.DEFAULT_SPEED_BUMP_ACQUIRED_PERMITS_PER_SECOND;
     20 import static com.android.car.CarConfigurationService.DEFAULT_SPEED_BUMP_MAX_PERMIT_POOL;
     21 import static com.android.car.CarConfigurationService.DEFAULT_SPEED_BUMP_PERMIT_FILL_DELAY;
     22 import static com.android.car.CarConfigurationService.SPEED_BUMP_ACQUIRED_PERMITS_PER_SECOND_KEY;
     23 import static com.android.car.CarConfigurationService.SPEED_BUMP_CONFIG_KEY;
     24 import static com.android.car.CarConfigurationService.SPEED_BUMP_MAX_PERMIT_POOL_KEY;
     25 import static com.android.car.CarConfigurationService.SPEED_BUMP_PERMIT_FILL_DELAY_KEY;
     26 
     27 import static com.google.common.truth.Truth.assertThat;
     28 
     29 import android.car.settings.SpeedBumpConfiguration;
     30 import android.support.test.InstrumentationRegistry;
     31 import android.support.test.runner.AndroidJUnit4;
     32 
     33 import org.json.JSONException;
     34 import org.json.JSONObject;
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 import org.mockito.MockitoAnnotations;
     39 
     40 /**
     41  * Tests for {@link CarConfigurationService}.
     42  */
     43 @RunWith(AndroidJUnit4.class)
     44 public class CarConfigurationServiceTest {
     45     @Before
     46     public void setUp() {
     47         MockitoAnnotations.initMocks(this);
     48     }
     49 
     50     @Test
     51     public void testJsonResourceSuccessfullyRead() {
     52         // Use the default JsonReader to check that the resource JSON can be retrieved.
     53         CarConfigurationService service = new CarConfigurationService(
     54                 InstrumentationRegistry.getTargetContext(),
     55                 new JsonReaderImpl());
     56         service.init();
     57 
     58         assertThat(service.mConfigFile).isNotNull();
     59 
     60         // Default values should be stored in the JSON file as well.
     61         SpeedBumpConfiguration expectedConfiguration = new SpeedBumpConfiguration(
     62                 DEFAULT_SPEED_BUMP_ACQUIRED_PERMITS_PER_SECOND,
     63                 DEFAULT_SPEED_BUMP_MAX_PERMIT_POOL,
     64                 DEFAULT_SPEED_BUMP_PERMIT_FILL_DELAY);
     65 
     66         assertThat(service.getSpeedBumpConfiguration()).isEqualTo(expectedConfiguration);
     67     }
     68 
     69     @Test
     70     public void testJsonFileSuccessfullyRead() {
     71         // Return an empty json file.
     72         CarConfigurationService service = new CarConfigurationService(
     73                 InstrumentationRegistry.getTargetContext(),
     74                 (context, resId) -> "{}");
     75         service.init();
     76 
     77         // The configuration should still be initialized.
     78         assertThat(service.mConfigFile).isNotNull();
     79     }
     80 
     81     @Test
     82     public void testNullJsonStringResultsInNullConfigFile() {
     83         // Return null as the string representation.
     84         CarConfigurationService service = new CarConfigurationService(
     85                 InstrumentationRegistry.getTargetContext(),
     86                 (context, resId) -> null);
     87         service.init();
     88 
     89         // No config file should be created.
     90         assertThat(service.mConfigFile).isNull();
     91     }
     92 
     93     @Test
     94     public void testSpeedBumpConfigurationSuccessfullyRead() throws JSONException {
     95         double acquiredPermitsPerSecond = 5d;
     96         double maxPermitPool = 10d;
     97         long permitFillDelay = 500L;
     98 
     99         String jsonFile = new SpeedBumpJsonBuilder()
    100                 .setAcquiredPermitsPerSecond(acquiredPermitsPerSecond)
    101                 .setMaxPermitPool(maxPermitPool)
    102                 .setPermitFillDelay(permitFillDelay)
    103                 .build();
    104 
    105         CarConfigurationService service = new CarConfigurationService(
    106                 InstrumentationRegistry.getTargetContext(),
    107                 (context, resId) -> jsonFile);
    108         service.init();
    109 
    110         SpeedBumpConfiguration expectedConfiguration = new SpeedBumpConfiguration(
    111                 acquiredPermitsPerSecond,
    112                 maxPermitPool,
    113                 permitFillDelay);
    114 
    115         assertThat(service.getSpeedBumpConfiguration()).isEqualTo(expectedConfiguration);
    116     }
    117 
    118     @Test
    119     public void testDefaultSpeedBumpConfigurationReturned() {
    120         // Return null as the JSON representation.
    121         CarConfigurationService service = new CarConfigurationService(
    122                 InstrumentationRegistry.getTargetContext(),
    123                 (context, resId) -> null);
    124         service.init();
    125 
    126         // Default values should be used.
    127         SpeedBumpConfiguration expectedConfiguration = new SpeedBumpConfiguration(
    128                 DEFAULT_SPEED_BUMP_ACQUIRED_PERMITS_PER_SECOND,
    129                 DEFAULT_SPEED_BUMP_MAX_PERMIT_POOL,
    130                 DEFAULT_SPEED_BUMP_PERMIT_FILL_DELAY);
    131 
    132         assertThat(service.getSpeedBumpConfiguration()).isEqualTo(expectedConfiguration);
    133     }
    134 
    135     /**
    136      * Builder for a string that represents the contents of a JSON file with speed bump
    137      * configuration.
    138      */
    139     private class SpeedBumpJsonBuilder {
    140         private double mAcquiredPermitsPerSecond;
    141         private double mMaxPermitPool;
    142         private long mPermitFillDelay;
    143 
    144         SpeedBumpJsonBuilder setAcquiredPermitsPerSecond(double acquiredPermitsPerSecond) {
    145             mAcquiredPermitsPerSecond = acquiredPermitsPerSecond;
    146             return this;
    147         }
    148 
    149         SpeedBumpJsonBuilder setMaxPermitPool(double maxPermitPool) {
    150             mMaxPermitPool = maxPermitPool;
    151             return this;
    152         }
    153 
    154         SpeedBumpJsonBuilder setPermitFillDelay(long permitFillDelay) {
    155             mPermitFillDelay = permitFillDelay;
    156             return this;
    157         }
    158 
    159         String build() throws JSONException {
    160             JSONObject speedBump = new JSONObject();
    161             speedBump.put(SPEED_BUMP_ACQUIRED_PERMITS_PER_SECOND_KEY, mAcquiredPermitsPerSecond);
    162             speedBump.put(SPEED_BUMP_MAX_PERMIT_POOL_KEY, mMaxPermitPool);
    163             speedBump.put(SPEED_BUMP_PERMIT_FILL_DELAY_KEY, mPermitFillDelay);
    164 
    165             JSONObject container = new JSONObject();
    166             container.put(SPEED_BUMP_CONFIG_KEY, speedBump);
    167 
    168             return container.toString();
    169         }
    170     }
    171 }
    172