• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.res;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 
5 import java.io.StringReader;
6 import javax.xml.stream.XMLInputFactory;
7 import javax.xml.stream.XMLStreamException;
8 import javax.xml.stream.XMLStreamReader;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.JUnit4;
13 import org.robolectric.res.android.ResTable_config;
14 
15 @RunWith(JUnit4.class)
16 public class StaxValueLoaderTest {
17 
18   private PackageResourceTable resourceTable;
19   private NodeHandler topLevelNodeHandler;
20   private StaxDocumentLoader staxDocumentLoader;
21 
22   @Before
setUp()23   public void setUp() throws Exception {
24     resourceTable = new PackageResourceTable("pkg");
25 
26     topLevelNodeHandler = new NodeHandler();
27     staxDocumentLoader = new StaxDocumentLoader("pkg", null, topLevelNodeHandler);
28   }
29 
30   @Test
ignoresXliffTags()31   public void ignoresXliffTags() throws Exception {
32     topLevelNodeHandler.addHandler("resources", new NodeHandler()
33         .addHandler("string", new StaxValueLoader(resourceTable, "string", ResType.CHAR_SEQUENCE))
34     );
35 
36     parse("<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">" +
37         "<string name=\"preposition_for_date\">on <xliff:g id=\"date\" example=\"May 29\">%s</xliff:g></string>" +
38         "</resources>");
39 
40     assertThat(resourceTable.getValue(new ResName("pkg:string/preposition_for_date"), new ResTable_config()).getData())
41         .isEqualTo("on %s");
42   }
43 
44   @Test
ignoresBTags()45   public void ignoresBTags() throws Exception {
46     topLevelNodeHandler.addHandler("resources", new NodeHandler()
47         .addHandler("item[@type='string']", new StaxValueLoader(resourceTable, "string", ResType.CHAR_SEQUENCE))
48     );
49 
50     parse("<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">" +
51         "<item type=\"string\" name=\"sms_short_code_details\">This <b>may cause charges</b> on your mobile account.</item>" +
52         "</resources>");
53 
54     assertThat(resourceTable.getValue(new ResName("pkg:string/sms_short_code_details"), new ResTable_config()).getData())
55         .isEqualTo("This may cause charges on your mobile account.");
56   }
57 
parse(String xml)58   private void parse(String xml) throws XMLStreamException {
59     XMLInputFactory factory = XMLInputFactory.newFactory();
60     XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(new StringReader(xml));
61     FsFile fsFile = Fs.fileFromPath("/tmp/fake.txt");
62     Qualifiers qualifiers = Qualifiers.fromParentDir(fsFile.getParent());
63     staxDocumentLoader.doParse(xmlStreamReader, new XmlContext("pkg",
64         fsFile, qualifiers));
65   }
66 }