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 | 20de405 | 2024-04-16 11:33:56 +0200 | [diff] [blame] | 23 | import org.junit.Assert.assertEquals |
Mårten Kongstad | f242ec8 | 2024-04-16 17:12:26 +0200 | [diff] [blame] | 24 | import org.junit.Test |
| 25 | import org.junit.runner.RunWith |
| 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 | 387ff6c | 2024-04-16 12:42:14 +0200 | [diff] [blame] | 39 | private val PARSED_FLAGS = |
| 40 | { |
| 41 | val parsed_flag = |
| 42 | Aconfig.parsed_flag |
| 43 | .newBuilder() |
| 44 | .setPackage("android.flag") |
| 45 | .setName("foo") |
| 46 | .setState(Aconfig.flag_state.ENABLED) |
| 47 | .setPermission(Aconfig.flag_permission.READ_ONLY) |
| 48 | .build() |
| 49 | val parsed_flags = Aconfig.parsed_flags.newBuilder().addParsedFlag(parsed_flag).build() |
| 50 | val binaryProto = ByteArrayOutputStream() |
| 51 | parsed_flags.writeTo(binaryProto) |
| 52 | ByteArrayInputStream(binaryProto.toByteArray()) |
| 53 | }() |
| 54 | |
Mårten Kongstad | b673d3b | 2024-04-16 18:34:20 +0200 | [diff] [blame^] | 55 | private val API_VERSIONS = |
| 56 | """ |
| 57 | <?xml version="1.0" encoding="utf-8"?> |
| 58 | <api version="3"> |
| 59 | <class name="android/Clazz" since="1"> |
| 60 | <method name="<init>()V"/> |
| 61 | <field name="FOO"/> |
| 62 | </class> |
| 63 | </api> |
| 64 | """ |
| 65 | .trim() |
| 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) |
| 79 | val actual = parseFlagValues(PARSED_FLAGS) |
| 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 | f242ec8 | 2024-04-16 17:12:26 +0200 | [diff] [blame] | 89 | } |