Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/res2/MergeResourceWriterWithCompilerTest.java

raw
1 /*
2 * Copyright (C) 2016 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.ide.common.res2;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21
22 import com.android.annotations.NonNull;
23 import com.android.ide.common.resources.configuration.FolderConfiguration;
24 import com.android.resources.ResourceType;
25 import com.google.common.base.Charsets;
26 import com.google.common.io.Files;
27 import com.google.common.util.concurrent.Futures;
28
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.TemporaryFolder;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 import javax.xml.parsers.DocumentBuilderFactory;
41
42 public class MergeResourceWriterWithCompilerTest {
43
44 @Rule
45 public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
46
47 private File mRoot;
48
49 private Map<String, ResourceItem> mResourceItems;
50
51 private ResourcePreprocessor mEmptyPreprocessor;
52
53 private ResourceCompiler mSimpleCompiler;
54
55 @Before
56 public final void before() throws Exception {
57 mEmptyPreprocessor = new ResourcePreprocessor() {
58 @Override
59 public boolean needsPreprocessing(File file) {
60 return false;
61 }
62
63 @Override
64 public Collection<File> getFilesToBeGenerated(File original) {
65 return null;
66 }
67
68 @Override
69 public void generateFile(File toBeGenerated, File original)
70 throws IOException {
71 }
72 };
73
74 mSimpleCompiler = (@NonNull File file, @NonNull File output) -> {
75 File outputPath = new File(output, file.getName() + "-c");
76 Files.copy(file, outputPath);
77 return Futures.immediateFuture(outputPath);
78 };
79
80 createSourceResourcesFiles();
81 }
82
83 /**
84 * Creates the source resources to merge:
85 *
86 * <pre>
87 * raw
88 * + f1.txt ("foo")
89 * </pre>
90 * @throws Exception
91 */
92 private void createSourceResourcesFiles() throws Exception {
93 File resourceDir = mTemporaryFolder.newFolder();
94 File rawRes = new File(resourceDir, "raw");
95 rawRes.mkdir();
96 File f1 = new File(rawRes, "f1.txt");
97 Files.write("foo", f1, Charsets.US_ASCII);
98
99 ResourceItem f1Item = new ResourceItem("f1.txt", ResourceType.RAW, null, null);
100 ResourceFile f1File = new ResourceFile(f1, f1Item, "", new FolderConfiguration());
101 f1Item.setSource(f1File);
102
103 File f2 = new File(rawRes, "f2.xml");
104 Files.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", f2, Charsets.US_ASCII);
105
106 ResourceItem f2Item = new ResourceItem("f2.xml", ResourceType.RAW, null, null);
107 ResourceFile f2File = new ResourceFile(f2, f2Item, "", new FolderConfiguration());
108 f2Item.setSource(f2File);
109
110 mResourceItems = new HashMap<>();
111 mResourceItems.put("f1.txt", f1Item);
112 mResourceItems.put("f2.xml", f2Item);
113 }
114
115 @Test
116 public void addAndDeleteFileTxt() throws Exception {
117 addAndDeleteFile("f1.txt");
118 }
119
120 @Test
121 public void addAndDeleteFileXml() throws Exception {
122 addAndDeleteFile("f2.xml");
123 }
124
125 public void addAndDeleteFile(@NonNull String name) throws Exception {
126 mRoot = mTemporaryFolder.newFolder();
127 File tmpFolder = mTemporaryFolder.newFolder();
128
129 MergedResourceWriter writer =
130 new MergedResourceWriter(
131 mRoot,
132 null,
133 null,
134 mEmptyPreprocessor,
135 mSimpleCompiler,
136 tmpFolder);
137
138 /*
139 * Add the file.
140 */
141
142 writer.start(DocumentBuilderFactory.newInstance());
143 mResourceItems.get(name).setTouched();
144 writer.addItem(mResourceItems.get(name));
145 writer.postWriteAction();
146 writer.end();
147
148 File f1Compiled = new File(mRoot, name + "-c");
149 assertTrue(f1Compiled.exists());
150
151
152 /*
153 * Remove the file.
154 */
155 writer =
156 new MergedResourceWriter(
157 mRoot,
158 null,
159 null,
160 mEmptyPreprocessor,
161 mSimpleCompiler,
162 tmpFolder);
163
164 mResourceItems.get(name).setRemoved();
165 writer.start(DocumentBuilderFactory.newInstance());
166 writer.removeItem(mResourceItems.get(name), null);
167 writer.postWriteAction();
168 writer.end();
169
170 assertFalse(f1Compiled.exists());
171 }
172 }