summaryrefslogtreecommitdiff
path: root/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/api27/ShellUtilTest.java
blob: b3aa42fd7c46b1e3c125c2b5fab4c5a63eea71f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (c) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.devtools.systemimage.uitest.smoke.api27;

import android.app.Instrumentation;
import.androidx.test.runner.AndroidJUnit4;
import.androidx.test.uiautomator.UiDevice;
import.androidx.test.uiautomator.UiObject;
import.androidx.test.uiautomator.UiSelector;
import android.util.Log;

import com.android.devtools.systemimage.uitest.annotations.TestInfo;
import com.android.devtools.systemimage.uitest.framework.SystemImageTestFramework;
import com.android.devtools.systemimage.uitest.utils.AppLauncher;
import com.android.devtools.systemimage.uitest.utils.DeveloperOptionsManager;
import com.android.devtools.systemimage.uitest.utils.ShellUtil;
import com.android.devtools.systemimage.uitest.utils.Wait;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

/**
 * Test on shell utility.
 */
@RunWith(AndroidJUnit4.class)
public class ShellUtilTest {
    private final String TAG = "ShellUtilTest";

    @Rule
    public final SystemImageTestFramework testFramework = new SystemImageTestFramework();

    @Rule
    public Timeout globalTimeout = Timeout.seconds(240);

    /**
     * Tests the integrity of Shell utilities.
     * <p>
     * This is run to qualify releases. Please involve the test team in substantial changes.
     * <p>
     * TR ID: C14578821
     * <p>
     *   <pre>
     *   Test Steps:
     *   1. Start the emulator.
     *   2. From the cmd line, run "adb shell ls /system/bin"
     *   Verify:
     *   Shell utilities are listed in SDK emulator image.
     *   </pre>
     */
    @Test
    @TestInfo(id = "14578821")
    public void testShellUtilIntegrity() throws Exception {
        Instrumentation instrumentation = testFramework.getInstrumentation();

        String cmd = "ls /system/bin";
        ShellUtil.ShellResult result = ShellUtil.invokeCommand(cmd);
        // Check if the cmd is executed correctly.
        Assert.assertEquals(result.stderr, 0, result.stderr.length());

        // Verify the integrity of the shell utilities.
        InputStream inputStream = instrumentation.getTargetContext().getAssets().open("util.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String line;
        StringBuilder util = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            util.append(line).append("\n");
        }
        Assert.assertThat("Failure: The shell util is incomplete.", result.stderr,
                Matchers.isEmptyOrNullString());
    }

    /**
     * Tests take bug report in Developer Options.
     * <p>
     * This is run to qualify releases. Please involve the test team in substantial changes.
     * <p>
     * TR ID: C14581588
     * <p>
     *   <pre>
     *   Test Steps:
     *   1. Start the emulator.
     *   2. Open Settings > Developer Options.
     *   3. Tap on "Take Bug Report"
     *   4. Click on REPORT button.
     *   Verify:
     *   Verify that a bug report is taken by checking for the png and zip file in the bugreport
     *     directory.
     *   </pre>
     */
    @Test
    @TestInfo(id = "14581588")
    public void createBugReport() throws Exception {
        Instrumentation instrumentation = testFramework.getInstrumentation();
        final UiDevice device = UiDevice.getInstance(instrumentation);
        final String BUG_REPORT_DIR = "/bugreports";

        ShellUtil.deleteBugReportFiles(BUG_REPORT_DIR, testFramework);

        if (!DeveloperOptionsManager.isDeveloperOptionsEnabled_v2(testFramework)) {
            DeveloperOptionsManager.enableDeveloperOptions_v2(testFramework);
        }

        AppLauncher.launchPath(instrumentation, true, "Settings", "System", "Developer options");
        // Remove bug report files even if the test fails.
        try {
            device.findObject(
                    new UiSelector().text("Take bug report")).clickAndWaitForNewWindow();
            UiObject fullReportButton = device.findObject(new UiSelector().textMatches("(?i)full report(?-i)"));
            if (fullReportButton.exists()) {
                fullReportButton.clickAndWaitForNewWindow();
            }
            UiObject reportButton = device.findObject(new UiSelector().textMatches("(?i)report(?-i)"));
            if (reportButton.exists()) {
                reportButton.click();
            }
            boolean gotPngAndZip = new Wait(
                    TimeUnit.MILLISECONDS.convert(30L, TimeUnit.SECONDS)).until(
                    new Wait.ExpectedCondition() {
                        @Override
                        public boolean isTrue() throws Exception {
                            String result = device.executeShellCommand("ls " + BUG_REPORT_DIR);
                            Log.d(TAG, "ls result " + result);

                            return result.matches("(?s).*bugreport.*\\.png.*")
                                    && result.matches("(?s).*bugreport.*\\.zip.*");
                        }
                    });
            Assert.assertTrue("Missing bug report files for png and zip.", gotPngAndZip);
        } finally {
            ShellUtil.deleteBugReportFiles(BUG_REPORT_DIR, testFramework);
        }
    }
}