blob: 9e6a6e334a542a55ebf074e4d0009f1bfe34afda [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 Kongstadb673d3b2024-04-16 18:34:20 +020055private 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="&lt;init>()V"/>
61 <field name="FOO"/>
62 </class>
63 </api>
64"""
65 .trim()
66
Mårten Kongstadf242ec82024-04-16 17:12:26 +020067@RunWith(DeviceJUnit4ClassRunner::class)
68class CheckFlaggedApisTest : BaseHostJUnit4Test() {
Mårten Kongstad20de4052024-04-16 11:33:56 +020069 @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 Kongstad387ff6c2024-04-16 12:42:14 +020075
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 Kongstadb673d3b2024-04-16 18:34:20 +020082
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 Kongstadf242ec82024-04-16 17:12:26 +020089}