1 /* 2 * Copyright (C) 2008 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 17 package com.android.unit_tests; 18 19 import android.os.Build; 20 import android.server.data.BuildData; 21 import android.test.suitebuilder.annotation.SmallTest; 22 import android.util.Log; 23 import junit.framework.Assert; 24 import junit.framework.TestCase; 25 26 /** 27 * Provides test cases for android.os.Build and android.server.data.BuildData, 28 * and, in turn, many of the system properties set by the build system. 29 */ 30 public class BuildTest extends TestCase { 31 32 private static final String TAG = "BuildTest"; 33 34 /** 35 * Asserts that a String is non-null and non-empty. If it is not, 36 * an AssertionFailedError is thrown with the given message. 37 */ assertNotEmpty(String message, String string)38 private static void assertNotEmpty(String message, String string) { 39 //Log.i(TAG, "" + message + ": " + string); 40 assertNotNull(message, string); 41 assertFalse(message, string.equals("")); 42 } 43 44 /** 45 * Asserts that a String is non-null and non-empty. If it is not, 46 * an AssertionFailedError is thrown. 47 */ assertNotEmpty(String string)48 private static void assertNotEmpty(String string) { 49 assertNotEmpty(null, string); 50 } 51 52 /** 53 * Asserts that all android.os.Build fields are non-empty and/or in a valid range. 54 */ 55 @SmallTest testBuildFields()56 public void testBuildFields() throws Exception { 57 assertNotEmpty("ID", Build.ID); 58 assertNotEmpty("DISPLAY", Build.DISPLAY); 59 assertNotEmpty("PRODUCT", Build.PRODUCT); 60 assertNotEmpty("DEVICE", Build.DEVICE); 61 assertNotEmpty("BOARD", Build.BOARD); 62 assertNotEmpty("BRAND", Build.BRAND); 63 assertNotEmpty("MODEL", Build.MODEL); 64 assertNotEmpty("VERSION.INCREMENTAL", Build.VERSION.INCREMENTAL); 65 assertNotEmpty("VERSION.RELEASE", Build.VERSION.RELEASE); 66 assertNotEmpty("TYPE", Build.TYPE); 67 Assert.assertNotNull("TAGS", Build.TAGS); // TAGS is allowed to be empty. 68 assertNotEmpty("FINGERPRINT", Build.FINGERPRINT); 69 Assert.assertTrue("TIME", Build.TIME > 0); 70 assertNotEmpty("USER", Build.USER); 71 assertNotEmpty("HOST", Build.HOST); 72 73 // TODO: if any of the android.os.Build fields have additional constraints 74 // (e.g., must be a C identifier, must be a valid filename, must not contain any spaces) 75 // add tests for them. 76 } 77 78 /** 79 * Asserts that android.server.data.BuildData behaves as expected. 80 */ 81 @SmallTest testBuildData()82 public void testBuildData() throws Exception { 83 BuildData bd; 84 85 /* 86 * Default constructor 87 */ 88 bd = new BuildData(); 89 assertNotEmpty(bd.getFingerprint()); 90 assertNotEmpty(bd.getIncrementalVersion()); 91 Assert.assertTrue(bd.getTime() > 0); 92 93 /* 94 * Explicit constructor 95 */ 96 final String FINGERPRINT = "fingerprint"; 97 final String INCREMENTAL_VERSION = "74321"; // a valid long, for the serialization test 98 final long TIME = 12345; 99 bd = new BuildData(FINGERPRINT, INCREMENTAL_VERSION, TIME); 100 Assert.assertEquals(FINGERPRINT, bd.getFingerprint()); 101 Assert.assertEquals(INCREMENTAL_VERSION, bd.getIncrementalVersion()); 102 Assert.assertTrue(bd.getTime() == TIME); 103 104 // The serialization methods are package-private. 105 // 106 // import java.io.ByteArrayInputStream; 107 // import java.io.ByteArrayOutputStream; 108 // import java.io.DataInputStream; 109 // import java.io.DataOutputStream; 110 // 111 // /* 112 // * Serialization 113 // */ 114 // ByteArrayOutputStream out = new ByteArrayOutputStream(); 115 // bd.write(new DataOutputStream(out)); 116 // Assert.assertTrue(out.size() > 0); 117 // 118 // /* 119 // * Deserialization 120 // * 121 // * The current version of BuildData converts the incremental version to 122 // * and from a long when serializing/deserializing. Future versions should 123 // * treat it as a string. 124 // */ 125 // BuildData bd2 = 126 // new BuildData(new DataInputStream(new ByteArrayInputStream(out.toByteArray()))); 127 // Assert.assertEquals(bd.getFingerprint(), bd2.getFingerprint()); 128 // Assert.assertEquals(bd.getIncrementalVersion(), bd2.getIncrementalVersion()); 129 // Assert.assertTrue(bd.getTime() == bd2.getTime()); 130 } 131 } 132