Merge "Generate license html file from xml files of partitions" into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
7f59081421
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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 static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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.anyInt;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.Application;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemProperties;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.Loader;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.res.builder.RobolectricPackageManager;
|
||||
import org.robolectric.util.ActivityController;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class SettingsLicenseActivityTest {
|
||||
private ActivityController<SettingsLicenseActivity> mActivityController;
|
||||
private SettingsLicenseActivity mActivity;
|
||||
private Application mApplication;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mApplication = RuntimeEnvironment.application;
|
||||
mActivityController = Robolectric.buildActivity(SettingsLicenseActivity.class);
|
||||
mActivity = spy(mActivityController.get());
|
||||
}
|
||||
|
||||
void assertEqualIntents(Intent actual, Intent expected) {
|
||||
assertThat(actual.getAction()).isEqualTo(expected.getAction());
|
||||
assertThat(actual.getDataString()).isEqualTo(expected.getDataString());
|
||||
assertThat(actual.getType()).isEqualTo(expected.getType());
|
||||
assertThat(actual.getCategories()).isEqualTo(expected.getCategories());
|
||||
assertThat(actual.getPackage()).isEqualTo(expected.getPackage());
|
||||
assertThat(actual.getFlags()).isEqualTo(expected.getFlags());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnCreateWithValidHtmlFile() {
|
||||
SystemProperties.set("ro.config.license_path", "/system/etc/NOTICE.html.gz");
|
||||
|
||||
doReturn(true).when(mActivity).isFileValid(any());
|
||||
mActivity.onCreate(null);
|
||||
|
||||
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(Uri.parse("file:///system/etc/NOTICE.html.gz"), "text/html");
|
||||
intent.putExtra(Intent.EXTRA_TITLE, mActivity.getString(
|
||||
R.string.settings_license_activity_title));
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
intent.setPackage("com.android.htmlviewer");
|
||||
|
||||
assertEqualIntents(shadowOf(mApplication).getNextStartedActivity(), intent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnCreateWithGeneratedHtmlFile() {
|
||||
doReturn(null).when(mActivity).onCreateLoader(anyInt(), any());
|
||||
doReturn(Uri.parse("content://com.android.settings.files/my_cache/generated_test.html"))
|
||||
.when(mActivity).getUriFromGeneratedHtmlFile(any());
|
||||
|
||||
mActivity.onCreate(null);
|
||||
mActivity.onLoadFinished(null, new File("/generated_test.html"));
|
||||
|
||||
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(
|
||||
Uri.parse("content://com.android.settings.files/my_cache/generated_test.html"),
|
||||
"text/html");
|
||||
intent.putExtra(Intent.EXTRA_TITLE, mActivity.getString(
|
||||
R.string.settings_license_activity_title));
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
intent.setPackage("com.android.htmlviewer");
|
||||
|
||||
assertEqualIntents(shadowOf(mApplication).getNextStartedActivity(), intent);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user