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