Merge "Move codes generating html file from xml files to SettingsLib (1/2)"

This commit is contained in:
TreeHugger Robot
2017-12-08 00:32:38 +00:00
committed by Android (Google) Code Review
22 changed files with 22 additions and 770 deletions

View File

@@ -1,125 +0,0 @@
/*
* 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 com.android.settings.testutils.SettingsRobolectricTestRunner;
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);
}
}

View File

@@ -1,109 +0,0 @@
/*
* 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 static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import java.io.File;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class LicenseHtmlLoaderTest {
@Mock
private Context mContext;
LicenseHtmlLoader newLicenseHtmlLoader(ArrayList<File> xmlFiles,
File cachedHtmlFile, boolean isCachedHtmlFileOutdated,
boolean generateHtmlFileSucceeded) {
LicenseHtmlLoader loader = spy(new LicenseHtmlLoader(mContext));
doReturn(xmlFiles).when(loader).getVaildXmlFiles();
doReturn(cachedHtmlFile).when(loader).getCachedHtmlFile();
doReturn(isCachedHtmlFileOutdated).when(loader).isCachedHtmlFileOutdated(any(), any());
doReturn(generateHtmlFileSucceeded).when(loader).generateHtmlFile(any(), any());
return loader;
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testLoadInBackground() {
ArrayList<File> xmlFiles = new ArrayList();
xmlFiles.add(new File("test.xml"));
File cachedHtmlFile = new File("test.html");
LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
verify(loader).generateHtmlFile(any(), any());
}
@Test
public void testLoadInBackgroundWithNoVaildXmlFiles() {
ArrayList<File> xmlFiles = new ArrayList();
File cachedHtmlFile = new File("test.html");
LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
assertThat(loader.loadInBackground()).isNull();
verify(loader, never()).generateHtmlFile(any(), any());
}
@Test
public void testLoadInBackgroundWithNonOutdatedCachedHtmlFile() {
ArrayList<File> xmlFiles = new ArrayList();
xmlFiles.add(new File("test.xml"));
File cachedHtmlFile = new File("test.html");
LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, false, true);
assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
verify(loader, never()).generateHtmlFile(any(), any());
}
@Test
public void testLoadInBackgroundWithGenerateHtmlFileFailed() {
ArrayList<File> xmlFiles = new ArrayList();
xmlFiles.add(new File("test.xml"));
File cachedHtmlFile = new File("test.html");
LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, false);
assertThat(loader.loadInBackground()).isNull();
verify(loader).generateHtmlFile(any(), any());
}
}

View File

@@ -18,21 +18,19 @@ package com.android.settings;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.robolectric.Shadows.shadowOf;
import android.app.Application;
import android.os.SystemProperties;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemProperties;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import java.io.File;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -42,6 +40,8 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import java.io.File;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SettingsLicenseActivityTest {