blob: e05ad290c366b37368a4a2c0fa0c2663bcfc2487 [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;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070029import android.support.test.InstrumentationRegistry;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070030import android.support.test.filters.SmallTest;
31import android.support.test.runner.AndroidJUnit4;
32
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070033import com.example.android.systemupdatersample.tests.R;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070034import com.example.android.systemupdatersample.util.PayloadSpecs;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070035import com.google.common.collect.ImmutableList;
36import com.google.common.io.CharStreams;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070037
38import org.junit.Before;
39import org.junit.Rule;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.Mock;
43import org.mockito.junit.MockitoJUnit;
44import org.mockito.junit.MockitoRule;
45
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070046import java.io.File;
47import java.io.IOException;
48import java.io.InputStreamReader;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070049
50/**
51 * Tests for {@link UpdateManager}
52 */
53@RunWith(AndroidJUnit4.class)
54@SmallTest
55public class UpdateManagerTest {
56
57 @Rule
58 public MockitoRule mockito = MockitoJUnit.rule();
59
60 @Mock
61 private UpdateEngine mUpdateEngine;
62 @Mock
63 private PayloadSpecs mPayloadSpecs;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070064 private UpdateManager mSubject;
65 private Context mContext;
66 private UpdateConfig mNonStreamingUpdate003;
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070067
68 @Before
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070069 public void setUp() throws Exception {
70 mContext = InstrumentationRegistry.getContext();
71 mSubject = new UpdateManager(mUpdateEngine, mPayloadSpecs);
72 mNonStreamingUpdate003 =
73 UpdateConfig.fromJson(readResource(R.raw.update_config_003_nonstream));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070074 }
75
76 @Test
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070077 public void applyUpdate_appliesPayloadToUpdateEngine() throws Exception {
78 PayloadSpec payload = buildMockPayloadSpec();
79 when(mPayloadSpecs.forNonStreaming(any(File.class))).thenReturn(payload);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070080 when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> {
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070081 // When UpdateManager is bound to update_engine, it passes
82 // UpdateEngineCallback as a callback to update_engine.
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070083 UpdateEngineCallback callback = answer.getArgument(0);
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070084 callback.onStatusUpdate(
85 UpdateEngine.UpdateStatusConstants.IDLE,
86 /*engineProgress*/ 0.0f);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070087 return null;
88 });
89
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070090 mSubject.bind();
91 mSubject.applyUpdate(null, mNonStreamingUpdate003);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -070092
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -070093 verify(mUpdateEngine).applyPayload(
94 "file://blah",
95 120,
96 340,
97 new String[] {
98 "SWITCH_SLOT_ON_REBOOT=0" // ab_config.force_switch_slot = false
99 });
100 }
101
102 @Test
103 public void stateIsRunningAndEngineStatusIsIdle_reApplyLastUpdate() throws Exception {
104 PayloadSpec payload = buildMockPayloadSpec();
105 when(mPayloadSpecs.forNonStreaming(any(File.class))).thenReturn(payload);
106 when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> {
107 // When UpdateManager is bound to update_engine, it passes
108 // UpdateEngineCallback as a callback to update_engine.
109 UpdateEngineCallback callback = answer.getArgument(0);
110 callback.onStatusUpdate(
111 UpdateEngine.UpdateStatusConstants.IDLE,
112 /*engineProgress*/ 0.0f);
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700113 return null;
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700114 });
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700115
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700116 mSubject.bind();
117 mSubject.applyUpdate(null, mNonStreamingUpdate003);
118 mSubject.unbind();
119 mSubject.bind(); // re-bind - now it should re-apply last update
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700120
Zhomart Mukhamejanov469b35a2018-06-01 12:41:20 -0700121 assertEquals(mSubject.getUpdaterState(), UpdaterState.RUNNING);
122 // it should be called 2 times
123 verify(mUpdateEngine, times(2)).applyPayload(
124 "file://blah",
125 120,
126 340,
127 new String[] {
128 "SWITCH_SLOT_ON_REBOOT=0" // ab_config.force_switch_slot = false
129 });
130 }
131
132 private PayloadSpec buildMockPayloadSpec() {
133 PayloadSpec payload = mock(PayloadSpec.class);
134 when(payload.getUrl()).thenReturn("file://blah");
135 when(payload.getOffset()).thenReturn(120L);
136 when(payload.getSize()).thenReturn(340L);
137 when(payload.getProperties()).thenReturn(ImmutableList.of());
138 return payload;
139 }
140
141 private String readResource(int id) throws IOException {
142 return CharStreams.toString(new InputStreamReader(
143 mContext.getResources().openRawResource(id)));
Zhomart Mukhamejanov6f26e712018-05-18 10:15:31 -0700144 }
145
146}