Home | History | Annotate | Download | only in deviceinfo
      1 /*
      2  * Copyright (C) 2015 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.deviceinfo;
     17 
     18 import android.test.AndroidTestCase;
     19 import android.test.ActivityInstrumentationTestCase2;
     20 
     21 import java.io.BufferedReader;
     22 import java.io.FileInputStream;
     23 import java.io.IOException;
     24 import java.io.InputStreamReader;
     25 
     26 /**
     27  * Test for {@link DeviceInfo}.
     28  */
     29 public class DeviceInfoTest extends AndroidTestCase {
     30 
     31     private static final String EXPECTED_FILE_PATH =
     32             "/storage/emulated/0/device-info-files/TestDeviceInfo.deviceinfo.json";
     33 
     34     private TestDeviceInfo testDeviceInfo = new TestDeviceInfo();
     35 
     36     public void testJsonFile() throws Exception {
     37         testDeviceInfo.setUp();
     38         testDeviceInfo.testCollectDeviceInfo();
     39         String resultFilePath = testDeviceInfo.getResultFilePath();
     40         // Check file path exist
     41         assertNotNull("Expected a non-null resultFilePath", resultFilePath);
     42         // Check file path location
     43         assertEquals("Incorrect file path", EXPECTED_FILE_PATH, resultFilePath);
     44         // Check json file content
     45         String jsonContent = readFile(resultFilePath);
     46         assertEquals("Incorrect json output", ExampleObjects.testDeviceInfoJson(), jsonContent);
     47     }
     48 
     49     private String readFile(String filePath) throws IOException {
     50         InputStreamReader inputStreamReader = new InputStreamReader(
     51                 new FileInputStream(filePath), "UTF-8");
     52         StringBuilder stringBuilder = new StringBuilder();
     53         BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
     54         String line;
     55         while((line = bufferedReader.readLine()) != null) {
     56             stringBuilder.append(line);
     57             stringBuilder.append('\n');
     58         }
     59         bufferedReader.close();
     60         return stringBuilder.toString();
     61     }
     62 }