blob: 5ad16d477caffb6d4002b53d88b88665895f8320 [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 Mukhamejanovbc077752018-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 Mukhamejanovbc077752018-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;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070033import android.support.test.InstrumentationRegistry;
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080034import android.support.test.annotation.UiThreadTest;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070035import android.support.test.filters.SmallTest;
36import android.support.test.runner.AndroidJUnit4;
37
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080038import com.example.android.systemupdatersample.services.PrepareUpdateService;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070039import com.example.android.systemupdatersample.tests.R;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070040import com.google.common.collect.ImmutableList;
41import com.google.common.io.CharStreams;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070042
43import org.junit.Before;
44import org.junit.Rule;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.Mock;
48import org.mockito.junit.MockitoJUnit;
49import org.mockito.junit.MockitoRule;
50
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070051import java.io.IOException;
52import java.io.InputStreamReader;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070053
54/**
55 * Tests for {@link UpdateManager}
56 */
57@RunWith(AndroidJUnit4.class)
58@SmallTest
59public class UpdateManagerTest {
60
61 @Rule
62 public MockitoRule mockito = MockitoJUnit.rule();
63
64 @Mock
65 private UpdateEngine mUpdateEngine;
66 @Mock
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080067 private Context mMockContext;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070068 private UpdateManager mSubject;
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080069 private Context mTestContext;
70 private UpdateConfig mStreamingUpdate002;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070071
72 @Before
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070073 public void setUp() throws Exception {
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080074 mTestContext = InstrumentationRegistry.getContext();
75 mSubject = new UpdateManager(mUpdateEngine, null);
76 mStreamingUpdate002 =
77 UpdateConfig.fromJson(readResource(R.raw.update_config_002_stream));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070078 }
79
80 @Test
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070081 public void applyUpdate_appliesPayloadToUpdateEngine() throws Exception {
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080082 mockContextStartServiceAnswer(buildMockPayloadSpec());
83 mSubject.applyUpdate(mMockContext, mStreamingUpdate002);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070084
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070085 verify(mUpdateEngine).applyPayload(
86 "file://blah",
87 120,
88 340,
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080089 new String[]{
90 "SWITCH_SLOT_ON_REBOOT=0", // ab_config.force_switch_slot = false
91 "USER_AGENT=" + UpdateManager.HTTP_USER_AGENT
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070092 });
93 }
94
95 @Test
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -080096 @UiThreadTest
97 public void stateIsRunningAndEngineStatusIsIdle_reApplyLastUpdate() throws Throwable {
98 mockContextStartServiceAnswer(buildMockPayloadSpec());
99 // UpdateEngine always returns IDLE status.
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700100 when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> {
101 // When UpdateManager is bound to update_engine, it passes
102 // UpdateEngineCallback as a callback to update_engine.
103 UpdateEngineCallback callback = answer.getArgument(0);
104 callback.onStatusUpdate(
105 UpdateEngine.UpdateStatusConstants.IDLE,
106 /*engineProgress*/ 0.0f);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700107 return null;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700108 });
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700109
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700110 mSubject.bind();
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -0800111 mSubject.applyUpdate(mMockContext, mStreamingUpdate002);
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700112 mSubject.unbind();
113 mSubject.bind(); // re-bind - now it should re-apply last update
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700114
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700115 assertEquals(mSubject.getUpdaterState(), UpdaterState.RUNNING);
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700116 verify(mUpdateEngine, times(2)).applyPayload(
117 "file://blah",
118 120,
119 340,
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -0800120 new String[]{
121 "SWITCH_SLOT_ON_REBOOT=0", // ab_config.force_switch_slot = false
122 "USER_AGENT=" + UpdateManager.HTTP_USER_AGENT
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700123 });
124 }
125
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -0800126 private void mockContextStartServiceAnswer(PayloadSpec payloadSpec) {
127 doAnswer(args -> {
128 Intent intent = args.getArgument(0);
129 ResultReceiver resultReceiver = intent.getParcelableExtra(
130 PrepareUpdateService.EXTRA_PARAM_RESULT_RECEIVER);
131 Bundle b = new Bundle();
132 b.putSerializable(
133 /* PrepareUpdateService.CallbackResultReceiver.BUNDLE_PARAM_PAYLOAD_SPEC */
134 "payload-spec",
135 payloadSpec);
136 resultReceiver.send(PrepareUpdateService.RESULT_CODE_SUCCESS, b);
137 return null;
138 }).when(mMockContext).startService(any(Intent.class));
139 }
140
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700141 private PayloadSpec buildMockPayloadSpec() {
142 PayloadSpec payload = mock(PayloadSpec.class);
143 when(payload.getUrl()).thenReturn("file://blah");
144 when(payload.getOffset()).thenReturn(120L);
145 when(payload.getSize()).thenReturn(340L);
146 when(payload.getProperties()).thenReturn(ImmutableList.of());
147 return payload;
148 }
149
150 private String readResource(int id) throws IOException {
151 return CharStreams.toString(new InputStreamReader(
Zhomart Mukhamejanovbc077752018-12-14 09:36:32 -0800152 mTestContext.getResources().openRawResource(id)));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700153 }
154
155}