Merge "Add tests for BackupSettingsActivity"
This commit is contained in:
committed by
Android (Google) Code Review
commit
0f76385e13
@@ -17,9 +17,11 @@
|
||||
package com.android.settings.backup;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.FragmentManager;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
@@ -29,6 +31,7 @@ import android.util.Log;
|
||||
*/
|
||||
public class BackupSettingsActivity extends Activity {
|
||||
private static final String TAG = "BackupSettingsActivity";
|
||||
private FragmentManager mFragmentManager;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -55,9 +58,18 @@ public class BackupSettingsActivity extends Activity {
|
||||
if (Log.isLoggable(TAG, Log.DEBUG)) {
|
||||
Log.d(TAG, "Manufacturer provided backup settings, showing the preference screen");
|
||||
}
|
||||
getFragmentManager().beginTransaction()
|
||||
// mFragmentManager can be set by {@link #setFragmentManager()} for testing
|
||||
if (mFragmentManager == null) {
|
||||
mFragmentManager = getFragmentManager();
|
||||
}
|
||||
mFragmentManager.beginTransaction()
|
||||
.replace(android.R.id.content, new BackupSettingsFragment())
|
||||
.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setFragmentManager(FragmentManager fragmentManager) {
|
||||
mFragmentManager = fragmentManager;
|
||||
}
|
||||
}
|
@@ -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.backup;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.isA;
|
||||
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.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.Application;
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.app.backup.IBackupManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.IBinder;
|
||||
|
||||
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.Robolectric;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
import org.robolectric.res.builder.RobolectricPackageManager;
|
||||
import org.robolectric.util.ActivityController;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
|
||||
shadows = {BackupSettingsActivityTest.ShadowBackupSettingsHelper.class})
|
||||
public class BackupSettingsActivityTest {
|
||||
private ActivityController<BackupSettingsActivity> mActivityController;
|
||||
private BackupSettingsActivity mActivity;
|
||||
private Application mApplication;
|
||||
private RobolectricPackageManager mPackageManager;
|
||||
private static boolean mIsBackupProvidedByOEM;
|
||||
|
||||
@Mock
|
||||
private FragmentManager mFragmentManager;
|
||||
|
||||
@Mock
|
||||
private FragmentTransaction mFragmentTransaction;
|
||||
@Mock
|
||||
private static Intent mIntent;
|
||||
@Mock
|
||||
private ComponentName mComponent;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mApplication = RuntimeEnvironment.application;
|
||||
mActivityController = Robolectric.buildActivity(BackupSettingsActivity.class);
|
||||
mActivity = mActivityController.get();
|
||||
mPackageManager = (RobolectricPackageManager) mApplication.getPackageManager();
|
||||
doReturn(mComponent).when(mIntent).getComponent();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreate_launchActivity() {
|
||||
mIsBackupProvidedByOEM = false;
|
||||
|
||||
// Testing the scenario when the activity is disabled
|
||||
mPackageManager.setComponentEnabledSetting(mComponent,
|
||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
|
||||
|
||||
mActivityController.create();
|
||||
|
||||
// Verify that the component to launch was enabled.
|
||||
assertThat(mPackageManager.getComponentState(mComponent).newState)
|
||||
.isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
|
||||
|
||||
// Verify that the intent returned by BackupSettingsHelper.getIntentForBackupSettings()
|
||||
// was launched.
|
||||
assertThat(shadowOf(mApplication).getNextStartedActivity()).isEqualTo(mIntent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreate_hasManufacturerIntent() {
|
||||
mIsBackupProvidedByOEM = true;
|
||||
|
||||
// Fragments are tested separately, so mock out the manager.
|
||||
mActivity.setFragmentManager(mFragmentManager);
|
||||
doReturn(mFragmentTransaction).when(mFragmentTransaction).replace(anyInt(),
|
||||
any(Fragment.class));
|
||||
doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
|
||||
|
||||
mActivityController.create();
|
||||
|
||||
assertThat(shadowOf(mApplication).getNextStartedActivity()).isNull();
|
||||
verify(mFragmentTransaction).replace(anyInt(), isA(BackupSettingsFragment.class));
|
||||
|
||||
}
|
||||
|
||||
@Implements(BackupSettingsHelper.class)
|
||||
public static class ShadowBackupSettingsHelper {
|
||||
@Implementation
|
||||
public Intent getIntentForBackupSettings() {
|
||||
return mIntent;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public boolean isBackupProvidedByManufacturer() {
|
||||
return mIsBackupProvidedByOEM;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user