Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d13067e0 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Refactor LicenseHtmlLoader to share data w/ Compat verison."

parents 9e795d82 9de8aeb6
Loading
Loading
Loading
Loading
+6 −47
Original line number Diff line number Diff line
@@ -16,14 +16,17 @@

package com.android.settingslib.license;

import static com.android.settingslib.license.LicenseHtmlLoaderCompat.generateHtmlFile;
import static com.android.settingslib.license.LicenseHtmlLoaderCompat.getCachedHtmlFile;
import static com.android.settingslib.license.LicenseHtmlLoaderCompat.getVaildXmlFiles;
import static com.android.settingslib.license.LicenseHtmlLoaderCompat.isCachedHtmlFileOutdated;

import android.content.Context;
import androidx.annotation.VisibleForTesting;
import android.util.Log;

import com.android.settingslib.utils.AsyncLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
@@ -32,14 +35,6 @@ import java.util.List;
public class LicenseHtmlLoader extends AsyncLoader<File> {
    private static final String TAG = "LicenseHtmlLoader";

    private static final String[] DEFAULT_LICENSE_XML_PATHS = {
            "/system/etc/NOTICE.xml.gz",
            "/vendor/etc/NOTICE.xml.gz",
            "/odm/etc/NOTICE.xml.gz",
            "/oem/etc/NOTICE.xml.gz",
            "/product/etc/NOTICE.xml.gz"};
    private static final String NOTICE_HTML_FILE_NAME = "NOTICE.html";

    private Context mContext;

    public LicenseHtmlLoader(Context context) {
@@ -63,7 +58,7 @@ public class LicenseHtmlLoader extends AsyncLoader<File> {
            return null;
        }

        File cachedHtmlFile = getCachedHtmlFile();
        File cachedHtmlFile = getCachedHtmlFile(mContext);
        if (!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile)
                || generateHtmlFile(xmlFiles, cachedHtmlFile)) {
            return cachedHtmlFile;
@@ -72,40 +67,4 @@ public class LicenseHtmlLoader extends AsyncLoader<File> {
        return null;
    }

    @VisibleForTesting
    List<File> getVaildXmlFiles() {
        final List<File> xmlFiles = new ArrayList();
        for (final String xmlPath : DEFAULT_LICENSE_XML_PATHS) {
            File file = new File(xmlPath);
            if (file.exists() && file.length() != 0) {
                xmlFiles.add(file);
            }
        }
        return xmlFiles;
    }

    @VisibleForTesting
    File getCachedHtmlFile() {
        return new File(mContext.getCacheDir(), NOTICE_HTML_FILE_NAME);
    }

    @VisibleForTesting
    boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
        boolean outdated = true;
        if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) {
            outdated = false;
            for (File file : xmlFiles) {
                if (cachedHtmlFile.lastModified() < file.lastModified()) {
                    outdated = true;
                    break;
                }
            }
        }
        return outdated;
    }

    @VisibleForTesting
    boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
        return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile);
    }
}
+11 −16
Original line number Diff line number Diff line
@@ -25,22 +25,21 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.VisibleForTesting;

/**
 * LicenseHtmlLoader is a loader which loads a license html file from default license xml files.
 */
public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> {
    private static final String TAG = "LicenseHtmlLoaderCompat";

    private static final String[] DEFAULT_LICENSE_XML_PATHS = {
    static final String[] DEFAULT_LICENSE_XML_PATHS = {
            "/system/etc/NOTICE.xml.gz",
            "/vendor/etc/NOTICE.xml.gz",
            "/odm/etc/NOTICE.xml.gz",
            "/oem/etc/NOTICE.xml.gz"};
    private static final String NOTICE_HTML_FILE_NAME = "NOTICE.html";
            "/oem/etc/NOTICE.xml.gz",
            "/product/etc/NOTICE.xml.gz"};
    static final String NOTICE_HTML_FILE_NAME = "NOTICE.html";

    private Context mContext;
    private final Context mContext;

    public LicenseHtmlLoaderCompat(Context context) {
        super(context);
@@ -63,7 +62,7 @@ public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> {
            return null;
        }

        File cachedHtmlFile = getCachedHtmlFile();
        File cachedHtmlFile = getCachedHtmlFile(mContext);
        if (!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile)
                || generateHtmlFile(xmlFiles, cachedHtmlFile)) {
            return cachedHtmlFile;
@@ -72,8 +71,7 @@ public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> {
        return null;
    }

    @VisibleForTesting
    List<File> getVaildXmlFiles() {
    static List<File> getVaildXmlFiles() {
        final List<File> xmlFiles = new ArrayList();
        for (final String xmlPath : DEFAULT_LICENSE_XML_PATHS) {
            File file = new File(xmlPath);
@@ -84,13 +82,11 @@ public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> {
        return xmlFiles;
    }

    @VisibleForTesting
    File getCachedHtmlFile() {
        return new File(mContext.getCacheDir(), NOTICE_HTML_FILE_NAME);
    static File getCachedHtmlFile(Context context) {
        return new File(context.getCacheDir(), NOTICE_HTML_FILE_NAME);
    }

    @VisibleForTesting
    boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
    static boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
        boolean outdated = true;
        if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) {
            outdated = false;
@@ -104,8 +100,7 @@ public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> {
        return outdated;
    }

    @VisibleForTesting
    boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
    static boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
        return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile);
    }
}
+71 −30
Original line number Diff line number Diff line
@@ -17,44 +17,43 @@
package com.android.settingslib.license;

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.settingslib.SettingsLibRobolectricTestRunner;

import org.junit.After;
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;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RunWith(SettingsLibRobolectricTestRunner.class)
@Config(shadows = LicenseHtmlLoaderCompatTest.ShadowLicenseHtmlLoaderCompat.class)
public class LicenseHtmlLoaderCompatTest {

    @Mock
    private Context mContext;

    LicenseHtmlLoaderCompat newLicenseHtmlLoader(ArrayList<File> xmlFiles,
            File cachedHtmlFile, boolean isCachedHtmlFileOutdated,
            boolean generateHtmlFileSucceeded) {
        LicenseHtmlLoaderCompat loader = spy(new LicenseHtmlLoaderCompat(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;
    }
    private LicenseHtmlLoaderCompat mLoader;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mLoader = new LicenseHtmlLoaderCompat(mContext);
    }

    @After
    public void tearDown() {
        ShadowLicenseHtmlLoaderCompat.reset();
    }

    @Test
@@ -63,10 +62,9 @@ public class LicenseHtmlLoaderCompatTest {
        xmlFiles.add(new File("test.xml"));
        File cachedHtmlFile = new File("test.html");

        LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
        setupFakeData(xmlFiles, cachedHtmlFile, true, true);

        assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
        verify(loader).generateHtmlFile(any(), any());
        assertThat(mLoader.loadInBackground()).isEqualTo(cachedHtmlFile);
    }

    @Test
@@ -74,10 +72,9 @@ public class LicenseHtmlLoaderCompatTest {
        ArrayList<File> xmlFiles = new ArrayList();
        File cachedHtmlFile = new File("test.html");

        LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
        setupFakeData(xmlFiles, cachedHtmlFile, true, true);

        assertThat(loader.loadInBackground()).isNull();
        verify(loader, never()).generateHtmlFile(any(), any());
        assertThat(mLoader.loadInBackground()).isNull();
    }

    @Test
@@ -86,11 +83,9 @@ public class LicenseHtmlLoaderCompatTest {
        xmlFiles.add(new File("test.xml"));
        File cachedHtmlFile = new File("test.html");

        LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, false,
                true);
        setupFakeData(xmlFiles, cachedHtmlFile, false, true);

        assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
        verify(loader, never()).generateHtmlFile(any(), any());
        assertThat(mLoader.loadInBackground()).isEqualTo(cachedHtmlFile);
    }

    @Test
@@ -99,10 +94,56 @@ public class LicenseHtmlLoaderCompatTest {
        xmlFiles.add(new File("test.xml"));
        File cachedHtmlFile = new File("test.html");

        LicenseHtmlLoaderCompat loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true,
                false);
        setupFakeData(xmlFiles, cachedHtmlFile, true, false);

        assertThat(mLoader.loadInBackground()).isNull();
    }

    void setupFakeData(ArrayList<File> xmlFiles,
            File cachedHtmlFile, boolean isCachedHtmlFileOutdated,
            boolean generateHtmlFileSucceeded) {

        ShadowLicenseHtmlLoaderCompat.sValidXmlFiles = xmlFiles;
        ShadowLicenseHtmlLoaderCompat.sCachedHtmlFile = cachedHtmlFile;
        ShadowLicenseHtmlLoaderCompat.sIsCachedHtmlFileOutdated = isCachedHtmlFileOutdated;
        ShadowLicenseHtmlLoaderCompat.sGenerateHtmlFileSucceeded = generateHtmlFileSucceeded;
    }

    @Implements(LicenseHtmlLoaderCompat.class)
    public static class ShadowLicenseHtmlLoaderCompat {


        public static List<File> sValidXmlFiles;
        public static File sCachedHtmlFile;
        public static boolean sIsCachedHtmlFileOutdated;
        public static boolean sGenerateHtmlFileSucceeded;

        assertThat(loader.loadInBackground()).isNull();
        verify(loader).generateHtmlFile(any(), any());
        @Resetter
        public static void reset() {
            sValidXmlFiles = null;
            sCachedHtmlFile = null;
            sIsCachedHtmlFileOutdated = false;
            sGenerateHtmlFileSucceeded = false;
        }

        @Implementation
        static List<File> getVaildXmlFiles() {
            return sValidXmlFiles;
        }

        @Implementation
        static File getCachedHtmlFile(Context context) {
            return sCachedHtmlFile;
        }

        @Implementation
        static boolean isCachedHtmlFileOutdated(List<File> xmlFiles, File cachedHtmlFile) {
            return sIsCachedHtmlFileOutdated;
        }

        @Implementation
        static boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
            return sGenerateHtmlFileSucceeded;
        }
    }
}
+0 −106
Original line number Diff line number Diff line
/*
 * 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.settingslib.license;

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.settingslib.SettingsLibRobolectricTestRunner;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.File;
import java.util.ArrayList;

@RunWith(SettingsLibRobolectricTestRunner.class)
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());
    }
}