blob: 6bef6de0e513e872a36eda6d3291f8cb42732ca6 [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;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070021import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070026import android.content.Context;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070027import android.os.UpdateEngine;
28import android.os.UpdateEngineCallback;
koushik panugantid5c7fb52018-12-12 10:39:44 -080029
30import androidx.test.InstrumentationRegistry;
31import androidx.test.filters.SmallTest;
32import androidx.test.runner.AndroidJUnit4;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070033
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070034import com.example.android.systemupdatersample.tests.R;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070035import com.example.android.systemupdatersample.util.PayloadSpecs;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070036import com.google.common.collect.ImmutableList;
37import com.google.common.io.CharStreams;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070038
39import org.junit.Before;
40import org.junit.Rule;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43import org.mockito.Mock;
44import org.mockito.junit.MockitoJUnit;
45import org.mockito.junit.MockitoRule;
46
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070047import java.io.File;
48import java.io.IOException;
49import java.io.InputStreamReader;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070050
51/**
52 * Tests for {@link UpdateManager}
53 */
54@RunWith(AndroidJUnit4.class)
55@SmallTest
56public class UpdateManagerTest {
57
58 @Rule
59 public MockitoRule mockito = MockitoJUnit.rule();
60
61 @Mock
62 private UpdateEngine mUpdateEngine;
63 @Mock
64 private PayloadSpecs mPayloadSpecs;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070065 private UpdateManager mSubject;
66 private Context mContext;
67 private UpdateConfig mNonStreamingUpdate003;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070068
69 @Before
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070070 public void setUp() throws Exception {
71 mContext = InstrumentationRegistry.getContext();
72 mSubject = new UpdateManager(mUpdateEngine, mPayloadSpecs);
73 mNonStreamingUpdate003 =
74 UpdateConfig.fromJson(readResource(R.raw.update_config_003_nonstream));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070075 }
76
77 @Test
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070078 public void applyUpdate_appliesPayloadToUpdateEngine() throws Exception {
79 PayloadSpec payload = buildMockPayloadSpec();
80 when(mPayloadSpecs.forNonStreaming(any(File.class))).thenReturn(payload);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070081 when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> {
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070082 // When UpdateManager is bound to update_engine, it passes
83 // UpdateEngineCallback as a callback to update_engine.
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070084 UpdateEngineCallback callback = answer.getArgument(0);
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070085 callback.onStatusUpdate(
86 UpdateEngine.UpdateStatusConstants.IDLE,
87 /*engineProgress*/ 0.0f);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070088 return null;
89 });
90
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070091 mSubject.bind();
92 mSubject.applyUpdate(null, mNonStreamingUpdate003);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070093
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070094 verify(mUpdateEngine).applyPayload(
95 "file://blah",
96 120,
97 340,
98 new String[] {
99 "SWITCH_SLOT_ON_REBOOT=0" // ab_config.force_switch_slot = false
100 });
101 }
102
103 @Test
104 public void stateIsRunningAndEngineStatusIsIdle_reApplyLastUpdate() throws Exception {
105 PayloadSpec payload = buildMockPayloadSpec();
106 when(mPayloadSpecs.forNonStreaming(any(File.class))).thenReturn(payload);
107 when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> {
108 // When UpdateManager is bound to update_engine, it passes
109 // UpdateEngineCallback as a callback to update_engine.
110 UpdateEngineCallback callback = answer.getArgument(0);
111 callback.onStatusUpdate(
112 UpdateEngine.UpdateStatusConstants.IDLE,
113 /*engineProgress*/ 0.0f);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700114 return null;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700115 });
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700116
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700117 mSubject.bind();
118 mSubject.applyUpdate(null, mNonStreamingUpdate003);
119 mSubject.unbind();
120 mSubject.bind(); // re-bind - now it should re-apply last update
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700121
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700122 assertEquals(mSubject.getUpdaterState(), UpdaterState.RUNNING);
123 // it should be called 2 times
124 verify(mUpdateEngine, times(2)).applyPayload(
125 "file://blah",
126 120,
127 340,
128 new String[] {
129 "SWITCH_SLOT_ON_REBOOT=0" // ab_config.force_switch_slot = false
130 });
131 }
132
133 private PayloadSpec buildMockPayloadSpec() {
134 PayloadSpec payload = mock(PayloadSpec.class);
135 when(payload.getUrl()).thenReturn("file://blah");
136 when(payload.getOffset()).thenReturn(120L);
137 when(payload.getSize()).thenReturn(340L);
138 when(payload.getProperties()).thenReturn(ImmutableList.of());
139 return payload;
140 }
141
142 private String readResource(int id) throws IOException {
143 return CharStreams.toString(new InputStreamReader(
144 mContext.getResources().openRawResource(id)));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700145 }
146
147}