Home | History | Annotate | Download | only in util
      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.internal.util;
     18 
     19 import static org.junit.Assert.*;
     20 
     21 import com.android.internal.util.MessageUtils;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 import android.util.SparseArray;
     24 
     25 import org.junit.Test;
     26 
     27 
     28 class A {
     29     // Should not see these.
     30     private int mMember;
     31     public final int CMD_NOT_STATIC = 7;
     32     private static final String CMD_NOT_INT = "not an integer";
     33     public static int CMD_NOT_FINAL = 34;
     34     public static final int kWrongPrefix = 99;
     35 
     36     // Should see these.
     37     private static final int CMD_DO_SOMETHING = 12;
     38     public static final int EVENT_SOMETHING_HAPPENED = 45;
     39 }
     40 
     41 class B {
     42     public static final int CMD_FOO = 56;
     43     public static final int EVENT_BAR = 55;
     44     public static final int NOTIFICATION_BAZ = 12;
     45 }
     46 
     47 /**
     48  * Unit tests for {@link com.android.util.MessageUtils}.
     49  */
     50 @SmallTest
     51 public class MessageUtilsTest {
     52 
     53     private static final Class[] CLASSES = { A.class, B.class };
     54 
     55     private SparseArray<String> makeSparseArray(int[] keys, String[] values) throws Exception {
     56         assertEquals("Must specify same number of keys and values", keys.length, values.length);
     57         SparseArray<String> out = new SparseArray<>();
     58         for (int i = 0; i < keys.length; i++) {
     59             out.put(keys[i], values[i]);
     60         }
     61         return out;
     62     }
     63 
     64     private void assertSparseArrayEquals(
     65             SparseArray<String> a1, SparseArray<String> a2) throws Exception {
     66         String msg = String.format("%s != %s", a1.toString(), a2.toString());
     67         assertEquals(msg, a1.size(), a2.size());
     68         int size = a1.size();
     69         for (int i = 0; i < size; i++) {
     70             assertEquals(msg, a1.keyAt(i), a2.keyAt(i));
     71             assertEquals(msg, a1.valueAt(i), a2.valueAt(i));
     72         }
     73     }
     74 
     75     @Test
     76     public void basicOperation() throws Exception {
     77         SparseArray<String> expected = makeSparseArray(
     78             new int[]{12, 45, 55, 56},
     79             new String[]{"CMD_DO_SOMETHING", "EVENT_SOMETHING_HAPPENED", "EVENT_BAR", "CMD_FOO"});
     80         assertSparseArrayEquals(expected, MessageUtils.findMessageNames(CLASSES));
     81     }
     82 
     83     @Test
     84     public void withPrefixes() throws Exception {
     85         SparseArray<String> expected = makeSparseArray(
     86             new int[]{45, 55},
     87             new String[]{"EVENT_SOMETHING_HAPPENED", "EVENT_BAR"});
     88         assertSparseArrayEquals(expected, MessageUtils.findMessageNames(CLASSES,
     89                 new String[]{"EVENT_"}));
     90     }
     91 
     92     @Test(expected=MessageUtils.DuplicateConstantError.class)
     93     public void duplicateConstants() {
     94         MessageUtils.findMessageNames(CLASSES, new String[]{"CMD_", "NOTIFICATION_"});
     95     }
     96 
     97 }
     98