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.anyInt;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26 import static org.robolectric.Shadows.shadowOf;
27 
28 import android.app.Application;
29 import android.os.Bundle;
30 import android.os.SystemProperties;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.Loader;
34 import android.net.Uri;
35 import android.os.Bundle;
36 
37 import java.io.File;
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.Robolectric;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.res.builder.RobolectricPackageManager;
50 import org.robolectric.util.ActivityController;
51 import org.robolectric.shadows.ShadowActivity;
52 
53 @RunWith(SettingsRobolectricTestRunner.class)
54 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
55 public class SettingsLicenseActivityTest {
56     private ActivityController<SettingsLicenseActivity> mActivityController;
57     private SettingsLicenseActivity mActivity;
58     private Application mApplication;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63 
64         mApplication = RuntimeEnvironment.application;
65         mActivityController = Robolectric.buildActivity(SettingsLicenseActivity.class);
66         mActivity = spy(mActivityController.get());
67     }
68 
assertEqualIntents(Intent actual, Intent expected)69     void assertEqualIntents(Intent actual, Intent expected) {
70         assertThat(actual.getAction()).isEqualTo(expected.getAction());
71         assertThat(actual.getDataString()).isEqualTo(expected.getDataString());
72         assertThat(actual.getType()).isEqualTo(expected.getType());
73         assertThat(actual.getCategories()).isEqualTo(expected.getCategories());
74         assertThat(actual.getPackage()).isEqualTo(expected.getPackage());
75         assertThat(actual.getFlags()).isEqualTo(expected.getFlags());
76     }
77 
78     @Test
testOnCreateWithValidHtmlFile()79     public void testOnCreateWithValidHtmlFile() {
80         SystemProperties.set("ro.config.license_path", "/system/etc/NOTICE.html.gz");
81 
82         doReturn(true).when(mActivity).isFileValid(any());
83         mActivity.onCreate(null);
84 
85         final Intent intent = new Intent(Intent.ACTION_VIEW);
86         intent.setDataAndType(Uri.parse("file:///system/etc/NOTICE.html.gz"), "text/html");
87         intent.putExtra(Intent.EXTRA_TITLE, mActivity.getString(
88                 R.string.settings_license_activity_title));
89         intent.addCategory(Intent.CATEGORY_DEFAULT);
90         intent.setPackage("com.android.htmlviewer");
91 
92         assertEqualIntents(shadowOf(mApplication).getNextStartedActivity(), intent);
93     }
94 
95     @Test
testOnCreateWithGeneratedHtmlFile()96     public void testOnCreateWithGeneratedHtmlFile() {
97         doReturn(null).when(mActivity).onCreateLoader(anyInt(), any());
98         doReturn(Uri.parse("content://com.android.settings.files/my_cache/generated_test.html"))
99                 .when(mActivity).getUriFromGeneratedHtmlFile(any());
100 
101         mActivity.onCreate(null);
102         mActivity.onLoadFinished(null, new File("/generated_test.html"));
103 
104         final Intent intent = new Intent(Intent.ACTION_VIEW);
105         intent.setDataAndType(
106                 Uri.parse("content://com.android.settings.files/my_cache/generated_test.html"),
107                 "text/html");
108         intent.putExtra(Intent.EXTRA_TITLE, mActivity.getString(
109                 R.string.settings_license_activity_title));
110         intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
111         intent.addCategory(Intent.CATEGORY_DEFAULT);
112         intent.setPackage("com.android.htmlviewer");
113 
114         assertEqualIntents(shadowOf(mApplication).getNextStartedActivity(), intent);
115     }
116 }
117