package com.wayne.www.waynelib

import com.wayne.www.waynelib.entity.AllowAction
import com.wayne.www.waynelib.entity.AllowActions
import com.wayne.www.waynelib.entity.GenericDisplayCommand
import com.wayne.www.waynelib.entity.LinearLayout
import com.wayne.www.waynelib.util.GenericDisplayCmdParser
import org.junit.Assert
import org.junit.Test
import java.io.BufferedReader
import java.util.stream.Collectors


class GenericDisplayCmdParserTest {
    @Test
    fun addition_isCorrect() {
        Assert.assertEquals(4, 2 + 2)
    }

    @Test
    fun shell_parser_plain_text_test_v1() {
        var xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n" +
                "<GenericDisplayCommand xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
                "               <Version>1</Version>\n" +
                "<LinearLayout>\n" +
                "                                                                           <Id>QCD343</Id>\n" +
                "                                                                           <Height>match_parent</Height>\n" +
                "                                                                           <Width>match_parent</Width>\n" +
                "                                                                           <AllowActions>\n" +
                "                                                                                          <AllowAction>Select</AllowAction>\n" +
                "                                                                                          <AllowAction>Drag</AllowAction>\n" +
                "                                                                                          <AllowAction>Drop</AllowAction>\n" +
                "                                                                           </AllowActions>\n" +
                "                                                                           <TextView>\n" +
                "                                                                                          <Background>White</Background>\n" +
                "                                                                                          <AllowActions>\n" +
                "                                                                                                         <AllowAction>Select</AllowAction>\n" +
                "                                                                                                         <AllowAction>Drag</AllowAction>\n" +
                "                                                                                          </AllowActions>\n" +
                "                                                                                          <Text>QCD343</Text>\n" +
                "                                                                           </TextView>\n" +
                "                                                                           <TextView>\n" +
                "                                                                                          <Background>Blue</Background>\n" +
                "                                                                                          <PaddingLeft>2dp</PaddingLeft>\n" +
                "                                                                                          <Text>车牌付加油中</Text>\n" +
                "                                                                           </TextView>\n" +
                "                                                                           <WeightSum>0</WeightSum>\n" +
                "                                                                           <Weight>0</Weight>\n" +
                "                                                                           <Orientation>horizontal</Orientation>\n" +
                "                                                            </LinearLayout>\n" +
                "</GenericDisplayCommand>"
        var result = GenericDisplayCmdParser.fromXml(xml)
        Assert.assertNotNull(result)
    }

    @Test
    fun shell_parser_to_xml_v1() {
        var genericDisplayCommand = GenericDisplayCommand("1", LinearLayout("", null,
                listOf(LinearLayout("12345", AllowActions(listOf(AllowAction("Submit")))),
                LinearLayout("12345", AllowActions(listOf(AllowAction("Submit")))))))
        var actualXml = GenericDisplayCmdParser.toXml(genericDisplayCommand);
        var expectXml = "<GenericDisplayCommand>\n" +
                "  <Version>1</Version>\n" +
                "  <LinearLayout>\n" +
                "    <Id></Id>\n" +
                "    <LinearLayout>\n" +
                "      <Id>12345</Id>\n" +
                "      <AllowActions>\n" +
                "        <AllowAction>Submit</AllowAction>\n" +
                "      </AllowActions>\n" +
                "      <Height></Height>\n" +
                "      <Width></Width>\n" +
                "      <WeightSum></WeightSum>\n" +
                "      <Weight></Weight>\n" +
                "      <Orientation></Orientation>\n" +
                "    </LinearLayout>\n" +
                "    <LinearLayout>\n" +
                "      <Id>12345</Id>\n" +
                "      <AllowActions>\n" +
                "        <AllowAction>Submit</AllowAction>\n" +
                "      </AllowActions>\n" +
                "      <Height></Height>\n" +
                "      <Width></Width>\n" +
                "      <WeightSum></WeightSum>\n" +
                "      <Weight></Weight>\n" +
                "      <Orientation></Orientation>\n" +
                "    </LinearLayout>\n" +
                "    <Height></Height>\n" +
                "    <Width></Width>\n" +
                "    <WeightSum></WeightSum>\n" +
                "    <Weight></Weight>\n" +
                "    <Orientation></Orientation>\n" +
                "  </LinearLayout>\n" +
                "</GenericDisplayCommand>"
        Assert.assertEquals(actualXml, expectXml)
    }

    @Test
    fun shell_file_parser_test_v1() {
        val stream = this.javaClass.classLoader.getResourceAsStream("GenericDisplayCommandV1.xml")
        var str = BufferedReader(stream.reader())
                .lines().collect(Collectors.joining(System.lineSeparator()))
        var result = GenericDisplayCmdParser.fromXml(str)
        Assert.assertNotNull(result)
    }

    @Test
    fun shell_file_parser_test_v2() {
        val stream = this.javaClass.classLoader.getResourceAsStream("GenericDisplayCommandV2.xml")
        var str = BufferedReader(stream.reader())
                .lines().collect(Collectors.joining(System.lineSeparator()))
        var result = GenericDisplayCmdParser.fromXml(str)
        Assert.assertNotNull(result)
    }

    @Test
    fun shell_parser_to_xml_prompt() {
        var genericDisplayCommand = GenericDisplayCommand("1", null, "prompt")
        var actualXml = GenericDisplayCmdParser.toXml(genericDisplayCommand);
        var expectXml = "<GenericDisplayCommand>\n" +
                "  <Version>1</Version>\n" +
                "  <Prompt>prompt</Prompt>\n" +
                "</GenericDisplayCommand>"
        Assert.assertEquals(actualXml, expectXml)
    }
}