aboutsummaryrefslogtreecommitdiff
path: root/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestTargetPreparer.java
blob: cfca249e447f284941e8a8a3c6dd37c5558be5b0 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * 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.afwtest.tradefed.targetprep;

import com.android.afwtest.tradefed.TestConfig;
import com.android.cts.tradefed.build.CtsBuildHelper;
import com.android.tradefed.build.IBuildInfo;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.device.PackageInfo;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.targetprep.TargetSetupError;
import com.android.tradefed.util.CommandResult;
import com.android.tradefed.util.CommandStatus;
import com.android.tradefed.util.RunUtil;

import java.io.File;
import java.util.concurrent.TimeUnit;


/**
 * Abstract class to keep common functionality.
 */
public abstract class AfwTestTargetPreparer {

    private CtsBuildHelper mBuildHelper;
    private Long mTimeoutSecs = TimeUnit.MINUTES.toSeconds(5);

    /**
     * Gets an instance of {@link CtsBuildHelper}.
     *
     * @param buildInfo reference to {@link IBuildInfo}
     * @return reference to {@link CtsBuildHelper} instance
     */
    protected CtsBuildHelper getCtsBuildHelper(IBuildInfo buildInfo) {
        if (mBuildHelper == null) {
            mBuildHelper = CtsBuildHelper.createBuildHelper(buildInfo);
        }
        return mBuildHelper;
    }

    /**
     * Gets repository directory.
     *
     * @param buildInfo reference to {@link IBuildInfo}
     * @return File path to repository folder.
     */
    protected File getRepositoryDir(IBuildInfo buildInfo) {
        return new File(getCtsBuildHelper(buildInfo).getCtsDir(), "repository");
    }

    /**
     * Gets apk file from testcases dir.
     *
     * @param buildInfo reference to {@link IBuildInfo}
     * @param fileName apk file name
     * @return {@link File} of the apk
     */
    protected File getApk(IBuildInfo buildInfo, String fileName) {
        return new File(getCtsBuildHelper(buildInfo).getTestCasesDir(), fileName);
    }

    /**
     * Enable adb root.
     *
     * @param device test device
     * @throws TargetSetupError if failed to enable adb root
     * @throws DeviceNotAvailableException if test device becomes unavailable
     */
    protected void enableAdbRoot(ITestDevice device)
            throws TargetSetupError, DeviceNotAvailableException {
        // Enable adb root
        if (!device.enableAdbRoot()) {
            throw new TargetSetupError(
                    String.format("Failed to enable adb root: %s", device.getSerialNumber()));
        }
    }

    /**
     * Waits and gets certain app package info.
     *
     * @param device test device
     * @param pkgName app package name
     * @param timeoutMs timeout in ms
     * @return App package info of given package if found, null otherwise
     * @throws DeviceNotAvailableException if test device becomes unavailable
     */
    protected PackageInfo waitForAppPkgInfo(ITestDevice device, String pkgName, long timeoutMs)
            throws DeviceNotAvailableException {

        long threadSleepTimeMs = TimeUnit.SECONDS.toMillis(5);

        PackageInfo pkgInfo = device.getAppPackageInfo(pkgName);

        while (pkgInfo == null && timeoutMs > 0) {
            RunUtil.getDefault().sleep(threadSleepTimeMs);
            timeoutMs -= threadSleepTimeMs;

            pkgInfo = device.getAppPackageInfo(pkgName);
        }

        return pkgInfo;
    }

    /**
     * Gets {@link TestConfig} instance.
     *
     * @return {@link TestConfig} instance or null if not found;
     */
    protected TestConfig getTestConfig() {
        return TestConfig.getInstance();
    }

    /**
     * Gets timeout size from config file.
     *
     * @return timeout size in integer
     */
    protected int getTimeoutSize() {
        TestConfig testConfig = TestConfig.getInstance();

        // Target prepares should still work without test config file;
        if (testConfig == null) {
            return 1;
        }

        return TestConfig.getInstance().getTimeoutSize();
    }

    /**
     * Wipes device via fastboot.
     *
     * <p>
     * Refers to com.android.tradefed.targetprep.DeviceWiper
     * </p>
     *
     * @param device test device
     */
    protected void wipeDevice(ITestDevice device)
            throws DeviceNotAvailableException, TargetSetupError {

        CLog.i(String.format("Wiping device %s".format(device.getSerialNumber())));

        device.rebootIntoBootloader();
        fastbootFormat(device, "cache");
        fastbootFormat(device, "userdata");
        device.executeFastbootCommand("reboot");
    }


    /**
     * Performs fastboot erase/format operation on certain partition
     *
     * @param device test device
     * @param partition android partition, e.g. userdata, cache, system
     */
    protected void fastbootFormat(ITestDevice device, String partition)
            throws DeviceNotAvailableException, TargetSetupError {

        CLog.i(String.format("Attempting: fastboot format %s", partition));
        CommandResult r = device.executeLongFastbootCommand("format", partition);
        if (r.getStatus() != CommandStatus.SUCCESS) {
            throw new TargetSetupError(
                    String.format("format %s failed: %s", partition, r.getStderr()));
        }
    }
}