Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.example.android.systemupdatersample; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.mockito.ArgumentMatchers.any; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
| 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 27 | import android.os.UpdateEngine; |
| 28 | import android.os.UpdateEngineCallback; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 29 | import android.support.test.InstrumentationRegistry; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 30 | import android.support.test.filters.SmallTest; |
| 31 | import android.support.test.runner.AndroidJUnit4; |
| 32 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 33 | import com.example.android.systemupdatersample.tests.R; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 34 | import com.example.android.systemupdatersample.util.PayloadSpecs; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 35 | import com.google.common.collect.ImmutableList; |
| 36 | import com.google.common.io.CharStreams; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 37 | |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Rule; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.junit.MockitoJUnit; |
| 44 | import org.mockito.junit.MockitoRule; |
| 45 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 46 | import java.io.File; |
| 47 | import java.io.IOException; |
| 48 | import java.io.InputStreamReader; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Tests for {@link UpdateManager} |
| 52 | */ |
| 53 | @RunWith(AndroidJUnit4.class) |
| 54 | @SmallTest |
| 55 | public 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 Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 64 | private UpdateManager mSubject; |
| 65 | private Context mContext; |
| 66 | private UpdateConfig mNonStreamingUpdate003; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 67 | |
| 68 | @Before |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 69 | 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 Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Test |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 77 | public void applyUpdate_appliesPayloadToUpdateEngine() throws Exception { |
| 78 | PayloadSpec payload = buildMockPayloadSpec(); |
| 79 | when(mPayloadSpecs.forNonStreaming(any(File.class))).thenReturn(payload); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 80 | when(mUpdateEngine.bind(any(UpdateEngineCallback.class))).thenAnswer(answer -> { |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 81 | // When UpdateManager is bound to update_engine, it passes |
| 82 | // UpdateEngineCallback as a callback to update_engine. |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 83 | UpdateEngineCallback callback = answer.getArgument(0); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 84 | callback.onStatusUpdate( |
| 85 | UpdateEngine.UpdateStatusConstants.IDLE, |
| 86 | /*engineProgress*/ 0.0f); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 87 | return null; |
| 88 | }); |
| 89 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 90 | mSubject.bind(); |
| 91 | mSubject.applyUpdate(null, mNonStreamingUpdate003); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 92 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 93 | 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 Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 113 | return null; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 114 | }); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 115 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 116 | mSubject.bind(); |
| 117 | mSubject.applyUpdate(null, mNonStreamingUpdate003); |
| 118 | mSubject.unbind(); |
| 119 | mSubject.bind(); // re-bind - now it should re-apply last update |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 120 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 121 | 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 Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } |