1 /*
2  * Copyright (C) 2022 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 android.hardware.google.pixel;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import static org.junit.Assert.fail;
22 
23 import com.google.common.collect.ImmutableSet;
24 import com.google.protobuf.Descriptors;
25 
26 public abstract class BasePixelAtomsTest {
27 
getAllowlistedAtoms()28     protected abstract ImmutableSet<Integer> getAllowlistedAtoms();
29 
doTestPushedAtomsHasReverseDomainName(Descriptors.Descriptor vendorAtoms)30     protected void doTestPushedAtomsHasReverseDomainName(Descriptors.Descriptor vendorAtoms) {
31         for (Descriptors.FieldDescriptor field : vendorAtoms.getFields()) {
32             Descriptors.OneofDescriptor oneOfAtom = field.getContainingOneof();
33             if (oneOfAtom == null) {
34                 fail("Atom not declared in a 'oneof' field");
35             }
36             if ("pushed".equals(oneOfAtom.getName())) {
37                 int atomId = field.getNumber();
38                 if (getAllowlistedAtoms().contains(atomId)) {
39                     continue;
40                 }
41                 if (atomId < 100001 || atomId > 150000) {
42                     fail("Atom id not in vendor range");
43                 }
44                 String atomName = field.getName();
45                 assertWithMessage(atomName + " field 1 should not be empty")
46                         .that(field.getMessageType().findFieldByNumber(1))
47                         .isNotNull();
48                 assertWithMessage(atomName + "field 1 should be of string type")
49                         .that(field.getMessageType().findFieldByNumber(1).getType())
50                         .isEqualTo(Descriptors.FieldDescriptor.Type.STRING);
51                 assertWithMessage(atomName +
52                         " should contain reverse_domain_name as field 1.")
53                         .that(field.getMessageType().findFieldByNumber(1).getName())
54                         .isEqualTo("reverse_domain_name");
55             }
56         }
57     }
58 }
59