blob: 5fb67bedabd2a2acc318949083ed63ff5049f3b9 [file] [log] [blame]
Mårten Kongstadf242ec82024-04-16 17:12:26 +02001/*
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 */
16package com.android.checkflaggedapis
17
Mårten Kongstad387ff6c2024-04-16 12:42:14 +020018import android.aconfig.Aconfig
Mårten Kongstadf242ec82024-04-16 17:12:26 +020019import com.android.tradefed.testtype.DeviceJUnit4ClassRunner
20import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test
Mårten Kongstad387ff6c2024-04-16 12:42:14 +020021import java.io.ByteArrayInputStream
22import java.io.ByteArrayOutputStream
Mårten Kongstad20de4052024-04-16 11:33:56 +020023import org.junit.Assert.assertEquals
Mårten Kongstadf242ec82024-04-16 17:12:26 +020024import org.junit.Test
25import org.junit.runner.RunWith
26
Mårten Kongstad20de4052024-04-16 11:33:56 +020027private 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 Kongstad387ff6c2024-04-16 12:42:14 +020039private 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 Kongstadf242ec82024-04-16 17:12:26 +020055@RunWith(DeviceJUnit4ClassRunner::class)
56class CheckFlaggedApisTest : BaseHostJUnit4Test() {
Mårten Kongstad20de4052024-04-16 11:33:56 +020057 @Test
58 fun testParseApiSignature() {
59 val expected = setOf(Pair(Symbol("android.Clazz.FOO"), Flag("android.flag.foo")))
60 val actual = parseApiSignature("in-memory", API_SIGNATURE.byteInputStream())
61 assertEquals(expected, actual)
62 }
Mårten Kongstad387ff6c2024-04-16 12:42:14 +020063
64 @Test
65 fun testParseFlagValues() {
66 val expected: Map<Flag, Boolean> = mapOf(Flag("android.flag.foo") to true)
67 val actual = parseFlagValues(PARSED_FLAGS)
68 assertEquals(expected, actual)
69 }
Mårten Kongstadf242ec82024-04-16 17:12:26 +020070}