1 /* 2 * Copyright (C) 2019 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 17 package com.android.internal.protolog; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 22 import android.platform.test.annotations.Presubmit; 23 24 import androidx.test.filters.SmallTest; 25 26 import org.junit.After; 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 import java.io.File; 33 import java.io.FileOutputStream; 34 import java.io.IOException; 35 import java.io.OutputStreamWriter; 36 import java.util.zip.GZIPOutputStream; 37 38 @SmallTest 39 @Presubmit 40 @RunWith(JUnit4.class) 41 public class ProtoLogViewerConfigReaderTest { 42 private static final String TEST_VIEWER_CONFIG = "{\n" 43 + " \"version\": \"1.0.0\",\n" 44 + " \"messages\": {\n" 45 + " \"70933285\": {\n" 46 + " \"message\": \"Test completed successfully: %b\",\n" 47 + " \"level\": \"ERROR\",\n" 48 + " \"group\": \"GENERIC_WM\"\n" 49 + " },\n" 50 + " \"1792430067\": {\n" 51 + " \"message\": \"Attempted to add window to a display that does not exist: %d." 52 + " Aborting.\",\n" 53 + " \"level\": \"WARN\",\n" 54 + " \"group\": \"GENERIC_WM\"\n" 55 + " },\n" 56 + " \"1352021864\": {\n" 57 + " \"message\": \"Test 2\",\n" 58 + " \"level\": \"WARN\",\n" 59 + " \"group\": \"GENERIC_WM\"\n" 60 + " },\n" 61 + " \"409412266\": {\n" 62 + " \"message\": \"Window %s is already added\",\n" 63 + " \"level\": \"WARN\",\n" 64 + " \"group\": \"GENERIC_WM\"\n" 65 + " }\n" 66 + " },\n" 67 + " \"groups\": {\n" 68 + " \"GENERIC_WM\": {\n" 69 + " \"tag\": \"WindowManager\"\n" 70 + " }\n" 71 + " }\n" 72 + "}\n"; 73 74 75 private ProtoLogViewerConfigReader 76 mConfig = new ProtoLogViewerConfigReader(); 77 private File mTestViewerConfig; 78 79 @Before setUp()80 public void setUp() throws IOException { 81 mTestViewerConfig = File.createTempFile("testConfig", ".json.gz"); 82 OutputStreamWriter writer = new OutputStreamWriter( 83 new GZIPOutputStream(new FileOutputStream(mTestViewerConfig))); 84 writer.write(TEST_VIEWER_CONFIG); 85 writer.close(); 86 } 87 88 @After tearDown()89 public void tearDown() { 90 //noinspection ResultOfMethodCallIgnored 91 mTestViewerConfig.delete(); 92 } 93 94 @Test getViewerString_notLoaded()95 public void getViewerString_notLoaded() { 96 assertNull(mConfig.getViewerString(1)); 97 } 98 99 @Test loadViewerConfig()100 public void loadViewerConfig() { 101 mConfig.loadViewerConfig(null, mTestViewerConfig.getAbsolutePath()); 102 assertEquals("Test completed successfully: %b", mConfig.getViewerString(70933285)); 103 assertEquals("Test 2", mConfig.getViewerString(1352021864)); 104 assertEquals("Window %s is already added", mConfig.getViewerString(409412266)); 105 assertNull(mConfig.getViewerString(1)); 106 } 107 108 @Test loadViewerConfig_invalidFile()109 public void loadViewerConfig_invalidFile() { 110 mConfig.loadViewerConfig(null, "/tmp/unknown/file/does/not/exist"); 111 // No exception is thrown. 112 assertNull(mConfig.getViewerString(1)); 113 } 114 } 115