Merge "Improve UI pref when sync/cancel account syncs."

This commit is contained in:
Fan Zhang
2016-12-09 17:39:15 +00:00
committed by Android (Google) Code Review
5 changed files with 300 additions and 55 deletions

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2016 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.accounts;
import android.accounts.Account;
import android.content.Context;
import com.android.settings.R;
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.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AccountPreferenceTest {
private Context mContext;
private Account mAccount;
private ArrayList<String> mAuthorities;
private AccountPreference mPreference;
@Before
public void setUp() {
mContext = ShadowApplication.getInstance().getApplicationContext();
mAccount = new Account("name", "type");
mAuthorities = new ArrayList<>();
mAuthorities.add("authority");
mPreference = spy(new AccountPreference(
mContext, mAccount, null /* icon */, mAuthorities, false /* showTypeIcon */));
}
@Test
public void setSyncStatus_differentStatus_shouldUpdate() {
mPreference.setSyncStatus(AccountPreference.SYNC_ERROR, true);
verify(mPreference).setSummary(R.string.sync_error);
}
@Test
public void setSyncStatus_sameStatus_shouldNotUpdate() {
// Set it once, should update summary
mPreference.setSyncStatus(AccountPreference.SYNC_ERROR, true);
verify(mPreference).setSummary(R.string.sync_error);
// Set it again, should not update summary
mPreference.setSyncStatus(AccountPreference.SYNC_ERROR, true);
verify(mPreference).setSummary(R.string.sync_error);
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2016 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.accounts;
import android.accounts.Account;
import android.content.SyncInfo;
import android.content.SyncStatusInfo;
import android.os.UserHandle;
import android.support.v7.preference.PreferenceScreen;
import android.util.ArraySet;
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 java.util.ArrayList;
import java.util.List;
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 ManageAccountsSettingsTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private AccountPreference mAccountPref;
private Account mAccount;
private ArrayList<String> mAuthorities;
private TestFragment mSettings;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mAuthorities = new ArrayList<>();
mAuthorities.add("authority");
mAccount = new Account("name", "type");
when(mAccountPref.getAccount()).thenReturn(mAccount);
when(mAccountPref.getAuthorities()).thenReturn(mAuthorities);
mSettings = new TestFragment();
}
@Test
public void showSyncState_noAccountPrefs_shouldUpdateNothing() {
when(mAccountPref.getAuthorities()).thenReturn(null);
mSettings.showSyncState();
verify(mSettings.getPreferenceScreen(), never()).getPreference(anyInt());
}
@Test
public void showSyncState_syncInProgress_shouldUpdateInProgress() {
mSettings.mUserFacingSyncAuthorities.add(mAuthorities.get(0));
mSettings.mSyncInfos.add(new SyncInfo(0, mAccount, mAuthorities.get(0), 0));
mSettings.mSyncStatusInfo = new SyncStatusInfo(0);
when(mSettings.getPreferenceScreen().getPreferenceCount()).thenReturn(1);
when(mSettings.getPreferenceScreen().getPreference(0)).thenReturn(mAccountPref);
mSettings.showSyncState();
verify(mSettings.getPreferenceScreen()).getPreference(anyInt());
verify(mAccountPref).setSyncStatus(AccountPreference.SYNC_IN_PROGRESS, true);
}
@Test
public void showSyncState_noUserFacingSynclets_shouldUpdateToDisabled() {
mSettings.mSyncInfos.add(new SyncInfo(0, mAccount, mAuthorities.get(0), 0));
mSettings.mSyncStatusInfo = new SyncStatusInfo(0);
when(mSettings.getPreferenceScreen().getPreferenceCount()).thenReturn(1);
when(mSettings.getPreferenceScreen().getPreference(0)).thenReturn(mAccountPref);
mSettings.showSyncState();
verify(mSettings.getPreferenceScreen()).getPreference(anyInt());
verify(mAccountPref).setSyncStatus(AccountPreference.SYNC_DISABLED, true);
}
public static class TestFragment extends ManageAccountsSettings {
private PreferenceScreen mScreen;
private List<SyncInfo> mSyncInfos;
private SyncStatusInfo mSyncStatusInfo;
public TestFragment() {
mUserHandle = mock(UserHandle.class);
mScreen = mock(PreferenceScreen.class);
mUserFacingSyncAuthorities = new ArraySet<>();
mSyncInfos = new ArrayList<>();
}
@Override
public PreferenceScreen getPreferenceScreen() {
return mScreen;
}
@Override
protected boolean isSyncEnabled(int userId, Account account, String authority) {
return true;
}
@Override
protected List<SyncInfo> getCurrentSyncs(int userId) {
return mSyncInfos;
}
@Override
protected SyncStatusInfo getSyncStatusInfo(Account account, String authority, int userId) {
return mSyncStatusInfo;
}
}
}