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.test; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assume.assumeTrue; 22 23 import android.platform.test.annotations.AppModeFull; 24 25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 26 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 27 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions; 28 import com.android.tradefed.util.RunUtil; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.io.BufferedReader; 34 import java.io.StringReader; 35 36 @RunWith(DeviceJUnit4ClassRunner.class) 37 public class Enable16KbTest extends BaseHostJUnit4Test { 38 private static final String TEST_APP_NAME = "test_16kb_app.apk"; 39 40 private static final String APP_PACKAGE = "com.android.settings.development.test"; 41 42 private static final String TEST_NAME = "Enable16KbDeviceTest"; 43 44 private static final String SWITCH_TO_EXT4 = "enable16k_switchToExt4"; 45 46 private static final String SWITCH_TO_16KB = "enable16k_switchTo16Kb"; 47 48 private static final String SWITCH_TO_4KB = "enable16k_switchTo4Kb"; 49 private static final String DISABLE_DEV_OPTION = "enable16k_disableDeveloperOption"; 50 51 private static final int DEVICE_WAIT_TIMEOUT = 120000; 52 private static final int DEVICE_UPDATE_TIMEOUT = 180000; 53 54 @Test 55 @AppModeFull enable16KbToggle()56 public void enable16KbToggle() throws Exception { 57 // Wait for 2 minutes for device to be online 58 prepareDevice(); 59 if (!isPackageInstalled(APP_PACKAGE)) { 60 //If test app has failed for some reason, retry installation 61 installTestApp(); 62 } 63 64 // Check if developer option is enabled otherwise exit 65 prepareDevice(); 66 String result = getDevice().getProperty("ro.product.build.16k_page.enabled"); 67 assumeTrue("true".equals(result)); 68 69 // This test can be run on OEM unlocked device only as unlocking bootloader requires 70 // manual intervention. 71 result = getDevice().getProperty("ro.boot.flash.locked"); 72 assumeTrue("0".equals(result)); 73 74 getDevice().executeShellCommand("am start -a com.android.setupwizard.FOUR_CORNER_EXIT"); 75 76 // Enables developer option and switch to ext4 77 runTestAndWait(SWITCH_TO_EXT4); 78 getDevice().executeShellCommand("am start -a com.android.setupwizard.FOUR_CORNER_EXIT"); 79 assertTrue(verifyExt4()); 80 81 // Device will wiped. need to install test package again. 82 installTestApp(); 83 84 // Enable developer option and switch to 16kb kernel and Check page size 85 runTestAndWait(SWITCH_TO_16KB); 86 result = getDevice().executeShellCommand("getconf PAGE_SIZE"); 87 assertEquals("16384", result.strip()); 88 89 // switch back to 4kb kernel and check page size 90 runTestAndWait(SWITCH_TO_4KB); 91 result = getDevice().executeShellCommand("getconf PAGE_SIZE"); 92 assertEquals("4096", result.strip()); 93 94 // Verify that developer options can't be turned off 95 runDeviceTests(APP_PACKAGE, APP_PACKAGE + "." + TEST_NAME, DISABLE_DEV_OPTION); 96 } 97 installTestApp()98 private void installTestApp() throws Exception { 99 DeviceTestRunOptions options = new DeviceTestRunOptions(null /* unused */); 100 options.setApkFileName(TEST_APP_NAME); 101 options.setInstallArgs("-r"); 102 installPackage(options); 103 assertTrue(isPackageInstalled(APP_PACKAGE)); 104 } 105 runTestAndWait(String testMethodName)106 private void runTestAndWait(String testMethodName) throws Exception { 107 prepareDevice(); 108 runDeviceTests(APP_PACKAGE, APP_PACKAGE + "." + TEST_NAME, testMethodName); 109 // Device is either formatting or applying update. It usually takes 3 minutes to boot. 110 RunUtil.getDefault().sleep(DEVICE_UPDATE_TIMEOUT); 111 112 // make sure it is available again after the test 113 prepareDevice(); 114 } 115 prepareDevice()116 private void prepareDevice() throws Exception { 117 // Verify that device is online before running test and enable root 118 getDevice().waitForDeviceAvailable(DEVICE_WAIT_TIMEOUT); 119 getDevice().enableAdbRoot(); 120 getDevice().waitForDeviceAvailable(DEVICE_WAIT_TIMEOUT); 121 122 getDevice().executeShellCommand("input keyevent KEYCODE_WAKEUP"); 123 getDevice().executeShellCommand("wm dismiss-keyguard"); 124 } 125 verifyExt4()126 private boolean verifyExt4() throws Exception { 127 String result = getDevice().executeShellCommand("cat /proc/mounts"); 128 BufferedReader br = new BufferedReader(new StringReader(result)); 129 String line; 130 while ((line = br.readLine()) != null) { 131 final String[] fields = line.split(" "); 132 final String partition = fields[1]; 133 final String fsType = fields[2]; 134 if (partition.equals("/data") && fsType.equals("ext4")) { 135 return true; 136 } 137 } 138 return false; 139 } 140 } 141