aboutsummaryrefslogtreecommitdiff
path: root/tools/tradefed-host/src/com/android/afwtest/tradefed/testtype/AfwTest.java
blob: b9f6d8c7f7b92eb66e0423cbff1ee5ff9d8ec96a (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
/*
 * 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.testtype;

import com.android.afwtest.tradefed.TestConfig;
import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
import com.android.compatibility.common.tradefed.testtype.IModuleRepo;
import com.android.compatibility.common.tradefed.testtype.CompatibilityTest;
import com.android.compatibility.common.tradefed.testtype.ModuleRepo;
import com.android.tradefed.build.IBuildInfo;
import com.android.tradefed.config.Option;
import com.android.tradefed.config.OptionClass;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.result.ITestInvocationListener;
import com.android.tradefed.util.FileUtil;

import java.io.File;
import java.io.IOException;

/**
 * Customized {@link CompatibilityTest} for running afw-test tests.
 * <p>Supports running all the tests contained in an afw-test plan, or individual test packages.
 * </p>
 */
@OptionClass(alias = "compatibility")
public class AfwTest extends CompatibilityTest {

    private static final String CMD_DISABLE_PKG_VERIFICATION =
            "settings put global package_verifier_enable 0";

    @Option(name = "afw-test-config-file",
            description = "Test harness config file to replace afw-test.props.")
    private String mConfigFile = null;

    @Option(name = "screenshot",
            description = "Take a screenshot on every test")
    private boolean mScreenshot = false;

    /**
     * A {@link CompatibilityBuildHelper} instance to access afw-test build info, such as the
     * absolute paths of the test plan file and test case apks.
     */
    private CompatibilityBuildHelper mAfwTestBuild = null;

    /**
     * {@inheritDoc}
     */
    public AfwTest() {
        this(1 /*totalShards*/, new ModuleRepo() /*moduleRepo*/);
    }

    /**
     * {@inheritDoc}
     */
    public AfwTest(int totalShards, IModuleRepo moduleRepo) {
        super(totalShards, moduleRepo);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setBuild(IBuildInfo build) {
        super.setBuild(build);

        mAfwTestBuild = new CompatibilityBuildHelper(build);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
        CLog.i("Running afw-test");

        ITestDevice device = getDevice();
        if (device == null) {
            throw new IllegalArgumentException("Device not found");
        }

        try {
            // Replace afw-test.props with a customized file if there is.
            if (mConfigFile != null) {
                FileUtil.copyFile(new File(mConfigFile), getTestConfigFile());
            }

            // Init TestConfig singleton.
            TestConfig.init(getTestConfigFile());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // Disable package verification.
        // This is necessary because CtsTest.run() will push TestDeviceSetup.apk
        // to the testing device which will trigger package verification dialog.
        device.executeShellCommand(CMD_DISABLE_PKG_VERIFICATION);

        ITestInvocationListener internalListener = listener;
        if (mScreenshot) {
            internalListener = new ScreenshotListener(internalListener, device);
        }

        super.run(internalListener);
    }

    /**
     * Reads test configuration file, afw-test.props.
     *
     * @return {@link File} object keeping loaded content from afw-test.props
     */
    protected File getTestConfigFile() throws IOException {
        return new File(mAfwTestBuild.getTestsDir(), "afw-test.props");
    }
}