1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.ide.eclipse.adt.internal.resources;
18 
19 import com.android.ide.common.resources.ResourceDeltaKind;
20 import com.android.ide.common.resources.configuration.FolderConfiguration;
21 import com.android.ide.common.resources.configuration.ResourceQualifier;
22 import com.android.resources.ResourceType;
23 
24 import org.eclipse.core.resources.IResourceDelta;
25 
26 import junit.framework.TestCase;
27 
28 
29 /**
30  * Test ResourceHelper
31  */
32 @SuppressWarnings("javadoc")
33 public class ResourceHelperTest extends TestCase {
34 
35     /**
36      * temp fake qualifier class.
37      */
38     private static class FakeQualifierClass extends ResourceQualifier {
39 
40         @Override
checkAndSet(String value, FolderConfiguration config)41         public boolean checkAndSet(String value, FolderConfiguration config) {
42             return false;
43         }
44 
45         @Override
equals(Object object)46         public boolean equals(Object object) {
47             return false;
48         }
49 
50         @Override
getFolderSegment()51         public String getFolderSegment() {
52             return null;
53         }
54 
55         @Override
getLongDisplayValue()56         public String getLongDisplayValue() {
57             return null;
58         }
59 
60         @Override
getName()61         public String getName() {
62             return null;
63         }
64 
65         @Override
getShortDisplayValue()66         public String getShortDisplayValue() {
67             return null;
68         }
69 
70         @Override
getShortName()71         public String getShortName() {
72             return null;
73         }
74 
75         @Override
hasFakeValue()76         public boolean hasFakeValue() {
77             return false;
78         }
79 
80         @Override
hashCode()81         public int hashCode() {
82             return 0;
83         }
84 
85         @Override
isValid()86         public boolean isValid() {
87             return false;
88         }
89 
90         @Override
since()91         public int since() {
92             return 0;
93         }
94     }
95 
testGetIcon()96     public void testGetIcon() throws Exception {
97         // check that the method returns null for an unknown qualifier class
98         assertNull(ResourceHelper.getIcon(FakeQualifierClass.class));
99 
100         // find all the qualifiers through FolderConfiguration.createdefault()
101         FolderConfiguration config = new FolderConfiguration();
102         config.createDefault();
103         final int count = FolderConfiguration.getQualifierCount();
104         for (int i = 0 ; i < count ; i++) {
105             ResourceQualifier qual = config.getQualifier(i);
106             assertNotNull(qual);
107             assertNotNull(qual.getClass().getCanonicalName(),
108                     ResourceHelper.getIcon(qual.getClass()));
109         }
110     }
111 
testGetResourceDeltaKind()112     public void testGetResourceDeltaKind() {
113         assertEquals(ResourceDeltaKind.ADDED,
114                 ResourceHelper.getResourceDeltaKind(IResourceDelta.ADDED));
115         assertEquals(ResourceDeltaKind.REMOVED,
116                 ResourceHelper.getResourceDeltaKind(IResourceDelta.REMOVED));
117         assertEquals(ResourceDeltaKind.CHANGED,
118                 ResourceHelper.getResourceDeltaKind(IResourceDelta.CHANGED));
119 
120         assertNull(ResourceHelper.getResourceDeltaKind(IResourceDelta.ADDED_PHANTOM));
121     }
122 
testIsFileBasedResourceType()123     public void testIsFileBasedResourceType() throws Exception {
124         assertTrue(ResourceHelper.isFileBasedResourceType(ResourceType.ANIMATOR));
125         assertTrue(ResourceHelper.isFileBasedResourceType(ResourceType.LAYOUT));
126 
127         assertFalse(ResourceHelper.isFileBasedResourceType(ResourceType.STRING));
128         assertFalse(ResourceHelper.isFileBasedResourceType(ResourceType.DIMEN));
129         assertFalse(ResourceHelper.isFileBasedResourceType(ResourceType.ID));
130 
131         // Both:
132         assertTrue(ResourceHelper.isFileBasedResourceType(ResourceType.DRAWABLE));
133         assertTrue(ResourceHelper.isFileBasedResourceType(ResourceType.COLOR));
134     }
135 
testIsValueBasedResourceType()136     public void testIsValueBasedResourceType() throws Exception {
137         assertTrue(ResourceHelper.isValueBasedResourceType(ResourceType.STRING));
138         assertTrue(ResourceHelper.isValueBasedResourceType(ResourceType.DIMEN));
139         assertTrue(ResourceHelper.isValueBasedResourceType(ResourceType.ID));
140 
141         assertFalse(ResourceHelper.isValueBasedResourceType(ResourceType.LAYOUT));
142 
143         // These can be both:
144         assertTrue(ResourceHelper.isValueBasedResourceType(ResourceType.DRAWABLE));
145         assertTrue(ResourceHelper.isValueBasedResourceType(ResourceType.COLOR));
146     }
147 
testCanCreateResource()148     public void testCanCreateResource() throws Exception {
149         assertTrue(ResourceHelper.canCreateResource("@layout/foo"));
150         assertTrue(ResourceHelper.canCreateResource("@string/foo"));
151         assertTrue(ResourceHelper.canCreateResource("@dimen/foo"));
152         assertTrue(ResourceHelper.canCreateResource("@color/foo"));
153 
154         assertFalse(ResourceHelper.canCreateResource("@typo/foo")); // nonexistent type
155         assertFalse(ResourceHelper.canCreateResource("@layout/foo bar")); // space
156         assertFalse(ResourceHelper.canCreateResource("@layout/new")); // keyword
157         assertFalse(ResourceHelper.canCreateResource("@android:string/foo")); // framework
158         assertFalse(ResourceHelper.canCreateResource("@android:dimen/foo"));
159         assertFalse(ResourceHelper.canCreateResource("@android:color/foo"));
160     }
161 
testCanCreateResourceType()162     public void testCanCreateResourceType() throws Exception {
163         assertTrue(ResourceHelper.canCreateResourceType(ResourceType.LAYOUT));
164         assertTrue(ResourceHelper.canCreateResourceType(ResourceType.STRING));
165         assertTrue(ResourceHelper.canCreateResourceType(ResourceType.DIMEN));
166         assertTrue(ResourceHelper.canCreateResourceType(ResourceType.COLOR));
167 
168         assertFalse(ResourceHelper.canCreateResourceType(ResourceType.RAW));
169         assertFalse(ResourceHelper.canCreateResourceType(ResourceType.XML));
170     }
171 
testStyleToTheme()172     public void testStyleToTheme() throws Exception {
173         assertEquals("Foo", ResourceHelper.styleToTheme("Foo"));
174         assertEquals("Theme", ResourceHelper.styleToTheme("@android:style/Theme"));
175         assertEquals("LocalTheme", ResourceHelper.styleToTheme("@style/LocalTheme"));
176         //assertEquals("LocalTheme", ResourceHelper.styleToTheme("@foo.bar:style/LocalTheme"));
177     }
178 
testIsProjectStyle()179     public void testIsProjectStyle() throws Exception {
180         assertFalse(ResourceHelper.isProjectStyle("@android:style/Theme"));
181         assertTrue(ResourceHelper.isProjectStyle("@style/LocalTheme"));
182     }
183 }
184