1 /*
2  * Copyright (C) 2016 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.tradefed.result;
18 
19 import com.android.test.metrics.proto.FileMetadataProto.FileMetadata;
20 import com.android.test.metrics.proto.FileMetadataProto.LogFile;
21 import com.android.test.metrics.proto.FileMetadataProto.LogType;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 
28 /** Unit tests for {@link com.android.tradefed.result.FileMetadataCollector}. */
29 @RunWith(JUnit4.class)
30 public class FileMetadataCollectorTest {
31     private FileMetadataCollector mCollector = null;
32 
33     private static final LogDataType DATA_TYPE_BR = LogDataType.BUGREPORT;
34     private static final LogDataType DATA_TYPE_LC = LogDataType.LOGCAT;
35     private static final LogType LOG_TYPE_BR = LogType.BUGREPORT;
36     private static final LogType LOG_TYPE_LC = LogType.LOGCAT;
37     private static final String NAME_BR1 = "br1";
38     private static final String NAME_BR2 = "br2";
39     private static final String NAME_LC1 = "lc1";
40     private static final String NAME_LC2 = "lc2";
41 
42     @Before
setUp()43     public void setUp() {
44         mCollector = new FileMetadataCollector();
45     }
46 
47     /** A simple test to ensure expected output is generated for test run with no logs. */
48     @Test
testNoElements()49     public void testNoElements() throws Exception {
50         // Generate actual value
51         FileMetadata actual = mCollector.getMetadataContents();
52         // Generate expected value
53         FileMetadata expected = FileMetadata.newBuilder().build();
54         // Assert equality
55         Assert.assertEquals(actual, expected);
56     }
57 
58     /**
59      * A simple test to ensure expected output is generated for test run with a single log type with a
60      * single log.
61      */
62     @Test
testSingleTypeSingleElement()63     public void testSingleTypeSingleElement() throws Exception {
64         // Generate actual value
65         mCollector.testLogSaved(NAME_BR1, DATA_TYPE_BR, null, null);
66         FileMetadata actual = mCollector.getMetadataContents();
67         // Generate expected value
68         LogFile log = LogFile.newBuilder().setLogType(LOG_TYPE_BR).setName(NAME_BR1).build();
69         FileMetadata expected = FileMetadata.newBuilder().addLogFiles(log).build();
70         // Assert equality
71         Assert.assertEquals(actual, expected);
72     }
73 
74     /**
75      * A simple test to ensure expected output is generated for test run with a single log type, but
76      * multiple log files.
77      */
78     @Test
testSingleTypeMultipleElements()79     public void testSingleTypeMultipleElements() throws Exception {
80         // Generate actual value
81         mCollector.testLogSaved(NAME_BR1, DATA_TYPE_BR, null, null);
82         mCollector.testLogSaved(NAME_BR2, DATA_TYPE_BR, null, null);
83         FileMetadata actual = mCollector.getMetadataContents();
84         // Generate expected value
85         LogFile log1 = LogFile.newBuilder().setLogType(LOG_TYPE_BR).setName(NAME_BR1).build();
86         LogFile log2 = LogFile.newBuilder().setLogType(LOG_TYPE_BR).setName(NAME_BR2).build();
87         FileMetadata expected = FileMetadata.newBuilder()
88                 .addLogFiles(log1).addLogFiles(log2).build();
89         // Assert equality
90         Assert.assertEquals(actual, expected);
91     }
92 
93     /**
94      * A simple test to ensure expected output is generated for test run with multiple log types and
95      * multiple log files.
96      */
97     @Test
testMultipleTypesMultipleElements()98     public void testMultipleTypesMultipleElements() throws Exception {
99         // Generate actual value
100         mCollector.testLogSaved(NAME_BR1, DATA_TYPE_BR, null, null);
101         mCollector.testLogSaved(NAME_BR2, DATA_TYPE_BR, null, null);
102         mCollector.testLogSaved(NAME_LC1, DATA_TYPE_LC, null, null);
103         mCollector.testLogSaved(NAME_LC2, DATA_TYPE_LC, null, null);
104         FileMetadata actual = mCollector.getMetadataContents();
105         // Generate expected value
106         LogFile log1 = LogFile.newBuilder().setLogType(LOG_TYPE_BR).setName(NAME_BR1).build();
107         LogFile log2 = LogFile.newBuilder().setLogType(LOG_TYPE_BR).setName(NAME_BR2).build();
108         LogFile log3 = LogFile.newBuilder().setLogType(LOG_TYPE_LC).setName(NAME_LC1).build();
109         LogFile log4 = LogFile.newBuilder().setLogType(LOG_TYPE_LC).setName(NAME_LC2).build();
110         FileMetadata expected = FileMetadata.newBuilder()
111                 .addLogFiles(log1).addLogFiles(log2).addLogFiles(log3).addLogFiles(log4).build();
112         // Assert equality
113         Assert.assertEquals(actual, expected);
114     }
115 }
116