1 /*
2  * Copyright (C) 2024 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.android.settings.development;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.anyList;
24 import static org.mockito.ArgumentMatchers.anyLong;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.spy;
27 
28 import android.content.Context;
29 import android.os.FileUtils;
30 import android.os.SystemProperties;
31 
32 import androidx.test.core.app.ApplicationProvider;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 
41 import java.io.File;
42 import java.io.FileNotFoundException;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.OutputStream;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class Enable16kPagesPreferenceControllerTest {
50 
51     @Mock private DevelopmentSettingsDashboardFragment mFragment;
52     private Context mContext;
53     private Enable16kPagesPreferenceController mController;
54 
55     @Before
setup()56     public void setup() throws Exception {
57         MockitoAnnotations.initMocks(this);
58         mContext = spy(ApplicationProvider.getApplicationContext());
59         mController = spy(new Enable16kPagesPreferenceController(mContext, mFragment));
60         doNothing().when(mController).applyPayload(any(), anyLong(), anyLong(), anyList());
61     }
62 
63     @Test
onSystemPropertyDisabled_shouldDisablePreference()64     public void onSystemPropertyDisabled_shouldDisablePreference() {
65         SystemProperties.set(Enable16kUtils.DEV_OPTION_PROPERTY, "false");
66         assertThat(mController.isAvailable()).isEqualTo(false);
67     }
68 
69     @Test
onSystemPropertyEnabled_shouldEnablePreference()70     public void onSystemPropertyEnabled_shouldEnablePreference() {
71         SystemProperties.set(Enable16kUtils.DEV_OPTION_PROPERTY, "true");
72         assertThat(mController.isAvailable()).isEqualTo(true);
73     }
74 
75     // TODO(b/303280163) : add tests to based on page size and whether preference is turned on/off
76 
77     @Test
validateUpdateParsing_validFile()78     public void validateUpdateParsing_validFile() throws IOException {
79         // TODO(b/295573133) : Add tests to verify applyPayload arguments
80         String filename = "valid_ota.zip";
81         File updateFile = copyFromAssetToDataDir(filename);
82         mController.applyUpdateFile(updateFile);
83     }
84 
85     @Test
validateUpdateParsing_emptyPayloadFile()86     public void validateUpdateParsing_emptyPayloadFile() {
87         String filename = "empty_payload_ota.zip";
88         File updateFile = copyFromAssetToDataDir(filename);
89         assertThrows(IOException.class, () -> mController.applyUpdateFile(updateFile));
90     }
91 
92     @Test
validateUpdateParsing_noPayloadFile()93     public void validateUpdateParsing_noPayloadFile() {
94         String filename = "no_payload_ota.zip";
95         File updateFile = copyFromAssetToDataDir(filename);
96         assertThrows(FileNotFoundException.class, () -> mController.applyUpdateFile(updateFile));
97     }
98 
99     @Test
validateUpdateParsing_noPropertiesFile()100     public void validateUpdateParsing_noPropertiesFile() {
101         String filename = "no_properties_ota.zip";
102         File updateFile = copyFromAssetToDataDir(filename);
103         assertThrows(FileNotFoundException.class, () -> mController.applyUpdateFile(updateFile));
104     }
105 
copyFromAssetToDataDir(String filename)106     private File copyFromAssetToDataDir(String filename) {
107         try {
108             InputStream in = mContext.getAssets().open(filename);
109             File destination =
110                     File.createTempFile(
111                             "test_update", ".zip", new File(mContext.getDataDir().getPath()));
112             FileUtils.setPermissions(
113                     /* path= */ destination,
114                     /* mode= */ FileUtils.S_IRWXU | FileUtils.S_IRGRP | FileUtils.S_IROTH,
115                     /* uid= */ -1,
116                     /* gid= */ -1);
117             OutputStream out = new FileOutputStream(destination);
118             FileUtils.copy(in, out);
119             return destination;
120         } catch (IOException e) {
121             throw new RuntimeException(e);
122         }
123     }
124 }
125