Merge "Full screen default app fragment"

This commit is contained in:
TreeHugger Robot
2017-01-24 01:38:42 +00:00
committed by Android (Google) Code Review
47 changed files with 2654 additions and 1007 deletions

View File

@@ -21,7 +21,6 @@ import android.content.Context;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
@@ -33,7 +32,6 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -42,27 +40,20 @@ public class AdvancedAppSettingsTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
private AdvancedAppSettings mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mFragment = new AdvancedAppSettings();
mFragment.onAttach(ShadowApplication.getInstance().getApplicationContext());
}
@Test
public void getPreferenceScreenResId_differentIAEnabledState_shouldUseDifferentPrefLayout() {
when(mFeatureFactory.dashboardFeatureProvider.isEnabled()).thenReturn(true);
public void getPreferenceScreenResId_shouldUseAppDefaultSettingPrefLayout() {
assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(
R.xml.app_default_settings);
when(mFeatureFactory.dashboardFeatureProvider.isEnabled()).thenReturn(false);
assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(
R.xml.advanced_apps);
}
}

View File

@@ -0,0 +1,93 @@
/*
* 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.settings.applications.defaultapps;
import android.content.ComponentName;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultAppInfoTest {
@Mock
private ActivityInfo mActivityInfo;
@Mock
private ApplicationInfo mApplicationInfo;
@Mock
private ComponentName mComponentName;
@Mock
private PackageManager mPackageManager;
private DefaultAppInfo mInfo;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void initInfoWithActivityInfo_shouldLoadInfo() {
mActivityInfo.packageName = "test";
mInfo = new DefaultAppInfo(mActivityInfo);
mInfo.loadLabel(mPackageManager);
mInfo.loadIcon(mPackageManager);
assertThat(mInfo.getKey()).isEqualTo(mActivityInfo.packageName);
verify(mActivityInfo).loadLabel(mPackageManager);
verify(mActivityInfo).loadIcon(mPackageManager);
}
@Test
public void initInfoWithApplicationInfo_shouldLoadInfo() {
mApplicationInfo.packageName = "test";
mInfo = new DefaultAppInfo(mApplicationInfo);
mInfo.loadLabel(mPackageManager);
mInfo.loadIcon(mPackageManager);
assertThat(mInfo.getKey()).isEqualTo(mApplicationInfo.packageName);
verify(mApplicationInfo).loadLabel(mPackageManager);
verify(mApplicationInfo).loadIcon(mPackageManager);
}
@Test
public void initInfoWithComponent_shouldLoadInfo() {
when(mComponentName.getPackageName()).thenReturn("com.android.settings");
mInfo = new DefaultAppInfo(0 /* uid */, mComponentName, null /*summary */);
mInfo.getKey();
verify(mComponentName).flattenToString();
}
}

View File

@@ -0,0 +1,139 @@
/*
* 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.settings.applications.defaultapps;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.UserManager;
import android.support.v4.app.FragmentManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.widget.RadioButtonPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
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.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultAppPickerFragmentTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private PreferenceScreen mScreen;
@Mock
private UserManager mUserManager;
@Mock
private FragmentManager mFragmentManager;
private TestFragment mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = spy(new TestFragment());
final Bundle bundle = new Bundle();
bundle.putBoolean(DefaultAppPickerFragment.EXTRA_FOR_WORK, false);
mFragment.setArguments(bundle);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
doReturn(mActivity).when(mFragment).getContext();
doReturn(mScreen).when(mFragment).getPreferenceScreen();
}
@Test
public void onAttach_userIsInitialized() {
mFragment.onAttach((Context) mActivity);
verify(mActivity).getPackageManager();
verify(mActivity).getSystemService(Context.USER_SERVICE);
}
@Test
public void clickPreference_noCofirmation_shouldDirectlyConfirm() {
final RadioButtonPreference pref =
new RadioButtonPreference(RuntimeEnvironment.application);
pref.setKey("TEST");
mFragment.onRadioButtonClicked(pref);
assertThat(mFragment.setDefaultAppKeyCalled).isTrue();
}
@Test
public void clickPreference_hasCofirmation_shouldShowConfirmation() {
final RadioButtonPreference pref =
new RadioButtonPreference(RuntimeEnvironment.application);
pref.setKey("TEST");
doReturn("confirmation_text").when(mFragment)
.getConfirmationMessage(any(DefaultAppInfo.class));
doReturn(mActivity).when(mFragment).getActivity();
mFragment.onRadioButtonClicked(pref);
}
public static class TestFragment extends DefaultAppPickerFragment {
boolean setDefaultAppKeyCalled;
@Override
public int getMetricsCategory() {
return 0;
}
@Override
protected List<DefaultAppInfo> getCandidates() {
return new ArrayList<>();
}
@Override
protected String getDefaultAppKey() {
return null;
}
@Override
protected boolean setDefaultAppKey(String key) {
setDefaultAppKeyCalled = true;
return true;
}
@Override
public Context getContext() {
return RuntimeEnvironment.application;
}
}
}

View File

@@ -0,0 +1,107 @@
/*
* 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.settings.applications.defaultapps;
import android.content.Context;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultAppPreferenceControllerTest {
private static final String TEST_APP_NAME = "test";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private UserManager mUserManager;
@Mock
private Preference mPreference;
private TestPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
}
@Test
public void updateState_hasDefaultApp_shouldUpdateAppName() {
mController = new TestPreferenceController(mContext);
when(mController.mAppInfo.loadLabel(mContext.getPackageManager()))
.thenReturn(TEST_APP_NAME);
mController.updateState(mPreference);
verify(mPreference).setSummary(TEST_APP_NAME);
}
@Test
public void updateState_hasNoApp_shouldNotUpdateAppName() {
mController = new TestPreferenceController(mContext);
mController.updateState(mPreference);
verify(mPreference, never()).setSummary(any(CharSequence.class));
}
private static class TestPreferenceController extends DefaultAppPreferenceController {
private DefaultAppInfo mAppInfo;
public TestPreferenceController(Context context) {
super(context);
mAppInfo = mock(DefaultAppInfo.class);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return "test";
}
@Override
protected DefaultAppInfo getDefaultAppInfo() {
return mAppInfo;
}
}
}

View File

@@ -0,0 +1,84 @@
/*
* 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.settings.applications.defaultapps;
import android.app.Activity;
import android.content.Context;
import android.os.UserManager;
import android.provider.Settings;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultBrowserPickerTest {
private static final String TEST_APP_KEY = "";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private PackageManagerWrapper mPackageManager;
private DefaultBrowserPicker mPicker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mPicker = new DefaultBrowserPicker();
mPicker.onAttach((Context) mActivity);
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
}
@Test
public void setDefaultAppKey_shouldUpdateDefaultBrowser() {
mPicker.setDefaultAppKey(TEST_APP_KEY);
verify(mPackageManager)
.setDefaultBrowserPackageNameAsUser(eq(TEST_APP_KEY), anyInt());
}
@Test
public void getDefaultAppKey_shouldReturnDefaultBrowser() {
mPicker.getDefaultAppKey();
verify(mPackageManager)
.getDefaultBrowserPackageNameAsUser(anyInt());
}
}

View File

@@ -0,0 +1,89 @@
/*
* 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.settings.applications.defaultapps;
import android.content.Context;
import android.content.Intent;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultBrowserPreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private UserManager mUserManager;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PackageManagerWrapper mPackageManager;
private DefaultBrowserPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mController = new DefaultBrowserPreferenceController(mContext);
ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getSoleAppLabel_hasNoApp_shouldNotReturnLabel() {
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(null);
final Preference pref = mock(Preference.class);
mController.updateState(pref);
verify(pref, never()).setSummary(any(String.class));
}
@Test
public void getDefaultApp_shouldGetDefaultBrowserPackage() {
mController.getDefaultAppInfo();
verify(mPackageManager).getDefaultBrowserPackageNameAsUser(anyInt());
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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.settings.applications.defaultapps;
import android.app.Activity;
import android.content.Context;
import android.os.UserManager;
import android.provider.Settings;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultEmergencyPickerTest {
private static final String TEST_APP_KEY = "test_app";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private PackageManagerWrapper mPackageManager;
private DefaultEmergencyPicker mPicker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mPicker = spy(new DefaultEmergencyPicker());
mPicker.onAttach((Context) mActivity);
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
}
@Test
public void setDefaultAppKey_shouldUpdateDefault() {
assertThat(mPicker.setDefaultAppKey(TEST_APP_KEY)).isTrue();
assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY);
}
@Test
public void getDefaultAppKey_shouldReturnDefault() {
Settings.Secure.putString(RuntimeEnvironment.application.getContentResolver(),
Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
TEST_APP_KEY);
assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY);
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.settings.applications.defaultapps;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.IntentFilter;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultHomePickerTest {
private static final String TEST_APP_KEY = "com.android.settings/DefaultEmergencyPickerTest";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private PackageManagerWrapper mPackageManager;
private DefaultHomePicker mPicker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mPicker = new DefaultHomePicker();
mPicker.onAttach((Context) mActivity);
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
}
@Test
public void setDefaultAppKey_shouldUpdateDefault() {
assertThat(mPicker.setDefaultAppKey(TEST_APP_KEY)).isTrue();
verify(mPackageManager).replacePreferredActivity(any(IntentFilter.class),
anyInt(), any(ComponentName[].class), any(ComponentName.class));
}
@Test
public void getDefaultAppKey_shouldReturnDefault() {
final ComponentName cn = mock(ComponentName.class);
when(mPackageManager.getHomeActivities(anyList()))
.thenReturn(cn);
mPicker.getDefaultAppKey();
verify(cn).flattenToString();
}
}

View File

@@ -0,0 +1,87 @@
/*
* 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.settings.applications.defaultapps;
import android.content.Context;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultHomePreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private UserManager mUserManager;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PackageManagerWrapper mPackageManager;
private DefaultHomePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mController = spy(new DefaultHomePreferenceController(mContext));
ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getDefaultApp_shouldGetDefaultBrowserPackage() {
assertThat(mController.getDefaultAppInfo()).isNotNull();
verify(mPackageManager).getHomeActivities(anyList());
}
@Test
public void updateState_noDefaultApp_shouldAskPackageManagerForOnlyApp() {
doReturn(null).when(mController).getDefaultAppInfo();
mController.updateState(mock(Preference.class));
verify(mPackageManager).getHomeActivities(anyList());
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.settings.applications.defaultapps;
import android.app.Activity;
import android.content.Context;
import android.os.UserManager;
import android.provider.Settings;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultNotificationAssistantPickerTest {
private static final String TEST_APP_KEY = "com.android.settings/PickerTest";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private PackageManagerWrapper mPackageManager;
private DefaultNotificationAssistantPicker mPicker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mPicker = spy(new DefaultNotificationAssistantPicker());
mPicker.onAttach((Context) mActivity);
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
}
@Test
public void setDefaultAppKey_shouldUpdateDefault() {
mPicker.setDefaultAppKey(TEST_APP_KEY);
assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY);
}
@Test
public void getDefaultAppKey_shouldReturnDefault() {
Settings.Secure.putString(RuntimeEnvironment.application.getContentResolver(),
Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT,
TEST_APP_KEY);
assertThat(mPicker.getDefaultAppKey()).isEqualTo(TEST_APP_KEY);
}
}

View File

@@ -0,0 +1,96 @@
/*
* 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.settings.applications.defaultapps;
import android.app.Activity;
import android.content.Context;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultPhonePickerTest {
private static final String TEST_APP_KEY = "com.android.settings/PickerTest";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private DefaultPhonePicker.DefaultKeyUpdater mDefaultKeyUpdater;
@Mock
private PackageManagerWrapper mPackageManager;
private DefaultPhonePicker mPicker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mActivity.getSystemService(Context.TELECOM_SERVICE)).thenReturn(null);
mPicker = spy(new DefaultPhonePicker());
mPicker.onAttach((Context) mActivity);
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
ReflectionHelpers.setField(mPicker, "mDefaultKeyUpdater", mDefaultKeyUpdater);
doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
}
@Test
public void getSystemDefaultPackage_shouldAskDefaultKeyUpdater() {
mPicker.getSystemDefaultAppKey();
verify(mDefaultKeyUpdater).getSystemDialerPackage();
}
@Test
public void setDefaultAppKey_shouldUpdateDefault() {
mPicker.setDefaultAppKey(TEST_APP_KEY);
verify(mDefaultKeyUpdater).setDefaultDialerApplication(
any(Context.class), eq(TEST_APP_KEY), anyInt());
}
@Test
public void getDefaultAppKey_shouldReturnDefault() {
mPicker.getDefaultAppKey();
verify(mDefaultKeyUpdater).getDefaultDialerApplication(any(Context.class), anyInt());
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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.settings.applications.defaultapps;
import android.app.Activity;
import android.content.Context;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.PackageManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DefaultSmsPickerTest {
private static final String TEST_APP_KEY = "com.android.settings/PickerTest";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private DefaultSmsPicker.DefaultKeyUpdater mDefaultKeyUpdater;
@Mock
private PackageManagerWrapper mPackageManager;
private DefaultSmsPicker mPicker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mPicker = spy(new DefaultSmsPicker());
mPicker.onAttach((Context) mActivity);
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
ReflectionHelpers.setField(mPicker, "mDefaultKeyUpdater", mDefaultKeyUpdater);
doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
}
@Test
public void setDefaultAppKey_shouldUpdateDefault() {
mPicker.setDefaultAppKey(TEST_APP_KEY);
verify(mDefaultKeyUpdater).setDefaultApplication(any(Context.class), eq(TEST_APP_KEY));
}
@Test
public void getDefaultAppKey_shouldReturnDefault() {
mPicker.getDefaultAppKey();
verify(mDefaultKeyUpdater).getDefaultApplication(any(Context.class));
}
}