blob: af891f0074734ed8da93abc0d4a4222d128e2e78 [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 Mukhamejanov75f40732018-12-14 09:36:32 -080021import static org.mockito.Mockito.doAnswer;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070022import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.when;
26
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070027import android.content.Context;
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080028import android.content.Intent;
29import android.os.Bundle;
30import android.os.ResultReceiver;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070031import android.os.UpdateEngine;
32import android.os.UpdateEngineCallback;
koushik panugantid5c7fb52018-12-12 10:39:44 -080033
34import androidx.test.InstrumentationRegistry;
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080035import androidx.test.annotation.UiThreadTest;
koushik panugantid5c7fb52018-12-12 10:39:44 -080036import androidx.test.filters.SmallTest;
37import androidx.test.runner.AndroidJUnit4;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070038
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080039import com.example.android.systemupdatersample.services.PrepareUpdateService;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070040import com.example.android.systemupdatersample.tests.R;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070041import com.google.common.collect.ImmutableList;
42import com.google.common.io.CharStreams;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070043
44import org.junit.Before;
45import org.junit.Rule;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48import org.mockito.Mock;
49import org.mockito.junit.MockitoJUnit;
50import org.mockito.junit.MockitoRule;
51
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070052import java.io.IOException;
53import java.io.InputStreamReader;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070054
55/**
56 * Tests for {@link UpdateManager}
57 */
58@RunWith(AndroidJUnit4.class)
59@SmallTest
60public class UpdateManagerTest {
61
62 @Rule
63 public MockitoRule mockito = MockitoJUnit.rule();
64
65 @Mock
66 private UpdateEngine mUpdateEngine;
67 @Mock
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080068 private Context mMockContext;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070069 private UpdateManager mSubject;
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080070 private Context mTestContext;
71 private UpdateConfig mStreamingUpdate002;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070072
73 @Before
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070074 public void setUp() throws Exception {
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080075 mTestContext = InstrumentationRegistry.getContext();
76 mSubject = new UpdateManager(mUpdateEngine, null);
77 mStreamingUpdate002 =
78 UpdateConfig.fromJson(readResource(R.raw.update_config_002_stream));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070079 }
80
81 @Test
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070082 public void applyUpdate_appliesPayloadToUpdateEngine() throws Exception {
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080083 mockContextStartServiceAnswer(buildMockPayloadSpec());
84 mSubject.applyUpdate(mMockContext, mStreamingUpdate002);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070085
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070086 verify(mUpdateEngine).applyPayload(
87 "file://blah",
88 120,
89 340,
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080090 new String[]{
91 "SWITCH_SLOT_ON_REBOOT=0", // ab_config.force_switch_slot = false
92 "USER_AGENT=" + UpdateManager.HTTP_USER_AGENT
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070093 });
94 }
95
96 @Test
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -080097 @UiThreadTest
98 public void stateIsRunningAndEngineStatusIsIdle_reApplyLastUpdate() throws Throwable {
99 mockContextStartServiceAnswer(buildMockPayloadSpec());
100 // UpdateEngine always returns IDLE status.
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700101 when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> {
102 // When UpdateManager is bound to update_engine, it passes
103 // UpdateEngineCallback as a callback to update_engine.
104 UpdateEngineCallback callback = answer.getArgument(0);
105 callback.onStatusUpdate(
106 UpdateEngine.UpdateStatusConstants.IDLE,
107 /*engineProgress*/ 0.0f);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700108 return null;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700109 });
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700110
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700111 mSubject.bind();
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -0800112 mSubject.applyUpdate(mMockContext, mStreamingUpdate002);
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700113 mSubject.unbind();
114 mSubject.bind(); // re-bind - now it should re-apply last update
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700115
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700116 assertEquals(mSubject.getUpdaterState(), UpdaterState.RUNNING);
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700117 verify(mUpdateEngine, times(2)).applyPayload(
118 "file://blah",
119 120,
120 340,
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -0800121 new String[]{
122 "SWITCH_SLOT_ON_REBOOT=0", // ab_config.force_switch_slot = false
123 "USER_AGENT=" + UpdateManager.HTTP_USER_AGENT
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700124 });
125 }
126
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -0800127 private void mockContextStartServiceAnswer(PayloadSpec payloadSpec) {
128 doAnswer(args -> {
129 Intent intent = args.getArgument(0);
130 ResultReceiver resultReceiver = intent.getParcelableExtra(
131 PrepareUpdateService.EXTRA_PARAM_RESULT_RECEIVER);
132 Bundle b = new Bundle();
133 b.putSerializable(
134 /* PrepareUpdateService.CallbackResultReceiver.BUNDLE_PARAM_PAYLOAD_SPEC */
135 "payload-spec",
136 payloadSpec);
137 resultReceiver.send(PrepareUpdateService.RESULT_CODE_SUCCESS, b);
138 return null;
139 }).when(mMockContext).startService(any(Intent.class));
140 }
141
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700142 private PayloadSpec buildMockPayloadSpec() {
143 PayloadSpec payload = mock(PayloadSpec.class);
144 when(payload.getUrl()).thenReturn("file://blah");
145 when(payload.getOffset()).thenReturn(120L);
146 when(payload.getSize()).thenReturn(340L);
147 when(payload.getProperties()).thenReturn(ImmutableList.of());
148 return payload;
149 }
150
151 private String readResource(int id) throws IOException {
152 return CharStreams.toString(new InputStreamReader(
Zhomart Mukhamejanov75f40732018-12-14 09:36:32 -0800153 mTestContext.getResources().openRawResource(id)));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700154 }
155
156}