blob: 419f3200fc17f245ae6089f5a49e618bb8aed1cd [file] [log] [blame]
Phil Dubach5d3e1b92009-04-23 10:12:28 -07001/*
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
17package android.os.cts;
18
Phil Dubach5d3e1b92009-04-23 10:12:28 -070019import android.os.Build;
Brett Chabot365914d2009-09-09 13:55:13 -070020import android.util.Log;
Phil Dubach5d3e1b92009-04-23 10:12:28 -070021
Brian Muramatsuc4d9a562010-12-20 13:34:37 -080022import java.util.Arrays;
23import java.util.HashSet;
24import java.util.Set;
25
Phil Dubach5d3e1b92009-04-23 10:12:28 -070026import junit.framework.TestCase;
27
Phil Dubach5d3e1b92009-04-23 10:12:28 -070028public class BuildVersionTest extends TestCase {
29
Brett Chabot365914d2009-09-09 13:55:13 -070030 private static final String LOG_TAG = "BuildVersionTest";
Nicholas Sauer16fc2a12014-12-11 15:45:43 -080031 private static final Set<String> EXPECTED_RELEASES =
Unsuk Jung34c91822015-04-14 17:49:50 -070032 new HashSet<String>(Arrays.asList("5.1", "5.1.1"));
Unsuk Jung7270fdb2015-03-13 05:44:59 -070033 private static final int EXPECTED_SDK = 22;
Nicholas Sauer0229ed12014-10-06 16:52:49 -070034 private static final String EXPECTED_BUILD_VARIANT = "user";
35 private static final String EXPECTED_TAG = "release-keys";
Phil Dubach5d3e1b92009-04-23 10:12:28 -070036
Brian Muramatsuca2836a2011-11-08 18:46:46 -080037 @SuppressWarnings("deprecation")
Phil Dubach5d3e1b92009-04-23 10:12:28 -070038 public void testReleaseVersion() {
39 // Applications may rely on the exact release version
Nicholas Sauer7d664da2014-07-30 09:26:48 -070040 assertAnyOf("BUILD.VERSION.RELEASE", Build.VERSION.RELEASE, EXPECTED_RELEASES);
Nicholas Sauer6bd00792014-10-07 17:41:04 -070041 assertEquals("Build.VERSION.SDK", "" + EXPECTED_SDK, Build.VERSION.SDK);
42 assertEquals("Build.VERSION.SDK_INT", EXPECTED_SDK, Build.VERSION.SDK_INT);
Phil Dubach5d3e1b92009-04-23 10:12:28 -070043 }
Brett Chabot365914d2009-09-09 13:55:13 -070044
Brian Muramatsu6259d612011-03-07 17:47:22 -080045 public void testIncremental() {
46 assertNotEmpty(Build.VERSION.INCREMENTAL);
47 }
48
Brett Chabot365914d2009-09-09 13:55:13 -070049 /**
Brian Muramatsuca2836a2011-11-08 18:46:46 -080050 * Verifies {@link Build#FINGERPRINT} follows expected format:
Brett Chabot365914d2009-09-09 13:55:13 -070051 * <p/>
52 * <code>
Brian Muramatsu0a571eb2010-10-15 15:08:00 -070053 * (BRAND)/(PRODUCT)/(DEVICE):(VERSION.RELEASE)/(BUILD_ID)/
Brett Chabot365914d2009-09-09 13:55:13 -070054 * (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 Muramatsu0a571eb2010-10-15 15:08:00 -070064 assertEquals("Build fingerprint does not match expected format", 6, fingerprintSegs.length);
Brett Chabot365914d2009-09-09 13:55:13 -070065 assertEquals(Build.BRAND, fingerprintSegs[0]);
66 assertEquals(Build.PRODUCT, fingerprintSegs[1]);
Brian Muramatsu0a571eb2010-10-15 15:08:00 -070067
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 Sauer7d664da2014-07-30 09:26:48 -070074
Brian Muramatsu0a571eb2010-10-15 15:08:00 -070075 assertTrue(fingerprintSegs[4].contains(":"));
Nicholas Sauer7d664da2014-07-30 09:26:48 -070076 String[] buildNumberVariant = fingerprintSegs[4].split(":");
77 String buildVariant = buildNumberVariant[1];
Nicholas Sauer0229ed12014-10-06 16:52:49 -070078 assertEquals("Variant", EXPECTED_BUILD_VARIANT, buildVariant);
79 assertEquals("Tag", EXPECTED_TAG, fingerprintSegs[5]);
Brett Chabot365914d2009-09-09 13:55:13 -070080 }
Brian Muramatsu6259d612011-03-07 17:47:22 -080081
82 private void assertNotEmpty(String value) {
83 assertNotNull(value);
84 assertFalse(value.isEmpty());
85 }
Nicholas Sauer7d664da2014-07-30 09:26:48 -070086
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 Dubach5d3e1b92009-04-23 10:12:28 -070094}