1 /* 2 * Copyright (C) 2023 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.providers.media.photopicker.sync; 18 19 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_CLOUD_ONLY; 20 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_LOCAL_AND_CLOUD; 21 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_LOCAL_ONLY; 22 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_RESET_ALBUM; 23 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_WORKER_INPUT_ALBUM_ID; 24 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_WORKER_INPUT_AUTHORITY; 25 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_WORKER_INPUT_RESET_TYPE; 26 import static com.android.providers.media.photopicker.sync.PickerSyncManager.SYNC_WORKER_INPUT_SYNC_SOURCE; 27 28 import android.content.Context; 29 import android.util.Log; 30 31 import androidx.annotation.NonNull; 32 import androidx.work.Configuration; 33 import androidx.work.Data; 34 import androidx.work.Worker; 35 import androidx.work.testing.SynchronousExecutor; 36 import androidx.work.testing.TestWorkerBuilder; 37 import androidx.work.testing.WorkManagerTestInitHelper; 38 39 import java.util.Map; 40 import java.util.Objects; 41 42 public class SyncWorkerTestUtils { initializeTestWorkManager(@onNull Context context)43 public static void initializeTestWorkManager(@NonNull Context context) { 44 Configuration workManagerConfig = new Configuration.Builder() 45 .setMinimumLoggingLevel(Log.DEBUG) 46 .setExecutor(new SynchronousExecutor()) // This runs WM synchronously. 47 .build(); 48 49 WorkManagerTestInitHelper.initializeTestWorkManager( 50 context, workManagerConfig); 51 } 52 53 @NonNull getLocalSyncInputData()54 public static Data getLocalSyncInputData() { 55 return new Data(Map.of(SYNC_WORKER_INPUT_SYNC_SOURCE, SYNC_LOCAL_ONLY)); 56 } 57 58 @NonNull getLocalAlbumSyncInputData(@onNull String albumId)59 public static Data getLocalAlbumSyncInputData(@NonNull String albumId) { 60 Objects.requireNonNull(albumId); 61 return new Data(Map.of(SYNC_WORKER_INPUT_SYNC_SOURCE, SYNC_LOCAL_ONLY, 62 SYNC_WORKER_INPUT_ALBUM_ID, albumId)); 63 } 64 65 @NonNull getCloudSyncInputData()66 public static Data getCloudSyncInputData() { 67 return new Data(Map.of(SYNC_WORKER_INPUT_SYNC_SOURCE, SYNC_CLOUD_ONLY)); 68 } 69 70 @NonNull getAlbumResetInputData( @onNull String albumId, String authority, boolean isLocal)71 public static Data getAlbumResetInputData( 72 @NonNull String albumId, String authority, boolean isLocal) { 73 Objects.requireNonNull(albumId); 74 Objects.requireNonNull(authority); 75 return new Data( 76 Map.of( 77 SYNC_WORKER_INPUT_AUTHORITY, authority, 78 SYNC_WORKER_INPUT_SYNC_SOURCE, isLocal ? SYNC_LOCAL_ONLY : SYNC_CLOUD_ONLY, 79 SYNC_WORKER_INPUT_RESET_TYPE, SYNC_RESET_ALBUM, 80 SYNC_WORKER_INPUT_ALBUM_ID, albumId)); 81 } 82 83 @NonNull getCloudAlbumSyncInputData(@onNull String albumId)84 public static Data getCloudAlbumSyncInputData(@NonNull String albumId) { 85 Objects.requireNonNull(albumId); 86 return new Data(Map.of(SYNC_WORKER_INPUT_SYNC_SOURCE, SYNC_CLOUD_ONLY, 87 SYNC_WORKER_INPUT_ALBUM_ID, albumId)); 88 } 89 90 @NonNull getLocalAndCloudSyncInputData()91 public static Data getLocalAndCloudSyncInputData() { 92 return new Data(Map.of(SYNC_WORKER_INPUT_SYNC_SOURCE, SYNC_LOCAL_AND_CLOUD)); 93 } 94 getLocalAndCloudAlbumSyncInputData(@onNull String albumId)95 public static Data getLocalAndCloudAlbumSyncInputData(@NonNull String albumId) { 96 Objects.requireNonNull(albumId); 97 return new Data(Map.of(SYNC_WORKER_INPUT_SYNC_SOURCE, SYNC_LOCAL_AND_CLOUD, 98 SYNC_WORKER_INPUT_ALBUM_ID, albumId)); 99 } 100 buildTestWorker(@onNull Context context, @NonNull Class<W> workerClass)101 static <W extends Worker> W buildTestWorker(@NonNull Context context, 102 @NonNull Class<W> workerClass) { 103 return TestWorkerBuilder.from(context, workerClass) 104 .setInputData(getLocalAndCloudSyncInputData()) 105 .build(); 106 } 107 } 108