Home | History | Annotate | Download | only in batterystats
      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 package com.android.server.cts.device.batterystats;
     17 
     18 import android.accounts.Account;
     19 import android.content.ContentResolver;
     20 import android.content.Context;
     21 import android.support.test.InstrumentationRegistry;
     22 import android.support.test.runner.AndroidJUnit4;
     23 
     24 import org.junit.After;
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 
     29 @RunWith(AndroidJUnit4.class)
     30 public class BatteryStatsSyncTest {
     31     private static final String TAG = "BatteryStatsSyncTest";
     32 
     33     private static Context getContext() {
     34         return InstrumentationRegistry.getContext();
     35     }
     36 
     37     @Before
     38     public void setUp() {
     39         BatteryStatsAuthenticator.removeAllAccounts(getContext());
     40     }
     41 
     42     @After
     43     public void tearDown() {
     44         BatteryStatsAuthenticator.removeAllAccounts(getContext());
     45     }
     46 
     47     /**
     48      * Run a sync N times and make sure it shows up in the battery stats.
     49      */
     50     @Test
     51     public void testRunSyncs() throws Exception {
     52         final Account account = BatteryStatsAuthenticator.getTestAccount();
     53 
     54         // Create the test account.
     55         BatteryStatsAuthenticator.ensureTestAccount(getContext());
     56 
     57         // Just force set is syncable.
     58         ContentResolver.setMasterSyncAutomatically(true);
     59         ContentResolver.setIsSyncable(account, BatteryStatsProvider.AUTHORITY, 1);
     60 
     61         // Cancel the initial sync.
     62         BatteryStatsSyncAdapter.cancelPendingSyncs(account);
     63 
     64         final int NUM_SYNC = 10;
     65 
     66         // Run syncs.
     67         for (int i = 0; i < NUM_SYNC; i++) {
     68             BatteryStatsSyncAdapter.requestSync(account);
     69             Thread.sleep(1000);
     70         }
     71     }
     72 }
     73