• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.compatibility.common.util;
17 
18 import static org.junit.Assert.fail;
19 
20 import com.android.tradefed.log.LogUtil.CLog;
21 import com.android.tradefed.result.FileInputStreamSource;
22 import com.android.tradefed.result.LogDataType;
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
26 import com.android.tradefed.util.FileUtil;
27 import com.android.tradefed.util.StreamUtil;
28 
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.io.File;
34 import java.io.EOFException;
35 
36 /**
37  * Collect device information from host and write to a JSON file.
38  */
39 @RunWith(DeviceJUnit4ClassRunner.class)
40 public abstract class DeviceInfo extends BaseHostJUnit4Test {
41 
42     // Default name of the directory for storing device info files within the result directory
43     public static final String RESULT_DIR_NAME = "device-info-files";
44 
45     public static final String FILE_SUFFIX = ".deviceinfo.json";
46 
47     @Rule
48     public TestLogData mLogger = new TestLogData();
49 
50     @Test
testCollectDeviceInfo()51     public void testCollectDeviceInfo() throws Exception {
52         String deviceInfoName = getClass().getSimpleName() + FILE_SUFFIX;
53         File jsonFile = null;
54         FileInputStreamSource source = null;
55         try {
56             jsonFile = FileUtil.createTempFile(getClass().getSimpleName(), FILE_SUFFIX);
57             try {
58                 collectDeviceInfo(jsonFile);
59             } finally {
60                 // If file is empty throw exception so it is not copied to the results.
61                 if (jsonFile != null && jsonFile.exists() &&
62                         jsonFile.length() == 0) {
63                     throw new EOFException(String.format("File is empty: %s", deviceInfoName));
64                 }
65             }
66             source = new FileInputStreamSource(jsonFile);
67             mLogger.addTestLog(deviceInfoName, LogDataType.TEXT, source);
68         } catch (Exception e) {
69             CLog.e(e);
70             fail(String.format("Failed to collect device info (%s): %s",
71                     deviceInfoName, e.getMessage()));
72         } finally {
73             FileUtil.deleteFile(jsonFile);
74             StreamUtil.close(source);
75         }
76     }
77 
78     /**
79      * Method to collect device information.
80      */
collectDeviceInfo(HostInfoStore store)81     protected abstract void collectDeviceInfo(HostInfoStore store) throws Exception;
82 
83     /**
84      * Method to collect device information; this method should write JSON to the specified file
85      * directly.
86      */
collectDeviceInfo(File jsonFile)87     protected void collectDeviceInfo(File jsonFile) throws Exception {
88         try (HostInfoStore store = new HostInfoStore(jsonFile)) {
89             store.open();
90             collectDeviceInfo(store);
91         }
92     }
93 }
94