Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 android.os.cts; |
| 18 | |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 19 | import android.os.Build; |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 20 | import android.util.Log; |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 21 | |
Brian Muramatsu | c4d9a56 | 2010-12-20 13:34:37 -0800 | [diff] [blame] | 22 | import java.util.Arrays; |
| 23 | import java.util.HashSet; |
| 24 | import java.util.Set; |
| 25 | |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 26 | import junit.framework.TestCase; |
| 27 | |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 28 | public class BuildVersionTest extends TestCase { |
| 29 | |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 30 | private static final String LOG_TAG = "BuildVersionTest"; |
Nicholas Sauer | 16fc2a1 | 2014-12-11 15:45:43 -0800 | [diff] [blame] | 31 | private static final Set<String> EXPECTED_RELEASES = |
Unsuk Jung | 34c9182 | 2015-04-14 17:49:50 -0700 | [diff] [blame] | 32 | new HashSet<String>(Arrays.asList("5.1", "5.1.1")); |
Unsuk Jung | 7270fdb | 2015-03-13 05:44:59 -0700 | [diff] [blame] | 33 | private static final int EXPECTED_SDK = 22; |
Nicholas Sauer | 0229ed1 | 2014-10-06 16:52:49 -0700 | [diff] [blame] | 34 | private static final String EXPECTED_BUILD_VARIANT = "user"; |
| 35 | private static final String EXPECTED_TAG = "release-keys"; |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 36 | |
Brian Muramatsu | ca2836a | 2011-11-08 18:46:46 -0800 | [diff] [blame] | 37 | @SuppressWarnings("deprecation") |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 38 | public void testReleaseVersion() { |
| 39 | // Applications may rely on the exact release version |
Nicholas Sauer | 7d664da | 2014-07-30 09:26:48 -0700 | [diff] [blame] | 40 | assertAnyOf("BUILD.VERSION.RELEASE", Build.VERSION.RELEASE, EXPECTED_RELEASES); |
Nicholas Sauer | 6bd0079 | 2014-10-07 17:41:04 -0700 | [diff] [blame] | 41 | assertEquals("Build.VERSION.SDK", "" + EXPECTED_SDK, Build.VERSION.SDK); |
| 42 | assertEquals("Build.VERSION.SDK_INT", EXPECTED_SDK, Build.VERSION.SDK_INT); |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 43 | } |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 44 | |
Brian Muramatsu | 6259d61 | 2011-03-07 17:47:22 -0800 | [diff] [blame] | 45 | public void testIncremental() { |
| 46 | assertNotEmpty(Build.VERSION.INCREMENTAL); |
| 47 | } |
| 48 | |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 49 | /** |
Brian Muramatsu | ca2836a | 2011-11-08 18:46:46 -0800 | [diff] [blame] | 50 | * Verifies {@link Build#FINGERPRINT} follows expected format: |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 51 | * <p/> |
| 52 | * <code> |
Brian Muramatsu | 0a571eb | 2010-10-15 15:08:00 -0700 | [diff] [blame] | 53 | * (BRAND)/(PRODUCT)/(DEVICE):(VERSION.RELEASE)/(BUILD_ID)/ |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 54 | * (BUILD_NUMBER):(BUILD_VARIANT)/(TAGS) |
| 55 | * </code> |
| 56 | */ |
| 57 | public void testBuildFingerprint() { |
| 58 | final String fingerprint = Build.FINGERPRINT; |
| 59 | Log.i(LOG_TAG, String.format("Testing fingerprint %s", fingerprint)); |
| 60 | |
| 61 | assertEquals("Build fingerprint must not include whitespace", -1, |
| 62 | fingerprint.indexOf(' ')); |
| 63 | final String[] fingerprintSegs = fingerprint.split("/"); |
Brian Muramatsu | 0a571eb | 2010-10-15 15:08:00 -0700 | [diff] [blame] | 64 | assertEquals("Build fingerprint does not match expected format", 6, fingerprintSegs.length); |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 65 | assertEquals(Build.BRAND, fingerprintSegs[0]); |
| 66 | assertEquals(Build.PRODUCT, fingerprintSegs[1]); |
Brian Muramatsu | 0a571eb | 2010-10-15 15:08:00 -0700 | [diff] [blame] | 67 | |
| 68 | String[] devicePlatform = fingerprintSegs[2].split(":"); |
| 69 | assertEquals(2, devicePlatform.length); |
| 70 | assertEquals(Build.DEVICE, devicePlatform[0]); |
| 71 | assertEquals(Build.VERSION.RELEASE, devicePlatform[1]); |
| 72 | |
| 73 | assertEquals(Build.ID, fingerprintSegs[3]); |
Nicholas Sauer | 7d664da | 2014-07-30 09:26:48 -0700 | [diff] [blame] | 74 | |
Brian Muramatsu | 0a571eb | 2010-10-15 15:08:00 -0700 | [diff] [blame] | 75 | assertTrue(fingerprintSegs[4].contains(":")); |
Nicholas Sauer | 7d664da | 2014-07-30 09:26:48 -0700 | [diff] [blame] | 76 | String[] buildNumberVariant = fingerprintSegs[4].split(":"); |
| 77 | String buildVariant = buildNumberVariant[1]; |
Nicholas Sauer | 0229ed1 | 2014-10-06 16:52:49 -0700 | [diff] [blame] | 78 | assertEquals("Variant", EXPECTED_BUILD_VARIANT, buildVariant); |
| 79 | assertEquals("Tag", EXPECTED_TAG, fingerprintSegs[5]); |
Brett Chabot | 365914d | 2009-09-09 13:55:13 -0700 | [diff] [blame] | 80 | } |
Brian Muramatsu | 6259d61 | 2011-03-07 17:47:22 -0800 | [diff] [blame] | 81 | |
| 82 | private void assertNotEmpty(String value) { |
| 83 | assertNotNull(value); |
| 84 | assertFalse(value.isEmpty()); |
| 85 | } |
Nicholas Sauer | 7d664da | 2014-07-30 09:26:48 -0700 | [diff] [blame] | 86 | |
| 87 | /** Assert that {@code actualValue} is equals to one of {@code permittedValues}. */ |
| 88 | private void assertAnyOf(String label, String actualValue, Set<String> permittedValues) { |
| 89 | if (!permittedValues.contains(actualValue)) { |
| 90 | fail("For: " + label + ", the value: " + actualValue + |
| 91 | ", should be one of: " + permittedValues); |
| 92 | } |
| 93 | } |
Phil Dubach | 5d3e1b9 | 2009-04-23 10:12:28 -0700 | [diff] [blame] | 94 | } |