Search in sources :

Example 1 with ThemePreviewBuilder

use of com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder in project android by JetBrains.

the class ThemePreviewBuilderTest method testSearchFilter.

public void testSearchFilter() throws ParserConfigurationException, XPathExpressionException {
    List<ThemePreviewBuilder.ComponentDefinition> componentDefinitionList = ImmutableList.of(new ThemePreviewBuilder.ComponentDefinition("Spinner", ThemePreviewBuilder.ComponentGroup.TEXT, "Spinner"), new ThemePreviewBuilder.ComponentDefinition("Custom_Component", ThemePreviewBuilder.ComponentGroup.CUSTOM, "CustomComponent").addAlias("Spinner").addAlias("ABC").addAlias("DEF"));
    XPath xPath = XPathFactory.newInstance().newXPath();
    ThemePreviewBuilder customComponentBuilder = new ThemePreviewBuilder().addAllComponents(componentDefinitionList);
    // Check the search "spinner" returns both the actual spinner control and the custom component with the alias
    Document document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("SPINNER")).build();
    NodeList nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(2, nodeList.getLength());
    // Test matching the name
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("CustomCOMPONENT")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test matching the description
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("Custom_COMPONENT")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test searching for an alias that only matches our custom component
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("AbC")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test partial match
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("EF")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test case sensitive search
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("AbC", true)).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(0, nodeList.getLength());
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 2 with ThemePreviewBuilder

use of com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder in project android by JetBrains.

the class ThemePreviewBuilderTest method testBasicComponentList.

public void testBasicComponentList() throws ParserConfigurationException, XPathExpressionException {
    Document document = new ThemePreviewBuilder().addAllComponents(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS).build();
    XPath xPath = XPathFactory.newInstance().newXPath();
    assertEquals("One root layout expected", 1, document.getChildNodes().getLength());
    Node rootNode = document.getChildNodes().item(0);
    assertEquals(SdkConstants.LINEAR_LAYOUT, rootNode.getNodeName());
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_WIDTH));
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_HEIGHT));
    // Find if any elements doesn't have layout_with or layout_height
    NodeList nodeList = (NodeList) xPath.evaluate("//*[not(@*[local-name() = 'layout_width'])]", document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals("All components must have 'layout_with'", 0, nodeList.getLength());
    nodeList = (NodeList) xPath.evaluate("//*[not(@*[local-name() = 'layout_height'])]", document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals("All components must have 'layout_height'", 0, nodeList.getLength());
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 3 with ThemePreviewBuilder

use of com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder in project android by JetBrains.

the class ThemePreviewBuilderTest method testGroups.

public void testGroups() throws ParserConfigurationException, XPathExpressionException {
    Document document = new ThemePreviewBuilder().addAllComponents(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS).build();
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(GROUP_LABELS_XPATH + "@*[local-name() = 'text']", document.getDocumentElement(), XPathConstants.NODESET);
    HashSet<String> headerTitles = new HashSet<>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        String title = nodeList.item(i).getNodeValue();
        if (headerTitles.contains(title)) {
            fail(String.format("Header '%s' exists more than once", title));
        }
        headerTitles.add(title);
    }
    assertFalse(headerTitles.isEmpty());
    assertFalse("'Custom' header shouldn't be present when there are no custom components", headerTitles.contains(ThemePreviewBuilder.ComponentGroup.CUSTOM.name()));
    // Check the group id attribute. This attribute is needed to handle clicks on the preview
    // Check all components have a group id.
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        assertFalse(node.getAttributes().getNamedItemNS(BUILDER_URI, BUILDER_ATTR_GROUP).getNodeValue().isEmpty());
    }
    // Add custom component
    document = new ThemePreviewBuilder().addComponent(new ThemePreviewBuilder.ComponentDefinition("Test", ThemePreviewBuilder.ComponentGroup.CUSTOM, "Test")).build();
    nodeList = (NodeList) xPath.evaluate(GROUP_LABELS_XPATH + "@*[local-name() = 'text']", document.getDocumentElement(), XPathConstants.NODESET);
    headerTitles.clear();
    for (int i = 0; i < nodeList.getLength(); i++) {
        headerTitles.add(nodeList.item(i).getNodeValue());
    }
    assertFalse(headerTitles.isEmpty());
    assertTrue("'Custom' header should be present", headerTitles.contains(ThemePreviewBuilder.ComponentGroup.CUSTOM.name));
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        assertFalse(node.getAttributes().getNamedItemNS(BUILDER_URI, BUILDER_ATTR_GROUP).getNodeValue().isEmpty());
    }
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) HashSet(java.util.HashSet)

Example 4 with ThemePreviewBuilder

use of com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder in project android by JetBrains.

the class ThemePreviewBuilderTest method testEmptyPreview.

public void testEmptyPreview() throws ParserConfigurationException, XPathExpressionException {
    Document document = new ThemePreviewBuilder().build();
    XPath xPath = XPathFactory.newInstance().newXPath();
    assertEquals("One root layout expected", 1, document.getChildNodes().getLength());
    Node rootNode = document.getChildNodes().item(0);
    assertEquals(SdkConstants.LINEAR_LAYOUT, rootNode.getNodeName());
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_WIDTH));
    assertNotNull(rootNode.getAttributes().getNamedItemNS(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_HEIGHT));
    NodeList nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(0, nodeList.getLength());
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 5 with ThemePreviewBuilder

use of com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder in project android by JetBrains.

the class ThemePreviewBuilderTest method testComponentList.

public void testComponentList() throws ParserConfigurationException, XPathExpressionException {
    Document document = new ThemePreviewBuilder().addAllComponents(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS).build();
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS.size(), nodeList.getLength());
    ThemePreviewBuilder customComponentBuilder = new ThemePreviewBuilder().addAllComponents(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS).addComponent(new ThemePreviewBuilder.ComponentDefinition("Test", ThemePreviewBuilder.ComponentGroup.CUSTOM, "Test").setApiLevel(15));
    document = customComponentBuilder.build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS.size() + 1, nodeList.getLength());
    // This shouldn't filter our custom component
    Predicate<ThemePreviewBuilder.ComponentDefinition> api15Filter = new Predicate<ThemePreviewBuilder.ComponentDefinition>() {

        @Override
        public boolean apply(ThemePreviewBuilder.ComponentDefinition input) {
            return input.apiLevel <= 15;
        }
    };
    document = customComponentBuilder.addComponentFilter(api15Filter).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    Iterable filteredBaseComponents = Iterables.filter(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS, new Predicate<ThemePreviewBuilder.ComponentDefinition>() {

        @Override
        public boolean apply(ThemePreviewBuilder.ComponentDefinition input) {
            return input.apiLevel <= 15;
        }
    });
    assertEquals(Iterables.size(filteredBaseComponents) + 1, nodeList.getLength());
    // This should filter the custom component
    Predicate<ThemePreviewBuilder.ComponentDefinition> api14Filter = new Predicate<ThemePreviewBuilder.ComponentDefinition>() {

        @Override
        public boolean apply(ThemePreviewBuilder.ComponentDefinition input) {
            return input.apiLevel <= 14;
        }
    };
    document = customComponentBuilder.addComponentFilter(api14Filter).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    filteredBaseComponents = Iterables.filter(ThemePreviewBuilder.AVAILABLE_BASE_COMPONENTS, new Predicate<ThemePreviewBuilder.ComponentDefinition>() {

        @Override
        public boolean apply(ThemePreviewBuilder.ComponentDefinition input) {
            return input.apiLevel <= 14;
        }
    });
    assertEquals(Iterables.size(filteredBaseComponents), nodeList.getLength());
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Predicate(com.google.common.base.Predicate)

Aggregations

ThemePreviewBuilder (com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder)5 XPath (javax.xml.xpath.XPath)5 Document (org.w3c.dom.Document)5 NodeList (org.w3c.dom.NodeList)5 Node (org.w3c.dom.Node)3 Predicate (com.google.common.base.Predicate)1 HashSet (java.util.HashSet)1