Disable the sync now menu while no any synced item
- Check the number of the switch on for all SyncStateSwitchPreferences to enable/disable the sync now menu item. Fixes: 147030748 Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.accounts Change-Id: I88e4b042e6d236b98866b9e8bff9ec2c64b41b87
This commit is contained in:
@@ -66,8 +66,8 @@ public class AccountSyncPreferenceController extends AbstractPreferenceControlle
|
|||||||
new SubSettingLauncher(mContext)
|
new SubSettingLauncher(mContext)
|
||||||
.setDestination(AccountSyncSettings.class.getName())
|
.setDestination(AccountSyncSettings.class.getName())
|
||||||
.setArguments(args)
|
.setArguments(args)
|
||||||
.setSourceMetricsCategory( SettingsEnums.ACCOUNT)
|
.setSourceMetricsCategory(SettingsEnums.ACCOUNT)
|
||||||
.setTitleRes( R.string.account_sync_title)
|
.setTitleRes(R.string.account_sync_title)
|
||||||
.launch();
|
.launch();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@@ -42,6 +42,7 @@ import android.view.Menu;
|
|||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
@@ -221,9 +222,8 @@ public class AccountSyncSettings extends AccountPreferenceBase {
|
|||||||
// Note that this also counts accounts that are not currently displayed
|
// Note that this also counts accounts that are not currently displayed
|
||||||
boolean syncActive = !ContentResolver.getCurrentSyncsAsUser(
|
boolean syncActive = !ContentResolver.getCurrentSyncsAsUser(
|
||||||
mUserHandle.getIdentifier()).isEmpty();
|
mUserHandle.getIdentifier()).isEmpty();
|
||||||
menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive);
|
menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive).setEnabled(enabledSyncNowMenu());
|
||||||
menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive);
|
menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -562,6 +562,23 @@ public class AccountSyncSettings extends AccountPreferenceBase {
|
|||||||
return R.string.help_url_accounts;
|
return R.string.help_url_accounts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
boolean enabledSyncNowMenu() {
|
||||||
|
boolean enabled = false;
|
||||||
|
for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) {
|
||||||
|
final Preference pref = getPreferenceScreen().getPreference(i);
|
||||||
|
if (!(pref instanceof SyncStateSwitchPreference)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final SyncStateSwitchPreference syncPref = (SyncStateSwitchPreference) pref;
|
||||||
|
if (syncPref.isChecked()) {
|
||||||
|
enabled = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
private static String formatSyncDate(Context context, Date date) {
|
private static String formatSyncDate(Context context, Date date) {
|
||||||
return DateUtils.formatDateTime(context, date.getTime(),
|
return DateUtils.formatDateTime(context, date.getTime(),
|
||||||
DateUtils.FORMAT_SHOW_DATE
|
DateUtils.FORMAT_SHOW_DATE
|
||||||
|
@@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.settings.accounts;
|
package com.android.settings.accounts;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@@ -24,10 +26,13 @@ import android.content.Context;
|
|||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
import androidx.preference.PreferenceManager;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.testutils.shadow.ShadowContentResolver;
|
import com.android.settings.testutils.shadow.ShadowContentResolver;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
@@ -38,6 +43,14 @@ import org.robolectric.util.ReflectionHelpers;
|
|||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = {ShadowContentResolver.class})
|
@Config(shadows = {ShadowContentResolver.class})
|
||||||
public class AccountSyncSettingsTest {
|
public class AccountSyncSettingsTest {
|
||||||
|
private Context mContext;
|
||||||
|
private AccountSyncSettings mAccountSyncSettings;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mAccountSyncSettings = spy(new TestAccountSyncSettings(mContext));
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
@@ -46,15 +59,52 @@ public class AccountSyncSettingsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onPreferenceTreeClick_nullAuthority_shouldNotCrash() {
|
public void onPreferenceTreeClick_nullAuthority_shouldNotCrash() {
|
||||||
final Context context = RuntimeEnvironment.application;
|
when(mAccountSyncSettings.getActivity()).thenReturn(mock(FragmentActivity.class));
|
||||||
final AccountSyncSettings settings = spy(new AccountSyncSettings());
|
final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext,
|
||||||
when(settings.getActivity()).thenReturn(mock(FragmentActivity.class));
|
|
||||||
final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(context,
|
|
||||||
new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */);
|
new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */);
|
||||||
preference.setOneTimeSyncMode(false);
|
preference.setOneTimeSyncMode(false);
|
||||||
ReflectionHelpers.setField(settings, "mUserHandle", UserHandle.CURRENT);
|
ReflectionHelpers.setField(mAccountSyncSettings, "mUserHandle", UserHandle.CURRENT);
|
||||||
|
|
||||||
settings.onPreferenceTreeClick(preference);
|
mAccountSyncSettings.onPreferenceTreeClick(preference);
|
||||||
// no crash
|
// no crash
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enabledSyncNowMenu_noSyncStateSwitchPreference_returnFalse() {
|
||||||
|
assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOn_returnTrue() {
|
||||||
|
final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext,
|
||||||
|
new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */);
|
||||||
|
preference.setChecked(true);
|
||||||
|
mAccountSyncSettings.getPreferenceScreen().addPreference(preference);
|
||||||
|
|
||||||
|
assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOff_returnFalse() {
|
||||||
|
final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext,
|
||||||
|
new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */);
|
||||||
|
preference.setChecked(false);
|
||||||
|
mAccountSyncSettings.getPreferenceScreen().addPreference(preference);
|
||||||
|
|
||||||
|
assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class TestAccountSyncSettings extends AccountSyncSettings {
|
||||||
|
private PreferenceScreen mScreen;
|
||||||
|
|
||||||
|
public TestAccountSyncSettings(Context context) {
|
||||||
|
final PreferenceManager preferenceManager = new PreferenceManager(context);
|
||||||
|
mScreen = preferenceManager.createPreferenceScreen(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreferenceScreen getPreferenceScreen() {
|
||||||
|
return mScreen;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user