2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.eclipse.org/org/documents/epl-v10.php
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.
17 package com.android.ide.eclipse.adt.internal.editors.resources.manager;
19 import com.android.ide.eclipse.adt.internal.resources.configurations.FolderConfiguration;
20 import com.android.ide.eclipse.adt.internal.resources.configurations.ResourceQualifier;
21 import com.android.ide.eclipse.adt.internal.resources.manager.ProjectResources;
22 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceFile;
23 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceFolder;
24 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceFolderType;
25 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceManager;
26 import com.android.ide.eclipse.adt.internal.resources.manager.SingleResourceFile;
27 import com.android.ide.eclipse.adt.io.IFileWrapper;
28 import com.android.ide.eclipse.adt.io.IFolderWrapper;
29 import com.android.ide.eclipse.mock.FileMock;
30 import com.android.ide.eclipse.mock.FolderMock;
31 import com.android.sdklib.io.IAbstractFolder;
32 import com.android.sdklib.resources.Keyboard;
33 import com.android.sdklib.resources.KeyboardState;
34 import com.android.sdklib.resources.Navigation;
35 import com.android.sdklib.resources.ScreenOrientation;
36 import com.android.sdklib.resources.TouchScreen;
38 import java.lang.reflect.Field;
39 import java.lang.reflect.Method;
41 import junit.framework.TestCase;
43 public class ConfigMatchTest extends TestCase {
44 private static final String SEARCHED_FILENAME = "main.xml"; //$NON-NLS-1$
45 private static final String MISC1_FILENAME = "foo.xml"; //$NON-NLS-1$
46 private static final String MISC2_FILENAME = "bar.xml"; //$NON-NLS-1$
48 private ProjectResources mResources;
49 private ResourceQualifier[] mQualifierList;
50 private FolderConfiguration config4;
51 private FolderConfiguration config3;
52 private FolderConfiguration config2;
53 private FolderConfiguration config1;
56 protected void setUp() throws Exception {
59 // create a Resource Manager to get a list of qualifier as instantiated by the real code.
60 // Thanks for QualifierListTest we know this contains all the qualifiers.
61 ResourceManager manager = ResourceManager.getInstance();
62 Field qualifierListField = ResourceManager.class.getDeclaredField("mQualifiers");
63 assertNotNull(qualifierListField);
64 qualifierListField.setAccessible(true);
66 // get the actual list.
67 mQualifierList = (ResourceQualifier[])qualifierListField.get(manager);
69 // create the project resources.
70 mResources = new ProjectResources(null /*project*/);
72 // create 2 arrays of IResource. one with the filename being looked up, and one without.
73 // Since the required API uses IResource, we can use MockFolder for them.
74 FileMock[] validMemberList = new FileMock[] {
75 new FileMock(MISC1_FILENAME),
76 new FileMock(SEARCHED_FILENAME),
77 new FileMock(MISC2_FILENAME),
79 FileMock[] invalidMemberList = new FileMock[] {
80 new FileMock(MISC1_FILENAME),
81 new FileMock(MISC2_FILENAME),
84 // add multiple ResourceFolder to the project resource.
85 FolderConfiguration defaultConfig = getConfiguration(
92 null, // screen orientation
95 null, // keyboard state
98 null, // screen dimension
101 addFolder(mResources, defaultConfig, validMemberList);
103 config1 = getConfiguration(
104 null, // country code
105 null, // network code
109 null, // screen ratio
110 null, // screen orientation
113 KeyboardState.EXPOSED.getResourceValue(), // keyboard state
116 null, // screen dimension
119 addFolder(mResources, config1, validMemberList);
121 config2 = getConfiguration(
122 null, // country code
123 null, // network code
127 null, // screen ratio
128 null, // screen orientation
131 KeyboardState.HIDDEN.getResourceValue(), // keyboard state
134 null, // screen dimension
137 addFolder(mResources, config2, validMemberList);
139 config3 = getConfiguration(
140 null, // country code
141 null, // network code
145 null, // screen ratio
146 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation
149 null, // keyboard state
152 null, // screen dimension
155 addFolder(mResources, config3, validMemberList);
157 config4 = getConfiguration(
158 "mcc310", // country code
159 "mnc435", // network code
162 "normal", // screen size
163 "notlong", // screen ratio
164 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation
166 TouchScreen.FINGER.getResourceValue(), // touch mode
167 KeyboardState.EXPOSED.getResourceValue(), // keyboard state
168 Keyboard.QWERTY.getResourceValue(), // text input
169 Navigation.DPAD.getResourceValue(), // navigation
170 "480x320", // screen dimension
173 addFolder(mResources, config4, invalidMemberList);
177 protected void tearDown() throws Exception {
182 public void test1() {
183 FolderConfiguration testConfig = getConfiguration(
184 "mcc310", // country code
185 "mnc435", // network code
188 "normal", // screen size
189 "notlong", // screen ratio
190 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation
192 TouchScreen.FINGER.getResourceValue(), // touch mode
193 KeyboardState.EXPOSED.getResourceValue(), // keyboard state
194 Keyboard.QWERTY.getResourceValue(), // text input
195 Navigation.DPAD.getResourceValue(), // navigation
196 "480x320", // screen dimension
199 ResourceFile result = mResources.getMatchingFile(SEARCHED_FILENAME,
200 ResourceFolderType.LAYOUT, testConfig);
202 boolean bresult = result.getFolder().getConfiguration().equals(config3);
203 assertEquals(bresult, true);
207 * Creates a {@link FolderConfiguration}.
208 * @param qualifierValues The list of qualifier values. The length must equals the total number
209 * of Qualifiers. <code>null</code> is permitted and will make the FolderConfiguration not use
210 * this particular qualifier.
212 private FolderConfiguration getConfiguration(String... qualifierValues) {
213 FolderConfiguration config = new FolderConfiguration();
215 // those must be of the same length
216 assertEquals(qualifierValues.length, mQualifierList.length);
220 for (ResourceQualifier qualifier : mQualifierList) {
221 String value = qualifierValues[index++];
223 assertTrue(qualifier.checkAndSet(value, config));
231 * Adds a folder to the given {@link ProjectResources} with the given
232 * {@link FolderConfiguration}. The folder is filled with files from the provided list.
233 * @param resources the {@link ProjectResources} in which to add the folder.
234 * @param config the {@link FolderConfiguration} for the created folder.
235 * @param memberList the list of files for the folder.
237 private void addFolder(ProjectResources resources, FolderConfiguration config,
238 FileMock[] memberList) throws Exception {
240 // figure out the folder name based on the configuration
241 String folderName = config.getFolderName(ResourceFolderType.LAYOUT);
243 // create the folder mock
244 FolderMock folder = new FolderMock(folderName, memberList);
246 // add it to the resource, and get back a ResourceFolder object.
247 ResourceFolder resFolder = _addProjectResourceFolder(resources, config, folder);
249 // and fill it with files from the list.
250 for (FileMock file : memberList) {
251 resFolder.addFile(new SingleResourceFile(new IFileWrapper(file), resFolder));
255 /** Calls ProjectResource.add method via reflection to circumvent access
256 * restrictions that are enforced when running in the plug-in environment
257 * ie cannot access package or protected members in a different plug-in, even
258 * if they are in the same declared package as the accessor
260 private ResourceFolder _addProjectResourceFolder(ProjectResources resources,
261 FolderConfiguration config, FolderMock folder) throws Exception {
263 Method addMethod = ProjectResources.class.getDeclaredMethod("add",
264 ResourceFolderType.class, FolderConfiguration.class,
265 IAbstractFolder.class);
266 addMethod.setAccessible(true);
267 ResourceFolder resFolder = (ResourceFolder)addMethod.invoke(resources,
268 ResourceFolderType.LAYOUT, config, new IFolderWrapper(folder));