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 | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.doAnswer; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
| 23 | import static org.mockito.Mockito.times; |
| 24 | import static org.mockito.Mockito.verify; |
| 25 | import static org.mockito.Mockito.when; |
| 26 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 28 | import android.content.Intent; |
| 29 | import android.os.Bundle; |
| 30 | import android.os.ResultReceiver; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 31 | import android.os.UpdateEngine; |
| 32 | import android.os.UpdateEngineCallback; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 33 | import android.support.test.InstrumentationRegistry; |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 34 | import android.support.test.annotation.UiThreadTest; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 35 | import android.support.test.filters.SmallTest; |
| 36 | import android.support.test.runner.AndroidJUnit4; |
| 37 | |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 38 | import com.example.android.systemupdatersample.services.PrepareUpdateService; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 39 | import com.example.android.systemupdatersample.tests.R; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 40 | import com.google.common.collect.ImmutableList; |
| 41 | import com.google.common.io.CharStreams; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 42 | |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Rule; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.junit.MockitoJUnit; |
| 49 | import org.mockito.junit.MockitoRule; |
| 50 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 51 | import java.io.IOException; |
| 52 | import java.io.InputStreamReader; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Tests for {@link UpdateManager} |
| 56 | */ |
| 57 | @RunWith(AndroidJUnit4.class) |
| 58 | @SmallTest |
| 59 | public class UpdateManagerTest { |
| 60 | |
| 61 | @Rule |
| 62 | public MockitoRule mockito = MockitoJUnit.rule(); |
| 63 | |
| 64 | @Mock |
| 65 | private UpdateEngine mUpdateEngine; |
| 66 | @Mock |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 67 | private Context mMockContext; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 68 | private UpdateManager mSubject; |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 69 | private Context mTestContext; |
| 70 | private UpdateConfig mStreamingUpdate002; |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 71 | |
| 72 | @Before |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 73 | public void setUp() throws Exception { |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 74 | mTestContext = InstrumentationRegistry.getContext(); |
| 75 | mSubject = new UpdateManager(mUpdateEngine, null); |
| 76 | mStreamingUpdate002 = |
| 77 | UpdateConfig.fromJson(readResource(R.raw.update_config_002_stream)); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 81 | public void applyUpdate_appliesPayloadToUpdateEngine() throws Exception { |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 82 | mockContextStartServiceAnswer(buildMockPayloadSpec()); |
| 83 | mSubject.applyUpdate(mMockContext, mStreamingUpdate002); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 84 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 85 | verify(mUpdateEngine).applyPayload( |
| 86 | "file://blah", |
| 87 | 120, |
| 88 | 340, |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 89 | new String[]{ |
| 90 | "SWITCH_SLOT_ON_REBOOT=0", // ab_config.force_switch_slot = false |
| 91 | "USER_AGENT=" + UpdateManager.HTTP_USER_AGENT |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 92 | }); |
| 93 | } |
| 94 | |
| 95 | @Test |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 96 | @UiThreadTest |
| 97 | public void stateIsRunningAndEngineStatusIsIdle_reApplyLastUpdate() throws Throwable { |
| 98 | mockContextStartServiceAnswer(buildMockPayloadSpec()); |
| 99 | // UpdateEngine always returns IDLE status. |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 100 | 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 Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 107 | return null; |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 108 | }); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 109 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 110 | mSubject.bind(); |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 111 | mSubject.applyUpdate(mMockContext, mStreamingUpdate002); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 112 | mSubject.unbind(); |
| 113 | mSubject.bind(); // re-bind - now it should re-apply last update |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 114 | |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 115 | assertEquals(mSubject.getUpdaterState(), UpdaterState.RUNNING); |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 116 | verify(mUpdateEngine, times(2)).applyPayload( |
| 117 | "file://blah", |
| 118 | 120, |
| 119 | 340, |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 120 | new String[]{ |
| 121 | "SWITCH_SLOT_ON_REBOOT=0", // ab_config.force_switch_slot = false |
| 122 | "USER_AGENT=" + UpdateManager.HTTP_USER_AGENT |
Zhomart Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 123 | }); |
| 124 | } |
| 125 | |
Zhomart Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 126 | 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 Mukhamejanov | 469b35a | 2018-06-01 12:41:20 -0700 | [diff] [blame] | 141 | 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 Mukhamejanov | bc07775 | 2018-12-14 09:36:32 -0800 | [diff] [blame] | 152 | mTestContext.getResources().openRawResource(id))); |
Zhomart Mukhamejanov | 6f26e71 | 2018-05-18 10:15:31 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } |