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     @Test
52     @AppModeFull
enable16KbToggle()53     public void enable16KbToggle() throws Exception {
54         assertTrue(isPackageInstalled(APP_PACKAGE));
55 
56         // Check if developer option is enabled otherwise exit
57         getDevice().enableAdbRoot();
58         String result = getDevice().getProperty("ro.product.build.16k_page.enabled");
59         assumeTrue("true".equals(result));
60 
61         // This test can be run on OEM unlocked device only as unlocking bootloader requires
62         // manual intervention.
63         result = getDevice().getProperty("ro.boot.flash.locked");
64         assumeTrue("0".equals(result));
65 
66         getDevice().executeShellCommand("am start -a com.android.setupwizard.FOUR_CORNER_EXIT");
67 
68         // Enables developer option and switch to ext4
69         runTestAndWait(SWITCH_TO_EXT4);
70 
71         getDevice().enableAdbRoot();
72         getDevice().executeShellCommand("am start -a com.android.setupwizard.FOUR_CORNER_EXIT");
73         assertTrue(verifyExt4());
74 
75         // Device will wiped. need to install test package again.
76         installTestApp();
77 
78         // Enable developer option and switch to 16kb kernel and Check page size
79         runTestAndWait(SWITCH_TO_16KB);
80         result = getDevice().executeShellCommand("getconf PAGE_SIZE");
81         assertEquals("16384", result.strip());
82 
83         // switch back to 4kb kernel and check page size
84         runTestAndWait(SWITCH_TO_4KB);
85         result = getDevice().executeShellCommand("getconf PAGE_SIZE");
86         assertEquals("4096", result.strip());
87 
88         // Verify that developer options can't be turned off
89         runDeviceTests(APP_PACKAGE, APP_PACKAGE + "." + TEST_NAME, DISABLE_DEV_OPTION);
90     }
91 
installTestApp()92     private void installTestApp() throws Exception {
93         DeviceTestRunOptions options = new DeviceTestRunOptions(null /* unused */);
94         options.setApkFileName(TEST_APP_NAME);
95         options.setInstallArgs("-r");
96         installPackage(options);
97         assertTrue(isPackageInstalled(APP_PACKAGE));
98     }
99 
runTestAndWait(String testMethodName)100     private void runTestAndWait(String testMethodName) throws Exception {
101         runDeviceTests(APP_PACKAGE, APP_PACKAGE + "." + TEST_NAME, testMethodName);
102         // Device is either formatting or applying update. It usually takes 3 minutes to boot.
103         RunUtil.getDefault().sleep(180000);
104         // Wait for 2 mins device to be online againg
105         getDevice().waitForDeviceOnline(120000);
106     }
107 
verifyExt4()108     private boolean verifyExt4() throws Exception {
109         String result = getDevice().executeShellCommand("cat /proc/mounts");
110         BufferedReader br = new BufferedReader(new StringReader(result));
111         String line;
112         while ((line = br.readLine()) != null) {
113             final String[] fields = line.split(" ");
114             final String partition = fields[1];
115             final String fsType = fields[2];
116             if (partition.equals("/data") && fsType.equals("ext4")) {
117                 return true;
118             }
119         }
120         return false;
121     }
122 }
123