blob: 0657a5eb6d85f1ba458324701b8b50f8c0ff0981 [file] [log] [blame]
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -07001/*
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
17package com.example.android.systemupdatersample;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.ArgumentMatchers.anyInt;
22import static org.mockito.Mockito.doAnswer;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
27
28import android.os.UpdateEngine;
29import android.os.UpdateEngineCallback;
30import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32
33import com.example.android.systemupdatersample.util.PayloadSpecs;
34
35import org.junit.Before;
36import org.junit.Rule;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.junit.MockitoJUnit;
41import org.mockito.junit.MockitoRule;
42
43import java.util.function.IntConsumer;
44
45/**
46 * Tests for {@link UpdateManager}
47 */
48@RunWith(AndroidJUnit4.class)
49@SmallTest
50public 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}