Home | History | Annotate | Download | only in statsd
      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 package com.android.server.cts.device.statsd;
     17 
     18 import android.accounts.Account;
     19 import android.content.AbstractThreadedSyncAdapter;
     20 import android.content.ContentProviderClient;
     21 import android.content.ContentResolver;
     22 import android.content.Context;
     23 import android.content.SyncResult;
     24 import android.os.Bundle;
     25 import android.os.SystemClock;
     26 import android.util.Log;
     27 
     28 import org.junit.Assert;
     29 
     30 import java.util.concurrent.CountDownLatch;
     31 
     32 import javax.annotation.concurrent.GuardedBy;
     33 
     34 /**
     35  * Sync adapter for the sync test.
     36  */
     37 public class StatsdSyncAdapter extends AbstractThreadedSyncAdapter {
     38     private static final String TAG = "AtomTestsSyncAdapter";
     39 
     40     private static final int TIMEOUT_SECONDS = 60 * 2;
     41 
     42     private static CountDownLatch sLatch;
     43 
     44     private static final Object sLock = new Object();
     45 
     46 
     47     public StatsdSyncAdapter(Context context) {
     48         // No need for auto-initialization because we set isSyncable in the test anyway.
     49         super(context, /* autoInitialize= */ false);
     50     }
     51 
     52     @Override
     53     public void onPerformSync(Account account, Bundle extras, String authority,
     54             ContentProviderClient provider, SyncResult syncResult) {
     55         try {
     56             Thread.sleep(500);
     57         } catch (InterruptedException e) {
     58         }
     59         synchronized (sLock) {
     60             Log.i(TAG, "onPerformSync");
     61             sLock.notifyAll();
     62             if (sLatch != null) {
     63                 sLatch.countDown();
     64             } else {
     65                 Log.w(TAG, "sLatch is null, resetCountDownLatch probably should have been called");
     66             }
     67         }
     68     }
     69 
     70     /**
     71      * Request a sync on the given account, and wait for it.
     72      */
     73     public static void requestSync(Account account) throws Exception {
     74         final Bundle extras = new Bundle();
     75         extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
     76         extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true);
     77         extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
     78 
     79         ContentResolver.requestSync(account, StatsdProvider.AUTHORITY, extras);
     80     }
     81 
     82     public static synchronized CountDownLatch resetCountDownLatch() {
     83         sLatch = new CountDownLatch(1);
     84         return sLatch;
     85     }
     86 }
     87