|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.appdistribution.impl; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | + |
| 19 | +import com.google.firebase.appdistribution.UpdateProgress; |
| 20 | +import com.google.firebase.appdistribution.UpdateStatus; |
| 21 | +import com.google.firebase.concurrent.FirebaseExecutors; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.robolectric.RobolectricTestRunner; |
| 27 | + |
| 28 | +@RunWith(RobolectricTestRunner.class) |
| 29 | +public class UpdateTaskImplTest { |
| 30 | + |
| 31 | + private static final UpdateProgress PROGRESS_DOWNLOADING = |
| 32 | + UpdateProgressImpl.builder() |
| 33 | + .setUpdateStatus(UpdateStatus.DOWNLOADING) |
| 34 | + .setApkBytesDownloaded(100) |
| 35 | + .setApkFileTotalBytes(123) |
| 36 | + .build(); |
| 37 | + |
| 38 | + private static final UpdateProgress PROGRESS_DOWNLOADED = |
| 39 | + UpdateProgressImpl.builder() |
| 40 | + .setUpdateStatus(UpdateStatus.DOWNLOADED) |
| 41 | + .setApkBytesDownloaded(123) |
| 42 | + .setApkFileTotalBytes(123) |
| 43 | + .build(); |
| 44 | + |
| 45 | + @Test |
| 46 | + public void addOnProgressListener_supportsMultipleListeners() { |
| 47 | + UpdateTaskImpl updateTaskImpl = new UpdateTaskImpl(); |
| 48 | + List<UpdateProgress> progressEvents1 = new ArrayList<>(); |
| 49 | + updateTaskImpl.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents1::add); |
| 50 | + List<UpdateProgress> progressEvents2 = new ArrayList<>(); |
| 51 | + updateTaskImpl.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents2::add); |
| 52 | + |
| 53 | + updateTaskImpl.updateProgress(PROGRESS_DOWNLOADING); |
| 54 | + |
| 55 | + assertThat(progressEvents1).containsExactly(PROGRESS_DOWNLOADING); |
| 56 | + assertThat(progressEvents2).containsExactly(PROGRESS_DOWNLOADING); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void setException_clearsProgressListeners() { |
| 61 | + UpdateTaskImpl updateTaskImpl = new UpdateTaskImpl(); |
| 62 | + List<UpdateProgress> progressEvents1 = new ArrayList<>(); |
| 63 | + updateTaskImpl.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents1::add); |
| 64 | + List<UpdateProgress> progressEvents2 = new ArrayList<>(); |
| 65 | + updateTaskImpl.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents2::add); |
| 66 | + |
| 67 | + // Set the result |
| 68 | + updateTaskImpl.setException(new RuntimeException("Error")); |
| 69 | + |
| 70 | + // Has no effect on the listeners after the exception is set |
| 71 | + updateTaskImpl.updateProgress(PROGRESS_DOWNLOADING); |
| 72 | + |
| 73 | + assertThat(progressEvents1).isEmpty(); |
| 74 | + assertThat(progressEvents2).isEmpty(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void setResult_clearsProgressListeners() { |
| 79 | + UpdateTaskImpl updateTaskImpl = new UpdateTaskImpl(); |
| 80 | + List<UpdateProgress> progressEvents1 = new ArrayList<>(); |
| 81 | + updateTaskImpl.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents1::add); |
| 82 | + List<UpdateProgress> progressEvents2 = new ArrayList<>(); |
| 83 | + updateTaskImpl.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents2::add); |
| 84 | + |
| 85 | + // Set the result |
| 86 | + updateTaskImpl.setResult(); |
| 87 | + |
| 88 | + // Has no effect on the listeners after the result is set |
| 89 | + updateTaskImpl.updateProgress(PROGRESS_DOWNLOADING); |
| 90 | + |
| 91 | + assertThat(progressEvents1).isEmpty(); |
| 92 | + assertThat(progressEvents2).isEmpty(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void shadow_completesWithShadowedException() { |
| 97 | + UpdateTaskImpl taskToShadow = new UpdateTaskImpl(); |
| 98 | + UpdateTaskImpl updateTaskImpl = new UpdateTaskImpl(); |
| 99 | + |
| 100 | + // Shadow the task |
| 101 | + updateTaskImpl.shadow(taskToShadow); |
| 102 | + |
| 103 | + // Complete the shadowed task |
| 104 | + RuntimeException expectedException = new RuntimeException("Error"); |
| 105 | + taskToShadow.setException(expectedException); |
| 106 | + |
| 107 | + assertThat(updateTaskImpl.isComplete()).isEqualTo(true); |
| 108 | + assertThat(updateTaskImpl.isSuccessful()).isEqualTo(false); |
| 109 | + assertThat(updateTaskImpl.getException()).isEqualTo(expectedException); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void shadow_completesWithShadowedResult() { |
| 114 | + UpdateTaskImpl taskToShadow = new UpdateTaskImpl(); |
| 115 | + UpdateTaskImpl updateTaskImpl = new UpdateTaskImpl(); |
| 116 | + |
| 117 | + // Shadow the task |
| 118 | + updateTaskImpl.shadow(taskToShadow); |
| 119 | + |
| 120 | + // Complete the shadowed task |
| 121 | + taskToShadow.setResult(); |
| 122 | + |
| 123 | + assertThat(updateTaskImpl.isComplete()).isEqualTo(true); |
| 124 | + assertThat(updateTaskImpl.isSuccessful()).isEqualTo(true); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void shadow_updatesWithShadowedProgress() { |
| 129 | + UpdateTaskImpl taskToShadow = new UpdateTaskImpl(); |
| 130 | + UpdateTaskImpl updateTaskImpl = new UpdateTaskImpl(); |
| 131 | + List<UpdateProgress> progressEvents = new ArrayList<>(); |
| 132 | + updateTaskImpl.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add); |
| 133 | + |
| 134 | + // Shadow the task |
| 135 | + updateTaskImpl.shadow(taskToShadow); |
| 136 | + |
| 137 | + // Update the shadowed task |
| 138 | + taskToShadow.updateProgress(PROGRESS_DOWNLOADING); |
| 139 | + taskToShadow.updateProgress(PROGRESS_DOWNLOADED); |
| 140 | + |
| 141 | + assertThat(updateTaskImpl.isComplete()).isEqualTo(false); |
| 142 | + assertThat(progressEvents).containsExactly(PROGRESS_DOWNLOADING, PROGRESS_DOWNLOADED); |
| 143 | + } |
| 144 | +} |
0 commit comments