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