1 /*
2  * Copyright (C) 2017 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.layoutlib.bridge.impl;
18 
19 import org.junit.Test;
20 
21 import static org.junit.Assert.*;
22 
23 public class ResourceHelperTest {
assertNumberFormatException(Runnable runnable)24     private static void assertNumberFormatException(Runnable runnable) {
25         try {
26             runnable.run();
27             fail("NumberFormatException expected");
28         } catch (NumberFormatException ignored) {
29         }
30     }
31 
32     @Test
testGetColor()33     public void testGetColor() {
34         assertNumberFormatException(() -> ResourceHelper.getColor(""));
35         assertNumberFormatException(() -> ResourceHelper.getColor("AFAFAF"));
36         assertNumberFormatException(() -> ResourceHelper.getColor("AAA"));
37         assertNumberFormatException(() -> ResourceHelper.getColor("#JFAFAF"));
38         assertNumberFormatException(() -> ResourceHelper.getColor("#AABBCCDDEE"));
39         assertNumberFormatException(() -> ResourceHelper.getColor("#JAAA"));
40         assertNumberFormatException(() -> ResourceHelper.getColor("#AA BBCC"));
41 
42         assertEquals(0xffaaaaaa, ResourceHelper.getColor("#AAA"));
43         assertEquals(0xffaaaaaa, ResourceHelper.getColor("  #AAA"));
44         assertEquals(0xffaaaaaa, ResourceHelper.getColor("#AAA   "));
45         assertEquals(0xffaaaaaa, ResourceHelper.getColor("  #AAA   "));
46         assertEquals(0xaaaaaa, ResourceHelper.getColor("#0AAA"));
47         assertEquals(0xffaabbcc, ResourceHelper.getColor("#AABBCC"));
48         assertEquals(0x12aabbcc, ResourceHelper.getColor("#12AABBCC"));
49         assertEquals(0x12345, ResourceHelper.getColor("#12345"));
50     }
51 }