1 /* 2 * Copyright (C) 2017 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.settings; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 import java.io.ByteArrayInputStream; 24 import java.io.IOException; 25 import java.io.InputStreamReader; 26 import java.io.PrintWriter; 27 import java.io.StringWriter; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.robolectric.annotation.Config; 32 import org.xmlpull.v1.XmlPullParserException; 33 34 @RunWith(SettingsRobolectricTestRunner.class) 35 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 36 public class LicenseHtmlGeneratorFromXmlTest { 37 private static final String VAILD_XML_STRING = 38 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 39 "<licenses>\n" + 40 "<file-name contentId=\"0\">/file0</file-name>\n" + 41 "<file-name contentId=\"0\">/file1</file-name>\n" + 42 "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n" + 43 "</licenses>"; 44 45 private static final String INVAILD_XML_STRING = 46 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 47 "<licenses2>\n" + 48 "<file-name contentId=\"0\">/file0</file-name>\n" + 49 "<file-name contentId=\"0\">/file1</file-name>\n" + 50 "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n" + 51 "</licenses2>"; 52 53 private static final String EXPECTED_HTML_STRING = 54 "<html><head>\n" + 55 "<style type=\"text/css\">\n" + 56 "body { padding: 0; font-family: sans-serif; }\n" + 57 ".same-license { background-color: #eeeeee;\n" + 58 " border-top: 20px solid white;\n" + 59 " padding: 10px; }\n" + 60 ".label { font-weight: bold; }\n" + 61 ".file-list { margin-left: 1em; color: blue; }\n" + 62 "</style>\n" + 63 "</head>" + 64 "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n" + 65 "<div class=\"toc\">\n" + 66 "<ul>\n" + 67 "<li><a href=\"#id0\">/file0</a></li>\n" + 68 "<li><a href=\"#id0\">/file1</a></li>\n" + 69 "</ul>\n" + 70 "</div><!-- table of contents -->\n" + 71 "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" + 72 "<tr id=\"id0\"><td class=\"same-license\">\n" + 73 "<div class=\"label\">Notices for file(s):</div>\n" + 74 "<div class=\"file-list\">\n" + 75 "/file0 <br/>\n" + 76 "/file1 <br/>\n" + 77 "</div><!-- file-list -->\n" + 78 "<pre class=\"license-text\">\n" + 79 "license content #0\n" + 80 "</pre><!-- license-text -->\n" + 81 "</td></tr><!-- same-license -->\n" + 82 "</table></body></html>\n"; 83 84 @Test testParseValidXmlStream()85 public void testParseValidXmlStream() throws XmlPullParserException, IOException { 86 Map<String, String> fileNameToContentIdMap = new HashMap<String, String>(); 87 Map<String, String> contentIdToFileContentMap = new HashMap<String, String>(); 88 89 LicenseHtmlGeneratorFromXml.parse( 90 new InputStreamReader(new ByteArrayInputStream(VAILD_XML_STRING.getBytes())), 91 fileNameToContentIdMap, contentIdToFileContentMap); 92 assertThat(fileNameToContentIdMap.size()).isEqualTo(2); 93 assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0"); 94 assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0"); 95 assertThat(contentIdToFileContentMap.size()).isEqualTo(1); 96 assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0"); 97 } 98 99 @Test(expected = XmlPullParserException.class) testParseInvalidXmlStream()100 public void testParseInvalidXmlStream() throws XmlPullParserException, IOException { 101 Map<String, String> fileNameToContentIdMap = new HashMap<String, String>(); 102 Map<String, String> contentIdToFileContentMap = new HashMap<String, String>(); 103 104 LicenseHtmlGeneratorFromXml.parse( 105 new InputStreamReader(new ByteArrayInputStream(INVAILD_XML_STRING.getBytes())), 106 fileNameToContentIdMap, contentIdToFileContentMap); 107 } 108 109 @Test testGenerateHtml()110 public void testGenerateHtml() { 111 Map<String, String> fileNameToContentIdMap = new HashMap<String, String>(); 112 Map<String, String> contentIdToFileContentMap = new HashMap<String, String>(); 113 114 fileNameToContentIdMap.put("/file0", "0"); 115 fileNameToContentIdMap.put("/file1", "0"); 116 contentIdToFileContentMap.put("0", "license content #0"); 117 118 StringWriter output = new StringWriter(); 119 LicenseHtmlGeneratorFromXml.generateHtml( 120 fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output)); 121 assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING); 122 } 123 } 124