1/*
2 * Copyright (C) 2010 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 */
16package com.android.cts.tradefed.device;
17
18import com.android.ddmlib.Log;
19import com.android.tradefed.device.DeviceNotAvailableException;
20import com.android.tradefed.device.ITestDevice;
21import com.android.tradefed.result.ITestInvocationListener;
22import com.android.tradefed.testtype.InstrumentationTest;
23
24import java.io.File;
25
26/**
27 * Collects info from device under test.
28 * <p/>
29 * This class simply serves as a conduit for grabbing info from device using the device info
30 * collector apk, and forwarding that data directly to the {@link ITestInvocationListener} as run
31 * metrics.
32 */
33public class DeviceInfoCollector {
34
35    private static final String LOG_TAG = "DeviceInfoCollector";
36    private static final String APK_NAME = "TestDeviceSetup";
37    public static final String APP_PACKAGE_NAME = "android.tests.devicesetup";
38    private static final String INSTRUMENTATION_NAME = "android.tests.getinfo.DeviceInfoInstrument";
39
40    /**
41     * Installs and runs the device info collector instrumentation, and forwards results
42     * to the <var>listener</var>
43     *
44     * @param device
45     * @param listener
46     * @throws DeviceNotAvailableException
47     */
48    public static void collectDeviceInfo(ITestDevice device, File testApkDir,
49            ITestInvocationListener listener) throws DeviceNotAvailableException {
50        File apkFile = new File(testApkDir, String.format("%s.apk", APK_NAME));
51        if (!apkFile.exists()) {
52            Log.e(LOG_TAG, String.format("Could not find %s", apkFile.getAbsolutePath()));
53        }
54        // collect the instrumentation bundle results using instrumentation test
55        // should work even though no tests will actually be run
56        InstrumentationTest instrTest = new InstrumentationTest();
57        instrTest.setDevice(device);
58        instrTest.setInstallFile(apkFile);
59        // no need to collect tests and re-run
60        instrTest.setRerunMode(false);
61        instrTest.setPackageName(APP_PACKAGE_NAME);
62        instrTest.setRunnerName(INSTRUMENTATION_NAME);
63        instrTest.run(listener);
64    }
65}
66