| 1 |
package com.android.ide.common.resources; |
| 2 |
|
| 3 |
import static com.android.SdkConstants.FD_RES; |
| 4 |
import static org.junit.Assert.assertTrue; |
| 5 |
import static org.junit.Assert.fail; |
| 6 |
|
| 7 |
import com.android.annotations.NonNull; |
| 8 |
import com.android.ide.common.res2.MergingException; |
| 9 |
import com.android.ide.common.res2.RecordingLogger; |
| 10 |
import com.android.ide.common.res2.ResourceMerger; |
| 11 |
import com.android.ide.common.res2.ResourceSet; |
| 12 |
import com.android.io.FolderWrapper; |
| 13 |
import com.android.io.IAbstractFolder; |
| 14 |
import com.android.testutils.TestUtils; |
| 15 |
import com.google.common.base.Charsets; |
| 16 |
import com.google.common.io.Files; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.io.IOException; |
| 20 |
|
| 21 |
public class TestResourceRepository extends ResourceRepository { |
| 22 |
private final File mDir; |
| 23 |
|
| 24 |
TestResourceRepository(@NonNull IAbstractFolder resFolder, boolean isFrameworkRepository, |
| 25 |
File dir) { |
| 26 |
super(resFolder, isFrameworkRepository); |
| 27 |
mDir = dir; |
| 28 |
} |
| 29 |
|
| 30 |
@NonNull |
| 31 |
@Override |
| 32 |
protected ResourceItem createResourceItem(@NonNull String name) { |
| 33 |
return new TestResourceItem(name); |
| 34 |
} |
| 35 |
|
| 36 |
public File getDir() { |
| 37 |
return mDir; |
| 38 |
} |
| 39 |
|
| 40 |
public void dispose() { |
| 41 |
deleteFile(mDir); |
| 42 |
} |
| 43 |
|
| 44 |
private static void deleteFile(File dir) { |
| 45 |
if (dir.isDirectory()) { |
| 46 |
File[] files = dir.listFiles(); |
| 47 |
if (files != null) { |
| 48 |
for (File f : files) { |
| 49 |
deleteFile(f); |
| 50 |
} |
| 51 |
} |
| 52 |
} else if (dir.isFile()) { |
| 53 |
assertTrue(dir.getPath(), dir.delete()); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Creates a resource repository for a resource folder whose contents is identified |
| 59 |
* by the pairs of relative paths and file contents |
| 60 |
*/ |
| 61 |
@SuppressWarnings("ResultOfMethodCallIgnored") |
| 62 |
@NonNull |
| 63 |
public static TestResourceRepository create(boolean isFramework, Object[] data) |
| 64 |
throws IOException { |
| 65 |
File dir = TestUtils.createTempDirDeletedOnExit(); |
| 66 |
File res = new File(dir, FD_RES); |
| 67 |
res.mkdirs(); |
| 68 |
|
| 69 |
assertTrue("Expected even number of items (path,contents)", data.length % 2 == 0); |
| 70 |
for (int i = 0; i < data.length; i += 2) { |
| 71 |
Object relativePathObject = data[i]; |
| 72 |
assertTrue(relativePathObject instanceof String); |
| 73 |
String relativePath = (String) relativePathObject; |
| 74 |
relativePath = relativePath.replace('/', File.separatorChar); |
| 75 |
File file = new File(res, relativePath); |
| 76 |
File parent = file.getParentFile(); |
| 77 |
parent.mkdirs(); |
| 78 |
|
| 79 |
Object fileContents = data[i + 1]; |
| 80 |
if (fileContents instanceof String) { |
| 81 |
String text = (String) fileContents; |
| 82 |
Files.write(text, file, Charsets.UTF_8); |
| 83 |
} else if (fileContents instanceof byte[]) { |
| 84 |
byte[] bytes = (byte[]) fileContents; |
| 85 |
Files.write(bytes, file); |
| 86 |
} else { |
| 87 |
fail("File contents must be Strings or byte[]'s"); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
IAbstractFolder resFolder = new FolderWrapper(dir, FD_RES); |
| 92 |
return new TestResourceRepository(resFolder, isFramework, dir); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Creates a res2 resource repository for a resource folder whose contents is identified |
| 97 |
* by the pairs of relative paths and file contents |
| 98 |
* |
| 99 |
* @see #create(boolean, Object[]) |
| 100 |
*/ |
| 101 |
@SuppressWarnings("ResultOfMethodCallIgnored") |
| 102 |
@NonNull |
| 103 |
public static com.android.ide.common.res2.ResourceRepository createRes2( |
| 104 |
boolean isFramework, Object[] data) |
| 105 |
throws IOException, MergingException { |
| 106 |
File dir = TestUtils.createTempDirDeletedOnExit(); |
| 107 |
File res = new File(dir, FD_RES); |
| 108 |
res.mkdirs(); |
| 109 |
|
| 110 |
assertTrue("Expected even number of items (path,contents)", data.length % 2 == 0); |
| 111 |
for (int i = 0; i < data.length; i += 2) { |
| 112 |
Object relativePathObject = data[i]; |
| 113 |
assertTrue(relativePathObject instanceof String); |
| 114 |
String relativePath = (String) relativePathObject; |
| 115 |
relativePath = relativePath.replace('/', File.separatorChar); |
| 116 |
File file = new File(res, relativePath); |
| 117 |
File parent = file.getParentFile(); |
| 118 |
parent.mkdirs(); |
| 119 |
|
| 120 |
Object fileContents = data[i + 1]; |
| 121 |
if (fileContents instanceof String) { |
| 122 |
String text = (String) fileContents; |
| 123 |
Files.write(text, file, Charsets.UTF_8); |
| 124 |
} else if (fileContents instanceof byte[]) { |
| 125 |
byte[] bytes = (byte[]) fileContents; |
| 126 |
Files.write(bytes, file); |
| 127 |
} else { |
| 128 |
fail("File contents must be Strings or byte[]'s"); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
File resFolder = new File(dir, FD_RES); |
| 133 |
|
| 134 |
ResourceMerger merger = new ResourceMerger(0); |
| 135 |
ResourceSet resourceSet = new ResourceSet("main", null); |
| 136 |
resourceSet.addSource(resFolder); |
| 137 |
resourceSet.loadFromFiles(new RecordingLogger()); |
| 138 |
merger.addDataSet(resourceSet); |
| 139 |
|
| 140 |
com.android.ide.common.res2.ResourceRepository repository; |
| 141 |
repository = new com.android.ide.common.res2.ResourceRepository(isFramework); |
| 142 |
merger.mergeData(repository.createMergeConsumer(), true /*doCleanUp*/); |
| 143 |
|
| 144 |
return repository; |
| 145 |
} |
| 146 |
|
| 147 |
private static class TestResourceItem extends ResourceItem { |
| 148 |
TestResourceItem(String name) { |
| 149 |
super(name); |
| 150 |
} |
| 151 |
} |
| 152 |
} |