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

Commit 90cf47db authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Add tests for ProtoLogViewerConfigReaderTest

Test: atest com.android.internal.protolog.ProtoLogViewerConfigReaderTest

Flag: TEST_ONLY
Change-Id: I99b79638ac2034ffb5d46a9a7639399715db3cf0
parent 974693e5
Loading
Loading
Loading
Loading
+114 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 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.internal.protolog;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import android.platform.test.annotations.Presubmit;

import androidx.test.filters.SmallTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.zip.GZIPOutputStream;

@SmallTest
@Presubmit
@RunWith(JUnit4.class)
public class LegacyProtoLogViewerConfigReaderTest {
    private static final String TEST_VIEWER_CONFIG = "{\n"
            + "  \"version\": \"1.0.0\",\n"
            + "  \"messages\": {\n"
            + "    \"70933285\": {\n"
            + "      \"message\": \"Test completed successfully: %b\",\n"
            + "      \"level\": \"ERROR\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    },\n"
            + "    \"1792430067\": {\n"
            + "      \"message\": \"Attempted to add window to a display that does not exist: %d."
            + "  Aborting.\",\n"
            + "      \"level\": \"WARN\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    },\n"
            + "    \"1352021864\": {\n"
            + "      \"message\": \"Test 2\",\n"
            + "      \"level\": \"WARN\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    },\n"
            + "    \"409412266\": {\n"
            + "      \"message\": \"Window %s is already added\",\n"
            + "      \"level\": \"WARN\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    }\n"
            + "  },\n"
            + "  \"groups\": {\n"
            + "    \"GENERIC_WM\": {\n"
            + "      \"tag\": \"WindowManager\"\n"
            + "    }\n"
            + "  }\n"
            + "}\n";


    private LegacyProtoLogViewerConfigReader
            mConfig = new LegacyProtoLogViewerConfigReader();
    private File mTestViewerConfig;

    @Before
    public void setUp() throws IOException {
        mTestViewerConfig = File.createTempFile("testConfig", ".json.gz");
        OutputStreamWriter writer = new OutputStreamWriter(
                new GZIPOutputStream(new FileOutputStream(mTestViewerConfig)));
        writer.write(TEST_VIEWER_CONFIG);
        writer.close();
    }

    @After
    public void tearDown() {
        //noinspection ResultOfMethodCallIgnored
        mTestViewerConfig.delete();
    }

    @Test
    public void getViewerString_notLoaded() {
        assertNull(mConfig.getViewerString(1));
    }

    @Test
    public void loadViewerConfig() {
        mConfig.loadViewerConfig(msg -> {}, mTestViewerConfig.getAbsolutePath());
        assertEquals("Test completed successfully: %b", mConfig.getViewerString(70933285));
        assertEquals("Test 2", mConfig.getViewerString(1352021864));
        assertEquals("Window %s is already added", mConfig.getViewerString(409412266));
        assertNull(mConfig.getViewerString(1));
    }

    @Test
    public void loadViewerConfig_invalidFile() {
        mConfig.loadViewerConfig(msg -> {}, "/tmp/unknown/file/does/not/exist");
        // No exception is thrown.
        assertNull(mConfig.getViewerString(1));
    }
}
+74 −63
Original line number Original line Diff line number Diff line
@@ -20,75 +20,77 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNull;


import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.Presubmit;
import android.util.proto.ProtoInputStream;


import androidx.test.filters.SmallTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.JUnit4;


import java.io.File;
import perfetto.protos.Protolog;
import java.io.FileOutputStream;
import perfetto.protos.ProtologCommon;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.zip.GZIPOutputStream;


@SmallTest
@Presubmit
@Presubmit
@RunWith(JUnit4.class)
@RunWith(JUnit4.class)
public class ProtoLogViewerConfigReaderTest {
public class ProtoLogViewerConfigReaderTest {
    private static final String TEST_VIEWER_CONFIG = "{\n"
    private static final String TEST_GROUP_NAME = "MY_TEST_GROUP";
            + "  \"version\": \"1.0.0\",\n"
    private static final String TEST_GROUP_TAG = "TEST";
            + "  \"messages\": {\n"
            + "    \"70933285\": {\n"
            + "      \"message\": \"Test completed successfully: %b\",\n"
            + "      \"level\": \"ERROR\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    },\n"
            + "    \"1792430067\": {\n"
            + "      \"message\": \"Attempted to add window to a display that does not exist: %d."
            + "  Aborting.\",\n"
            + "      \"level\": \"WARN\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    },\n"
            + "    \"1352021864\": {\n"
            + "      \"message\": \"Test 2\",\n"
            + "      \"level\": \"WARN\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    },\n"
            + "    \"409412266\": {\n"
            + "      \"message\": \"Window %s is already added\",\n"
            + "      \"level\": \"WARN\",\n"
            + "      \"group\": \"GENERIC_WM\"\n"
            + "    }\n"
            + "  },\n"
            + "  \"groups\": {\n"
            + "    \"GENERIC_WM\": {\n"
            + "      \"tag\": \"WindowManager\"\n"
            + "    }\n"
            + "  }\n"
            + "}\n";


    private static final String OTHER_TEST_GROUP_NAME = "MY_OTHER_TEST_GROUP";
    private static final String OTHER_TEST_GROUP_TAG = "OTHER_TEST";


    private LegacyProtoLogViewerConfigReader
    private static final byte[] TEST_VIEWER_CONFIG =
            mConfig = new LegacyProtoLogViewerConfigReader();
            perfetto.protos.Protolog.ProtoLogViewerConfig.newBuilder()
    private File mTestViewerConfig;
                .addGroups(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.Group.newBuilder()
                                .setId(1)
                                .setName(TEST_GROUP_NAME)
                                .setTag(TEST_GROUP_TAG)
                ).addGroups(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.Group.newBuilder()
                                .setId(1)
                                .setName(OTHER_TEST_GROUP_NAME)
                                .setTag(OTHER_TEST_GROUP_TAG)
                ).addMessages(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder()
                                .setMessageId(1)
                                .setMessage("My Test Log Message 1 %b")
                                .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_DEBUG)
                                .setGroupId(1)
                ).addMessages(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder()
                                .setMessageId(2)
                                .setMessage("My Test Log Message 2 %b")
                                .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_VERBOSE)
                                .setGroupId(1)
                ).addMessages(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder()
                                .setMessageId(3)
                                .setMessage("My Test Log Message 3 %b")
                                .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_WARN)
                                .setGroupId(1)
                ).addMessages(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder()
                                .setMessageId(4)
                                .setMessage("My Test Log Message 4 %b")
                                .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_ERROR)
                                .setGroupId(2)
                ).addMessages(
                        perfetto.protos.Protolog.ProtoLogViewerConfig.MessageData.newBuilder()
                                .setMessageId(5)
                                .setMessage("My Test Log Message 5 %b")
                                .setLevel(ProtologCommon.ProtoLogLevel.PROTOLOG_LEVEL_WTF)
                                .setGroupId(2)
                ).build().toByteArray();


    @Before
    private final ViewerConfigInputStreamProvider mViewerConfigInputStreamProvider =
    public void setUp() throws IOException {
            () -> new ProtoInputStream(TEST_VIEWER_CONFIG);
        mTestViewerConfig = File.createTempFile("testConfig", ".json.gz");

        OutputStreamWriter writer = new OutputStreamWriter(
    private ProtoLogViewerConfigReader mConfig;
                new GZIPOutputStream(new FileOutputStream(mTestViewerConfig)));
        writer.write(TEST_VIEWER_CONFIG);
        writer.close();
    }


    @After
    @Before
    public void tearDown() {
    public void before() {
        //noinspection ResultOfMethodCallIgnored
        mConfig = new ProtoLogViewerConfigReader(mViewerConfigInputStreamProvider);
        mTestViewerConfig.delete();
    }
    }


    @Test
    @Test
@@ -98,17 +100,26 @@ public class ProtoLogViewerConfigReaderTest {


    @Test
    @Test
    public void loadViewerConfig() {
    public void loadViewerConfig() {
        mConfig.loadViewerConfig(msg -> {}, mTestViewerConfig.getAbsolutePath());
        mConfig.loadViewerConfig(new String[] { TEST_GROUP_NAME });
        assertEquals("Test completed successfully: %b", mConfig.getViewerString(70933285));
        assertEquals("My Test Log Message 1 %b", mConfig.getViewerString(1));
        assertEquals("Test 2", mConfig.getViewerString(1352021864));
        assertEquals("My Test Log Message 2 %b", mConfig.getViewerString(2));
        assertEquals("Window %s is already added", mConfig.getViewerString(409412266));
        assertEquals("My Test Log Message 3 %b", mConfig.getViewerString(3));
        assertNull(mConfig.getViewerString(1));
        assertNull(mConfig.getViewerString(4));
        assertNull(mConfig.getViewerString(5));
    }
    }


    @Test
    @Test
    public void loadViewerConfig_invalidFile() {
    public void unloadViewerConfig() {
        mConfig.loadViewerConfig(msg -> {}, "/tmp/unknown/file/does/not/exist");
        mConfig.loadViewerConfig(new String[] { TEST_GROUP_NAME, OTHER_TEST_GROUP_NAME });
        // No exception is thrown.
        mConfig.unloadViewerConfig(new String[] { TEST_GROUP_NAME });
        assertNull(mConfig.getViewerString(1));
        assertNull(mConfig.getViewerString(1));
        assertNull(mConfig.getViewerString(2));
        assertNull(mConfig.getViewerString(3));
        assertEquals("My Test Log Message 4 %b", mConfig.getViewerString(4));
        assertEquals("My Test Log Message 5 %b", mConfig.getViewerString(5));

        mConfig.unloadViewerConfig(new String[] { OTHER_TEST_GROUP_NAME });
        assertNull(mConfig.getViewerString(4));
        assertNull(mConfig.getViewerString(5));
    }
    }
}
}