Home | History | Annotate | Download | only in batterytip
      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.settings.fuelgauge.batterytip;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import static org.junit.Assert.assertEquals;
     22 import static org.mockito.Matchers.any;
     23 import static org.mockito.Matchers.anyInt;
     24 import static org.mockito.Matchers.anyString;
     25 import static org.mockito.Matchers.eq;
     26 import static org.mockito.Mockito.doReturn;
     27 import static org.mockito.Mockito.spy;
     28 import static org.mockito.Mockito.times;
     29 import static org.mockito.Mockito.verify;
     30 import static org.mockito.Mockito.when;
     31 import static org.robolectric.RuntimeEnvironment.application;
     32 
     33 import android.app.StatsManager;
     34 import android.app.job.JobInfo;
     35 import android.app.job.JobScheduler;
     36 import android.content.Context;
     37 import android.content.SharedPreferences;
     38 import android.provider.Settings;
     39 
     40 import com.android.settings.R;
     41 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     42 
     43 import org.junit.Before;
     44 import org.junit.Test;
     45 import org.junit.runner.RunWith;
     46 import org.mockito.Mock;
     47 import org.mockito.MockitoAnnotations;
     48 import org.robolectric.RuntimeEnvironment;
     49 import org.robolectric.Shadows;
     50 import org.robolectric.shadows.ShadowJobScheduler;
     51 
     52 import java.util.List;
     53 import java.util.concurrent.TimeUnit;
     54 
     55 @RunWith(SettingsRobolectricTestRunner.class)
     56 public class AnomalyConfigJobServiceTest {
     57 
     58     private static final int ANOMALY_CONFIG_VERSION = 1;
     59     private static final String ANOMALY_CONFIG = "X64s";
     60     @Mock
     61     private StatsManager mStatsManager;
     62 
     63     private Context mContext;
     64     private JobScheduler mJobScheduler;
     65     private AnomalyConfigJobService mJobService;
     66 
     67     @Before
     68     public void setUp() {
     69         MockitoAnnotations.initMocks(this);
     70 
     71         mContext = spy(RuntimeEnvironment.application);
     72         mJobScheduler = spy(mContext.getSystemService(JobScheduler.class));
     73         when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
     74 
     75         mJobService = spy(new AnomalyConfigJobService());
     76         doReturn(application.getSharedPreferences(AnomalyConfigJobService.PREF_DB,
     77                 Context.MODE_PRIVATE)).when(mJobService).getSharedPreferences(anyString(),
     78                 anyInt());
     79         doReturn(application.getContentResolver()).when(mJobService).getContentResolver();
     80     }
     81 
     82     @Test
     83     public void testScheduleConfigUpdate() {
     84         AnomalyConfigJobService.scheduleConfigUpdate(mContext);
     85 
     86         ShadowJobScheduler shadowJobScheduler =
     87                 Shadows.shadowOf(mContext.getSystemService(JobScheduler.class));
     88         List<JobInfo> pendingJobs = shadowJobScheduler.getAllPendingJobs();
     89         assertEquals(1, pendingJobs.size());
     90         JobInfo pendingJob = pendingJobs.get(0);
     91         assertThat(pendingJob.getId()).isEqualTo(R.integer.job_anomaly_config_update);
     92         assertThat(pendingJob.getIntervalMillis()).isEqualTo(TimeUnit.DAYS.toMillis(1));
     93         assertThat(pendingJob.isRequireDeviceIdle()).isTrue();
     94         assertThat(pendingJob.isRequireCharging()).isTrue();
     95         assertThat(pendingJob.isPersisted()).isTrue();
     96     }
     97 
     98     @Test
     99     public void testScheduleConfigUpdate_invokeTwice_onlyScheduleOnce() {
    100         AnomalyConfigJobService.scheduleConfigUpdate(mContext);
    101         AnomalyConfigJobService.scheduleConfigUpdate(mContext);
    102 
    103         verify(mJobScheduler, times(1)).schedule(any());
    104     }
    105 
    106     @Test
    107     public void checkAnomalyConfig_newConfigExist_removeOldConfig()
    108             throws StatsManager.StatsUnavailableException{
    109         Settings.Global.putInt(application.getContentResolver(),
    110                 Settings.Global.ANOMALY_CONFIG_VERSION, ANOMALY_CONFIG_VERSION);
    111         Settings.Global.putString(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG,
    112                 ANOMALY_CONFIG);
    113 
    114         mJobService.checkAnomalyConfig(mStatsManager);
    115 
    116         verify(mStatsManager).removeConfig(StatsManagerConfig.ANOMALY_CONFIG_KEY);
    117     }
    118 
    119     @Test
    120     public void checkAnomalyConfig_newConfigExist_uploadNewConfig()
    121             throws StatsManager.StatsUnavailableException{
    122         Settings.Global.putInt(application.getContentResolver(),
    123                 Settings.Global.ANOMALY_CONFIG_VERSION, ANOMALY_CONFIG_VERSION);
    124         Settings.Global.putString(application.getContentResolver(), Settings.Global.ANOMALY_CONFIG,
    125                 ANOMALY_CONFIG);
    126 
    127         mJobService.checkAnomalyConfig(mStatsManager);
    128 
    129         verify(mStatsManager).addConfig(eq(StatsManagerConfig.ANOMALY_CONFIG_KEY), any());
    130     }
    131 
    132 }
    133