Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0bda0309 authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Add test to make sure we use ids from ProtoLogGroups in ProtoLogTool

Test: atest protologtool-tests
Flag: TEST_ONLY
Bug: 424792568
Change-Id: I2786cd73bf8d214ff7af71e7b6e9ad2f84f65d68
parent ae21ae5d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ java_test_host {
    test_suites: ["general-tests"],
    srcs: [
        "tests/**/*.kt",
        "tests/**/*.java",
    ],
    test_options: {
        unit_test: true,
+21 −9
Original line number Diff line number Diff line
@@ -38,7 +38,22 @@ class ProtoLogGroupReader {
        try {
            val classLoader = getClassloaderForJar(jarPath)
            val clazz = classLoader.loadClass(className)
            return loadFromClass(clazz)
        } catch (ex: ReflectiveOperationException) {
            throw RuntimeException("Unable to load ProtoLogGroup enum class", ex)
        }
    }

    fun loadFromClass(clazz: Class<*>): Map<String, LogGroup> {
        try {
            val values = getEnumValues(clazz)
            return loadFromIProtoLogGroups(values)
        } catch (ex: ReflectiveOperationException) {
            throw RuntimeException("Unable to load ProtoLogGroup enum class", ex)
        }
    }

    fun loadFromIProtoLogGroups(values: List<IProtoLogGroup>): Map<String, LogGroup> {
        return values.map { group ->
            group.name() to LogGroup(
                group.name(),
@@ -48,8 +63,5 @@ class ProtoLogGroupReader {
                group.id
            )
        }.toMap()
        } catch (ex: ReflectiveOperationException) {
            throw RuntimeException("Unable to load ProtoLogGroup enum class", ex)
        }
    }
}
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.protolog.tool

import com.google.common.truth.Truth.assertThat
import org.junit.Test

class ProtoLogGroupReaderTest {
    @Test
    fun loadFromClass_usesProvidedGroupId() {
        val reader = ProtoLogGroupReader()
        val groups = reader.loadFromClass(TestProtoLogGroups::class.java)

        assertThat(groups).hasSize(TestProtoLogGroups.entries.size)
        assertThat(groups["TEST_GROUP_1"]?.id).isEqualTo(TestProtoLogGroups.TEST_GROUP_1.id)
        assertThat(groups["TEST_GROUP_2"]?.id).isEqualTo(TestProtoLogGroups.TEST_GROUP_2.id)
    }
}
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.protolog.tool;

import com.android.internal.protolog.common.IProtoLogGroup;

import java.util.UUID;

public enum TestProtoLogGroups implements IProtoLogGroup {

    TEST_GROUP_1(true, true, false, "TEST_TAG_1"),
    TEST_GROUP_2(true, false, true, "TEST_TAG_2");

    private final boolean mEnabled;
    private volatile boolean mLogToProto;
    private volatile boolean mLogToLogcat;
    private final String mTag;

    TestProtoLogGroups(boolean enabled, boolean logToProto, boolean logToLogcat, String tag) {
        this.mEnabled = enabled;
        this.mLogToProto = logToProto;
        this.mLogToLogcat = logToLogcat;
        this.mTag = tag;
    }

    @Override
    public boolean isEnabled() {
        return mEnabled;
    }

    @Override
    public boolean isLogToProto() {
        return mLogToProto;
    }

    @Override
    public boolean isLogToLogcat() {
        return mLogToLogcat;
    }

    @Override
    public boolean isLogToAny() {
        return mLogToLogcat || mLogToProto;
    }

    @Override
    public String getTag() {
        return mTag;
    }

    @Override
    public void setLogToProto(boolean logToProto) {
        this.mLogToProto = logToProto;
    }

    @Override
    public void setLogToLogcat(boolean logToLogcat) {
        this.mLogToLogcat = logToLogcat;
    }

    @Override
    public int getId() {
        return Consts.START_ID + this.ordinal();
    }

    private static class Consts {
        private static final int START_ID = (int) (
                UUID.nameUUIDFromBytes(TestProtoLogGroups.class.getName().getBytes())
                        .getMostSignificantBits() % Integer.MAX_VALUE);
    }
}