Mårten Kongstad | f242ec8 | 2024-04-16 17:12:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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.checkflaggedapis |
| 17 | |
Mårten Kongstad | 387ff6c | 2024-04-16 12:42:14 +0200 | [diff] [blame] | 18 | import android.aconfig.Aconfig |
Mårten Kongstad | f242ec8 | 2024-04-16 17:12:26 +0200 | [diff] [blame] | 19 | import com.android.tradefed.testtype.DeviceJUnit4ClassRunner |
| 20 | import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test |
Mårten Kongstad | 387ff6c | 2024-04-16 12:42:14 +0200 | [diff] [blame] | 21 | import java.io.ByteArrayInputStream |
| 22 | import java.io.ByteArrayOutputStream |
Mårten Kongstad | 9238a3a | 2024-04-16 13:19:50 +0200 | [diff] [blame^] | 23 | import java.io.InputStream |
Mårten Kongstad | 20de405 | 2024-04-16 11:33:56 +0200 | [diff] [blame] | 24 | import org.junit.Assert.assertEquals |
Mårten Kongstad | f242ec8 | 2024-04-16 17:12:26 +0200 | [diff] [blame] | 25 | import org.junit.Test |
| 26 | import org.junit.runner.RunWith |
| 27 | |
Mårten Kongstad | 20de405 | 2024-04-16 11:33:56 +0200 | [diff] [blame] | 28 | private val API_SIGNATURE = |
| 29 | """ |
| 30 | // Signature format: 2.0 |
| 31 | package android { |
| 32 | public final class Clazz { |
| 33 | ctor public Clazz(); |
| 34 | field @FlaggedApi("android.flag.foo") public static final int FOO = 1; // 0x1 |
| 35 | } |
| 36 | } |
| 37 | """ |
| 38 | .trim() |
| 39 | |
Mårten Kongstad | b673d3b | 2024-04-16 18:34:20 +0200 | [diff] [blame] | 40 | private val API_VERSIONS = |
| 41 | """ |
| 42 | <?xml version="1.0" encoding="utf-8"?> |
| 43 | <api version="3"> |
| 44 | <class name="android/Clazz" since="1"> |
| 45 | <method name="<init>()V"/> |
| 46 | <field name="FOO"/> |
| 47 | </class> |
| 48 | </api> |
| 49 | """ |
| 50 | .trim() |
| 51 | |
Mårten Kongstad | 9238a3a | 2024-04-16 13:19:50 +0200 | [diff] [blame^] | 52 | private fun generateFlagsProto(fooState: Aconfig.flag_state): InputStream { |
| 53 | val parsed_flag = |
| 54 | Aconfig.parsed_flag |
| 55 | .newBuilder() |
| 56 | .setPackage("android.flag") |
| 57 | .setName("foo") |
| 58 | .setState(fooState) |
| 59 | .setPermission(Aconfig.flag_permission.READ_ONLY) |
| 60 | .build() |
| 61 | val parsed_flags = Aconfig.parsed_flags.newBuilder().addParsedFlag(parsed_flag).build() |
| 62 | val binaryProto = ByteArrayOutputStream() |
| 63 | parsed_flags.writeTo(binaryProto) |
| 64 | return ByteArrayInputStream(binaryProto.toByteArray()) |
| 65 | } |
| 66 | |
Mårten Kongstad | f242ec8 | 2024-04-16 17:12:26 +0200 | [diff] [blame] | 67 | @RunWith(DeviceJUnit4ClassRunner::class) |
| 68 | class CheckFlaggedApisTest : BaseHostJUnit4Test() { |
Mårten Kongstad | 20de405 | 2024-04-16 11:33:56 +0200 | [diff] [blame] | 69 | @Test |
| 70 | fun testParseApiSignature() { |
| 71 | val expected = setOf(Pair(Symbol("android.Clazz.FOO"), Flag("android.flag.foo"))) |
| 72 | val actual = parseApiSignature("in-memory", API_SIGNATURE.byteInputStream()) |
| 73 | assertEquals(expected, actual) |
| 74 | } |
Mårten Kongstad | 387ff6c | 2024-04-16 12:42:14 +0200 | [diff] [blame] | 75 | |
| 76 | @Test |
| 77 | fun testParseFlagValues() { |
| 78 | val expected: Map<Flag, Boolean> = mapOf(Flag("android.flag.foo") to true) |
Mårten Kongstad | 9238a3a | 2024-04-16 13:19:50 +0200 | [diff] [blame^] | 79 | val actual = parseFlagValues(generateFlagsProto(Aconfig.flag_state.ENABLED)) |
Mårten Kongstad | 387ff6c | 2024-04-16 12:42:14 +0200 | [diff] [blame] | 80 | assertEquals(expected, actual) |
| 81 | } |
Mårten Kongstad | b673d3b | 2024-04-16 18:34:20 +0200 | [diff] [blame] | 82 | |
| 83 | @Test |
| 84 | fun testParseApiVersions() { |
| 85 | val expected: Set<Symbol> = setOf(Symbol("android.Clazz.FOO")) |
| 86 | val actual = parseApiVersions(API_VERSIONS.byteInputStream()) |
| 87 | assertEquals(expected, actual) |
| 88 | } |
Mårten Kongstad | 9238a3a | 2024-04-16 13:19:50 +0200 | [diff] [blame^] | 89 | |
| 90 | @Test |
| 91 | fun testFindErrorsNoErrors() { |
| 92 | val expected = setOf<ApiError>() |
| 93 | val actual = |
| 94 | findErrors( |
| 95 | parseApiSignature("in-memory", API_SIGNATURE.byteInputStream()), |
| 96 | parseFlagValues(generateFlagsProto(Aconfig.flag_state.ENABLED)), |
| 97 | parseApiVersions(API_VERSIONS.byteInputStream())) |
| 98 | assertEquals(expected, actual) |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | fun testFindErrorsDisabledFlaggedApiIsPresent() { |
| 103 | val expected = |
| 104 | setOf<ApiError>( |
| 105 | DisabledFlaggedApiIsPresentError(Symbol("android.Clazz.FOO"), Flag("android.flag.foo"))) |
| 106 | val actual = |
| 107 | findErrors( |
| 108 | parseApiSignature("in-memory", API_SIGNATURE.byteInputStream()), |
| 109 | parseFlagValues(generateFlagsProto(Aconfig.flag_state.DISABLED)), |
| 110 | parseApiVersions(API_VERSIONS.byteInputStream())) |
| 111 | assertEquals(expected, actual) |
| 112 | } |
Mårten Kongstad | f242ec8 | 2024-04-16 17:12:26 +0200 | [diff] [blame] | 113 | } |