OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / eclipse / plugins / com.android.ide.eclipse.tests / unittests / com / android / ide / eclipse / adt / internal / editors / resources / manager / ConfigMatchTest.java
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.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;
37
38 import java.lang.reflect.Field;
39 import java.lang.reflect.Method;
40
41 import junit.framework.TestCase;
42
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$
47
48     private ProjectResources mResources;
49     private ResourceQualifier[] mQualifierList;
50     private FolderConfiguration config4;
51     private FolderConfiguration config3;
52     private FolderConfiguration config2;
53     private FolderConfiguration config1;
54
55     @Override
56     protected void setUp() throws Exception {
57         super.setUp();
58
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);
65
66         // get the actual list.
67         mQualifierList = (ResourceQualifier[])qualifierListField.get(manager);
68
69         // create the project resources.
70         mResources = new ProjectResources(null /*project*/);
71
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),
78         };
79         FileMock[] invalidMemberList = new FileMock[] {
80                 new FileMock(MISC1_FILENAME),
81                 new FileMock(MISC2_FILENAME),
82         };
83
84         // add multiple ResourceFolder to the project resource.
85         FolderConfiguration defaultConfig = getConfiguration(
86                 null, // country code
87                 null, // network code
88                 null, // language
89                 null, // region
90                 null, // screen size
91                 null, // screen ratio
92                 null, // screen orientation
93                 null, // dpi
94                 null, // touch mode
95                 null, // keyboard state
96                 null, // text input
97                 null, // navigation
98                 null, // screen dimension
99                 null);// version
100
101         addFolder(mResources, defaultConfig, validMemberList);
102
103         config1 = getConfiguration(
104                 null, // country code
105                 null, // network code
106                 "en", // language
107                 null, // region
108                 null, // screen size
109                 null, // screen ratio
110                 null, // screen orientation
111                 null, // dpi
112                 null, // touch mode
113                 KeyboardState.EXPOSED.getResourceValue(), // keyboard state
114                 null, // text input
115                 null, // navigation
116                 null, // screen dimension
117                 null);// version
118
119         addFolder(mResources, config1, validMemberList);
120
121         config2 = getConfiguration(
122                 null, // country code
123                 null, // network code
124                 "en", // language
125                 null, // region
126                 null, // screen size
127                 null, // screen ratio
128                 null, // screen orientation
129                 null, // dpi
130                 null, // touch mode
131                 KeyboardState.HIDDEN.getResourceValue(), // keyboard state
132                 null, // text input
133                 null, // navigation
134                 null, // screen dimension
135                 null);// version
136
137         addFolder(mResources, config2, validMemberList);
138
139         config3 = getConfiguration(
140                 null, // country code
141                 null, // network code
142                 "en", // language
143                 null, // region
144                 null, // screen size
145                 null, // screen ratio
146                 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation
147                 null, // dpi
148                 null, // touch mode
149                 null, // keyboard state
150                 null, // text input
151                 null, // navigation
152                 null, // screen dimension
153                 null);// version
154
155         addFolder(mResources, config3, validMemberList);
156
157         config4 = getConfiguration(
158                 "mcc310", // country code
159                 "mnc435", // network code
160                 "en", // language
161                 "rUS", // region
162                 "normal", // screen size
163                 "notlong", // screen ratio
164                 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation
165                 "mdpi", // dpi
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
171                 "v3"); // version
172
173         addFolder(mResources, config4, invalidMemberList);
174     }
175
176     @Override
177     protected void tearDown() throws Exception {
178         super.tearDown();
179         mResources = null;
180     }
181
182     public void test1() {
183         FolderConfiguration testConfig = getConfiguration(
184                 "mcc310", // country code
185                 "mnc435", // network code
186                 "en", // language
187                 "rUS", // region
188                 "normal", // screen size
189                 "notlong", // screen ratio
190                 ScreenOrientation.LANDSCAPE.getResourceValue(), // screen orientation
191                 "mdpi", // dpi
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
197                 "v3"); // version
198
199         ResourceFile result = mResources.getMatchingFile(SEARCHED_FILENAME,
200                 ResourceFolderType.LAYOUT, testConfig);
201
202         boolean bresult = result.getFolder().getConfiguration().equals(config3);
203         assertEquals(bresult, true);
204     }
205
206     /**
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.
211      */
212     private FolderConfiguration getConfiguration(String... qualifierValues) {
213         FolderConfiguration config = new FolderConfiguration();
214
215         // those must be of the same length
216         assertEquals(qualifierValues.length, mQualifierList.length);
217
218         int index = 0;
219
220         for (ResourceQualifier qualifier : mQualifierList) {
221             String value = qualifierValues[index++];
222             if (value != null) {
223                 assertTrue(qualifier.checkAndSet(value, config));
224             }
225         }
226
227         return config;
228     }
229
230     /**
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.
236      */
237     private void addFolder(ProjectResources resources, FolderConfiguration config,
238             FileMock[] memberList) throws Exception {
239
240         // figure out the folder name based on the configuration
241         String folderName = config.getFolderName(ResourceFolderType.LAYOUT);
242
243         // create the folder mock
244         FolderMock folder = new FolderMock(folderName, memberList);
245
246         // add it to the resource, and get back a ResourceFolder object.
247         ResourceFolder resFolder = _addProjectResourceFolder(resources, config, folder);
248
249         // and fill it with files from the list.
250         for (FileMock file : memberList) {
251             resFolder.addFile(new SingleResourceFile(new IFileWrapper(file), resFolder));
252         }
253     }
254
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
259      */
260     private ResourceFolder _addProjectResourceFolder(ProjectResources resources,
261             FolderConfiguration config, FolderMock folder) throws Exception {
262
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));
269         return resFolder;
270     }
271 }