1 /* 2 * Copyright (C) 2008 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.editors.resources.manager; 18 19 import com.android.SdkConstants; 20 import com.android.ide.common.resources.ResourceFile; 21 import com.android.ide.common.resources.ResourceFolder; 22 import com.android.ide.common.resources.ResourceItem; 23 import com.android.ide.common.resources.ResourceRepository; 24 import com.android.ide.common.resources.SingleResourceFile; 25 import com.android.ide.common.resources.configuration.FolderConfiguration; 26 import com.android.ide.eclipse.adt.internal.resources.manager.ProjectResources; 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.Mocks; 30 import com.android.io.IAbstractFolder; 31 import com.android.io.IAbstractResource; 32 import com.android.resources.Keyboard; 33 import com.android.resources.KeyboardState; 34 import com.android.resources.Navigation; 35 import com.android.resources.NavigationState; 36 import com.android.resources.NightMode; 37 import com.android.resources.ResourceFolderType; 38 import com.android.resources.ScreenOrientation; 39 import com.android.resources.TouchScreen; 40 import com.android.resources.UiMode; 41 42 import org.eclipse.core.resources.IFile; 43 import org.eclipse.core.resources.IFolder; 44 45 import junit.framework.TestCase; 46 47 public class ConfigMatchTest extends TestCase { 48 private static final String SEARCHED_FILENAME = "main.xml"; //$NON-NLS-1$ 49 private static final String MISC1_FILENAME = "foo.xml"; //$NON-NLS-1$ 50 private static final String MISC2_FILENAME = "bar.xml"; //$NON-NLS-1$ 51 52 private FolderConfiguration mDefaultConfig; 53 private ResourceRepository mResources; 54 private FolderConfiguration config4; 55 private FolderConfiguration config3; 56 private FolderConfiguration config2; 57 private FolderConfiguration config1; 58 59 @Override 60 protected void setUp() throws Exception { 61 super.setUp(); 62 63 // create a default config with all qualifiers. 64 mDefaultConfig = new FolderConfiguration(); 65 mDefaultConfig.createDefault(); 66 67 IAbstractFolder folder = Mocks.createAbstractFolder( 68 SdkConstants.FD_RESOURCES, new IAbstractResource[0]); 69 70 // create the project resources. 71 mResources = new ResourceRepository(folder, false) { 72 @Override 73 protected ResourceItem createResourceItem(String name) { 74 return new ResourceItem(name); 75 } 76 }; 77 78 // create 2 arrays of IResource. one with the filename being looked up, and one without. 79 // Since the required API uses IResource, we can use MockFolder for them. 80 IFile[] validMemberList = new IFile[] { 81 Mocks.createFile(MISC1_FILENAME), 82 Mocks.createFile(SEARCHED_FILENAME), 83 Mocks.createFile(MISC2_FILENAME), 84 }; 85 IFile[] invalidMemberList = new IFile[] { 86 Mocks.createFile(MISC1_FILENAME), 87 Mocks.createFile(MISC2_FILENAME), 88 }; 89 90 // add multiple ResourceFolder to the project resource. 91 FolderConfiguration defaultConfig = getConfiguration( 92 null, // country code 93 null, // network code 94 null, // language 95 null, // region 96 null, // smallest width dp 97 null, // width dp 98 null, // height dp 99 null, // screen size 100 null, // screen ratio 101 null, // screen orientation 102 null, // dock mode 103 null, // night mode 104 null, // dpi 105 null, // touch mode 106 null, // keyboard state 107 null, // text input 108 null, // navigation state 109 null, // navigation method 110 null, // screen dimension 111 null);// version 112 113 addFolder(mResources, defaultConfig, validMemberList); 114 115 config1 = getConfiguration( 116 null, // country code 117 null, // network code 118 "en", // language 119 null, // region 120 null, // smallest width dp 121 null, // width dp 122 null, // height dp 123 null, // screen size 124 null, // screen ratio 125 null, // screen orientation 126 null, // dock mode 127 null, // night mode 128 null, // dpi 129 null, // touch mode 130 KeyboardState.EXPOSED.getResourceValue(), // keyboard state 131 null, // text input 132 null, // navigation state 133 null, // navigation method 134 null, // screen dimension 135 null);// version 136 137 addFolder(mResources, config1, validMemberList); 138 139 config2 = getConfiguration( 140 null, // country code 141 null, // network code 142 "en", // language 143 null, // region 144 null, // smallest width dp 145 null, // width dp 146 null, // height dp 147 null, // screen size 148 null, // screen ratio 149 null, // screen orientation 150 null, // dock mode 151 null, // night mode 152 null, // dpi 153 null, // touch mode 154 KeyboardState.HIDDEN.getResourceValue(), // keyboard state 155 null, // text input 156 null, // navigation state 157 null, // navigation method 158 null, // screen dimension 159 null);// version 160 161 addFolder(mResources, config2, validMemberList); 162 163 config3 = getConfiguration( 164 null, // country code 165 null, // network code 166 "en", // language 167 null, // region 168 null, // smallest width dp 169 null, // width dp 170 null, // height dp 171 null, // screen size 172 null, // screen ratio 173 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation 174 null, // dock mode 175 null, // night mode 176 null, // dpi 177 null, // touch mode 178 null, // keyboard state 179 null, // text input 180 null, // navigation state 181 null, // navigation method 182 null, // screen dimension 183 null);// version 184 185 addFolder(mResources, config3, validMemberList); 186 187 config4 = getConfiguration( 188 "mcc310", // country code 189 "mnc435", // network code 190 "en", // language 191 "rUS", // region 192 null, // smallest width dp 193 null, // width dp 194 null, // height dp 195 "normal", // screen size 196 "notlong", // screen ratio 197 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation 198 UiMode.DESK.getResourceValue(), // dock mode 199 NightMode.NIGHT.getResourceValue(), // night mode 200 "mdpi", // dpi 201 TouchScreen.FINGER.getResourceValue(), // touch mode 202 KeyboardState.EXPOSED.getResourceValue(), // keyboard state 203 Keyboard.QWERTY.getResourceValue(), // text input 204 NavigationState.EXPOSED.getResourceValue(), // navigation state 205 Navigation.DPAD.getResourceValue(), // navigation method 206 "480x320", // screen dimension 207 "v3"); // version 208 209 addFolder(mResources, config4, invalidMemberList); 210 } 211 212 @Override 213 protected void tearDown() throws Exception { 214 super.tearDown(); 215 mResources = null; 216 } 217 218 public void test1() { 219 FolderConfiguration testConfig = getConfiguration( 220 "mcc310", // country code 221 "mnc435", // network code 222 "en", // language 223 "rUS", // region 224 null, // smallest width dp 225 null, // width dp 226 null, // height dp 227 "normal", // screen size 228 "notlong", // screen ratio 229 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation 230 UiMode.DESK.getResourceValue(), // dock mode 231 NightMode.NIGHT.getResourceValue(), // night mode 232 "mdpi", // dpi 233 TouchScreen.FINGER.getResourceValue(), // touch mode 234 KeyboardState.EXPOSED.getResourceValue(), // keyboard state 235 Keyboard.QWERTY.getResourceValue(), // text input 236 NavigationState.EXPOSED.getResourceValue(), // navigation state 237 Navigation.DPAD.getResourceValue(), // navigation method 238 "480x320", // screen dimension 239 "v3"); // version 240 241 ResourceFile result = mResources.getMatchingFile(SEARCHED_FILENAME, 242 ResourceFolderType.LAYOUT, testConfig); 243 244 boolean bresult = result.getFolder().getConfiguration().equals(config3); 245 assertEquals(bresult, true); 246 } 247 248 /** 249 * Creates a {@link FolderConfiguration}. 250 * @param qualifierValues The list of qualifier values. The length must equals the total number 251 * of Qualifiers. <code>null</code> is permitted and will make the FolderConfiguration not use 252 * this particular qualifier. 253 */ 254 private FolderConfiguration getConfiguration(String... qualifierValues) { 255 // FolderConfiguration.getQualifierCount is always valid and up to date. 256 final int count = FolderConfiguration.getQualifierCount(); 257 258 // Check we have the right number of qualifier. 259 assertEquals(qualifierValues.length, count); 260 261 FolderConfiguration config = new FolderConfiguration(); 262 263 for (int i = 0 ; i < count ; i++) { 264 String value = qualifierValues[i]; 265 if (value != null) { 266 assertTrue(mDefaultConfig.getQualifier(i).checkAndSet(value, config)); 267 } 268 } 269 270 return config; 271 } 272 273 /** 274 * Adds a folder to the given {@link ProjectResources} with the given 275 * {@link FolderConfiguration}. The folder is filled with files from the provided list. 276 * @param resources the {@link ResourceRepository} in which to add the folder. 277 * @param config the {@link FolderConfiguration} for the created folder. 278 * @param memberList the list of files for the folder. 279 */ 280 private void addFolder(ResourceRepository resources, FolderConfiguration config, 281 IFile[] memberList) throws Exception { 282 283 // figure out the folder name based on the configuration 284 String folderName = config.getFolderName(ResourceFolderType.LAYOUT); 285 286 // create the folder mock 287 IFolder folder = Mocks.createFolder(folderName, memberList); 288 289 // add it to the resource, and get back a ResourceFolder object. 290 ResourceFolder resFolder = resources.processFolder(new IFolderWrapper(folder)); 291 292 // and fill it with files from the list. 293 for (IFile file : memberList) { 294 resFolder.addFile(new SingleResourceFile(new IFileWrapper(file), resFolder)); 295 } 296 } 297 } 298