• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 
30 import java.io.File;
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.annotation.Config;
40 
41 @RunWith(SettingsRobolectricTestRunner.class)
42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43 public class LicenseHtmlLoaderTest {
44     @Mock
45     private Context mContext;
46 
newLicenseHtmlLoader(ArrayList<File> xmlFiles, File cachedHtmlFile, boolean isCachedHtmlFileOutdated, boolean generateHtmlFileSucceeded)47     LicenseHtmlLoader newLicenseHtmlLoader(ArrayList<File> xmlFiles,
48             File cachedHtmlFile, boolean isCachedHtmlFileOutdated,
49             boolean generateHtmlFileSucceeded) {
50         LicenseHtmlLoader loader = spy(new LicenseHtmlLoader(mContext));
51         doReturn(xmlFiles).when(loader).getVaildXmlFiles();
52         doReturn(cachedHtmlFile).when(loader).getCachedHtmlFile();
53         doReturn(isCachedHtmlFileOutdated).when(loader).isCachedHtmlFileOutdated(any(), any());
54         doReturn(generateHtmlFileSucceeded).when(loader).generateHtmlFile(any(), any());
55         return loader;
56     }
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61     }
62 
63     @Test
testLoadInBackground()64     public void testLoadInBackground() {
65         ArrayList<File> xmlFiles = new ArrayList();
66         xmlFiles.add(new File("test.xml"));
67         File cachedHtmlFile = new File("test.html");
68 
69         LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
70 
71         assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
72         verify(loader).generateHtmlFile(any(), any());
73     }
74 
75     @Test
testLoadInBackgroundWithNoVaildXmlFiles()76     public void testLoadInBackgroundWithNoVaildXmlFiles() {
77         ArrayList<File> xmlFiles = new ArrayList();
78         File cachedHtmlFile = new File("test.html");
79 
80         LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true);
81 
82         assertThat(loader.loadInBackground()).isNull();
83         verify(loader, never()).generateHtmlFile(any(), any());
84     }
85 
86     @Test
testLoadInBackgroundWithNonOutdatedCachedHtmlFile()87     public void testLoadInBackgroundWithNonOutdatedCachedHtmlFile() {
88         ArrayList<File> xmlFiles = new ArrayList();
89         xmlFiles.add(new File("test.xml"));
90         File cachedHtmlFile = new File("test.html");
91 
92         LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, false, true);
93 
94         assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile);
95         verify(loader, never()).generateHtmlFile(any(), any());
96     }
97 
98     @Test
testLoadInBackgroundWithGenerateHtmlFileFailed()99     public void testLoadInBackgroundWithGenerateHtmlFileFailed() {
100         ArrayList<File> xmlFiles = new ArrayList();
101         xmlFiles.add(new File("test.xml"));
102         File cachedHtmlFile = new File("test.html");
103 
104         LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, false);
105 
106         assertThat(loader.loadInBackground()).isNull();
107         verify(loader).generateHtmlFile(any(), any());
108     }
109 }
110