Home | History | Annotate | Download | only in testing
      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.server.backup.testing;
     18 
     19 import static org.mockito.Mockito.mock;
     20 import static org.mockito.Mockito.when;
     21 
     22 import android.app.Application;
     23 import android.app.IActivityManager;
     24 import android.content.Context;
     25 import android.content.pm.PackageManager;
     26 import android.os.HandlerThread;
     27 import android.os.Looper;
     28 import android.os.PowerManager;
     29 import android.util.SparseArray;
     30 
     31 import com.android.server.backup.BackupAgentTimeoutParameters;
     32 import com.android.server.backup.BackupManagerService;
     33 import com.android.server.backup.TransportManager;
     34 import com.android.server.backup.internal.BackupHandler;
     35 
     36 import java.lang.Thread.UncaughtExceptionHandler;
     37 
     38 /** Test utils for {@link BackupManagerService} and friends. */
     39 public class BackupManagerServiceTestUtils {
     40     /** Sets up basic mocks for {@link BackupManagerService}. */
     41     public static void setUpBackupManagerServiceBasics(
     42             BackupManagerService backupManagerService,
     43             Context context,
     44             TransportManager transportManager,
     45             PackageManager packageManager,
     46             BackupHandler backupHandler,
     47             PowerManager.WakeLock wakeLock,
     48             BackupAgentTimeoutParameters agentTimeoutParameters) {
     49         when(backupManagerService.getContext()).thenReturn(context);
     50         when(backupManagerService.getTransportManager()).thenReturn(transportManager);
     51         when(backupManagerService.getPackageManager()).thenReturn(packageManager);
     52         when(backupManagerService.getBackupHandler()).thenReturn(backupHandler);
     53         when(backupManagerService.getCurrentOpLock()).thenReturn(new Object());
     54         when(backupManagerService.getQueueLock()).thenReturn(new Object());
     55         when(backupManagerService.getCurrentOperations()).thenReturn(new SparseArray<>());
     56         when(backupManagerService.getActivityManager()).thenReturn(mock(IActivityManager.class));
     57         when(backupManagerService.getWakelock()).thenReturn(wakeLock);
     58         when(backupManagerService.getAgentTimeoutParameters()).thenReturn(agentTimeoutParameters);
     59     }
     60 
     61     public static PowerManager.WakeLock createBackupWakeLock(Application application) {
     62         PowerManager powerManager =
     63                 (PowerManager) application.getSystemService(Context.POWER_SERVICE);
     64         return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*");
     65     }
     66 
     67     /**
     68      * Creates a backup thread associated with a looper, starts it and returns its looper for
     69      * shadowing and creation of the backup handler.
     70      *
     71      * <p>Note that Robolectric simulates multi-thread in a single-thread to avoid flakiness, so
     72      * even though we started the thread, you should control its execution via the shadow of the
     73      * looper returned.
     74      *
     75      * @return The {@link Looper} for the backup thread.
     76      */
     77     public static Looper startBackupThreadAndGetLooper() {
     78         HandlerThread backupThread = new HandlerThread("backup");
     79         backupThread.start();
     80         return backupThread.getLooper();
     81     }
     82 
     83     /**
     84      * Similar to {@link #startBackupThreadAndGetLooper()} but with a custom exception handler and
     85      * returning the thread instead of the looper associated with it.
     86      *
     87      * @param exceptionHandler Uncaught exception handler for backup thread.
     88      * @return The backup thread.
     89      * @see #startBackupThreadAndGetLooper()
     90      */
     91     public static HandlerThread startBackupThread(UncaughtExceptionHandler exceptionHandler) {
     92         HandlerThread backupThread = new HandlerThread("backup");
     93         backupThread.setUncaughtExceptionHandler(exceptionHandler);
     94         backupThread.start();
     95         return backupThread;
     96     }
     97 
     98     private BackupManagerServiceTestUtils() {}
     99 }
    100