1 /*
2  * Copyright (C) 2021 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.transport;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.backup.BackupTransport;
22 import android.platform.test.annotations.Presubmit;
23 
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 @Presubmit
31 @RunWith(AndroidJUnit4.class)
32 public class TransportStatusCallbackTest {
33     private static final int OPERATION_TIMEOUT_MILLIS = 10;
34     private static final int OPERATION_COMPLETE_STATUS = 123;
35 
36     private TransportStatusCallback mTransportStatusCallback;
37 
38     @Before
setUp()39     public void setUp() {
40         mTransportStatusCallback = new TransportStatusCallback();
41     }
42 
43     @Test
testGetOperationStatus_withPreCompletedOperation_returnsStatus()44     public void testGetOperationStatus_withPreCompletedOperation_returnsStatus() throws Exception {
45         mTransportStatusCallback.onOperationCompleteWithStatus(OPERATION_COMPLETE_STATUS);
46 
47         int result = mTransportStatusCallback.getOperationStatus();
48 
49         assertThat(result).isEqualTo(OPERATION_COMPLETE_STATUS);
50     }
51 
52     @Test
testGetOperationStatus_completeOperation_returnsStatus()53     public void testGetOperationStatus_completeOperation_returnsStatus() throws Exception {
54         Thread thread = new Thread(() -> {
55             int result = mTransportStatusCallback.getOperationStatus();
56             assertThat(result).isEqualTo(OPERATION_COMPLETE_STATUS);
57         });
58         thread.start();
59 
60         mTransportStatusCallback.onOperationCompleteWithStatus(OPERATION_COMPLETE_STATUS);
61 
62         thread.join();
63     }
64 
65     @Test
testGetOperationStatus_operationTimesOut_returnsError()66     public void testGetOperationStatus_operationTimesOut_returnsError() throws Exception {
67         TransportStatusCallback callback = new TransportStatusCallback(OPERATION_TIMEOUT_MILLIS);
68 
69         int result = callback.getOperationStatus();
70 
71         assertThat(result).isEqualTo(BackupTransport.TRANSPORT_ERROR);
72     }
73 }
74