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