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