Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 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.example.android.systemupdatersample; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.mockito.ArgumentMatchers.any; |
| 21 | import static org.mockito.ArgumentMatchers.anyInt; |
| 22 | import static org.mockito.Mockito.doAnswer; |
| 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.times; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.os.UpdateEngine; |
| 29 | import android.os.UpdateEngineCallback; |
| 30 | import android.support.test.filters.SmallTest; |
| 31 | import android.support.test.runner.AndroidJUnit4; |
| 32 | |
| 33 | import com.example.android.systemupdatersample.util.PayloadSpecs; |
| 34 | |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Rule; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.mockito.Mock; |
| 40 | import org.mockito.junit.MockitoJUnit; |
| 41 | import org.mockito.junit.MockitoRule; |
| 42 | |
| 43 | import java.util.function.IntConsumer; |
| 44 | |
| 45 | /** |
| 46 | * Tests for {@link UpdateManager} |
| 47 | */ |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | @SmallTest |
| 50 | public class UpdateManagerTest { |
| 51 | |
| 52 | @Rule |
| 53 | public MockitoRule mockito = MockitoJUnit.rule(); |
| 54 | |
| 55 | @Mock |
| 56 | private UpdateEngine mUpdateEngine; |
| 57 | @Mock |
| 58 | private PayloadSpecs mPayloadSpecs; |
| 59 | private UpdateManager mUpdateManager; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | mUpdateManager = new UpdateManager(mUpdateEngine, mPayloadSpecs); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void storesProgressThenInvokesCallbacks() { |
| 68 | IntConsumer statusUpdateCallback = mock(IntConsumer.class); |
| 69 | |
| 70 | // When UpdateManager is bound to update_engine, it passes |
| 71 | // UpdateManager.UpdateEngineCallbackImpl as a callback to update_engine. |
| 72 | when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> { |
| 73 | UpdateEngineCallback callback = answer.getArgument(0); |
| 74 | callback.onStatusUpdate(/*engineStatus*/ 4, /*engineProgress*/ 0.2f); |
| 75 | return null; |
| 76 | }); |
| 77 | |
| 78 | mUpdateManager.setOnEngineStatusUpdateCallback(statusUpdateCallback); |
| 79 | |
| 80 | // Making sure that manager.getProgress() returns correct progress |
| 81 | // in "onEngineStatusUpdate" callback. |
| 82 | doAnswer(answer -> { |
| 83 | assertEquals(0.2f, mUpdateManager.getProgress(), 1E-5); |
| 84 | return null; |
| 85 | }).when(statusUpdateCallback).accept(anyInt()); |
| 86 | |
| 87 | mUpdateManager.bind(); |
| 88 | |
| 89 | verify(statusUpdateCallback, times(1)).accept(4); |
| 90 | } |
| 91 | |
| 92 | } |