Files
app_Settings/tests/robotests/src/com/android/settings/LicenseHtmlGeneratorFromXmlTest.java
Jaekyun Seok fa0242c2e3 Generate license html file from xml files of partitions
Treble-ization requires each partner to store their license information
into their own partition because each partition can be updated
individually.
So each partition will have its own NOTICE.xml.gz, and Settings should
be able to generate license html from xml files of partitions.

Test: building succeeded and tested on sailfish.
  make ROBOTEST_FILTER=LicenseHtmlGeneratorFromXmlTest RunSettingsRoboTests
  make ROBOTEST_FILTER=LicenseHtmlLoaderTest RunSettingsRoboTests
  make ROBOTEST_FILTER=SettingsLicenseActivityTest RunSettingsRoboTests
Bug: 37099941
Merged-In: If797759d300ee20dd43ad8efd7d17b4f7e0c4537
Change-Id: If797759d300ee20dd43ad8efd7d17b4f7e0c4537
(cherry picked from commit 74812873f4)
2017-05-01 22:10:06 +00:00

124 lines
5.4 KiB
Java

/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import static com.google.common.truth.Truth.assertThat;
import java.util.HashMap;
import java.util.Map;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.xmlpull.v1.XmlPullParserException;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class LicenseHtmlGeneratorFromXmlTest {
private static final String VAILD_XML_STRING =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<licenses>\n" +
"<file-name contentId=\"0\">/file0</file-name>\n" +
"<file-name contentId=\"0\">/file1</file-name>\n" +
"<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n" +
"</licenses>";
private static final String INVAILD_XML_STRING =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<licenses2>\n" +
"<file-name contentId=\"0\">/file0</file-name>\n" +
"<file-name contentId=\"0\">/file1</file-name>\n" +
"<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n" +
"</licenses2>";
private static final String EXPECTED_HTML_STRING =
"<html><head>\n" +
"<style type=\"text/css\">\n" +
"body { padding: 0; font-family: sans-serif; }\n" +
".same-license { background-color: #eeeeee;\n" +
" border-top: 20px solid white;\n" +
" padding: 10px; }\n" +
".label { font-weight: bold; }\n" +
".file-list { margin-left: 1em; color: blue; }\n" +
"</style>\n" +
"</head>" +
"<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n" +
"<div class=\"toc\">\n" +
"<ul>\n" +
"<li><a href=\"#id0\">/file0</a></li>\n" +
"<li><a href=\"#id0\">/file1</a></li>\n" +
"</ul>\n" +
"</div><!-- table of contents -->\n" +
"<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" +
"<tr id=\"id0\"><td class=\"same-license\">\n" +
"<div class=\"label\">Notices for file(s):</div>\n" +
"<div class=\"file-list\">\n" +
"/file0 <br/>\n" +
"/file1 <br/>\n" +
"</div><!-- file-list -->\n" +
"<pre class=\"license-text\">\n" +
"license content #0\n" +
"</pre><!-- license-text -->\n" +
"</td></tr><!-- same-license -->\n" +
"</table></body></html>\n";
@Test
public void testParseValidXmlStream() throws XmlPullParserException, IOException {
Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
LicenseHtmlGeneratorFromXml.parse(
new InputStreamReader(new ByteArrayInputStream(VAILD_XML_STRING.getBytes())),
fileNameToContentIdMap, contentIdToFileContentMap);
assertThat(fileNameToContentIdMap.size()).isEqualTo(2);
assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0");
assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0");
assertThat(contentIdToFileContentMap.size()).isEqualTo(1);
assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0");
}
@Test(expected = XmlPullParserException.class)
public void testParseInvalidXmlStream() throws XmlPullParserException, IOException {
Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
LicenseHtmlGeneratorFromXml.parse(
new InputStreamReader(new ByteArrayInputStream(INVAILD_XML_STRING.getBytes())),
fileNameToContentIdMap, contentIdToFileContentMap);
}
@Test
public void testGenerateHtml() {
Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();
fileNameToContentIdMap.put("/file0", "0");
fileNameToContentIdMap.put("/file1", "0");
contentIdToFileContentMap.put("0", "license content #0");
StringWriter output = new StringWriter();
LicenseHtmlGeneratorFromXml.generateHtml(
fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output));
assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING);
}
}