Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3d983d4f authored by Chihhang Chuang's avatar Chihhang Chuang
Browse files

Fix broken tests and add tests for ThemeManagerTest

Change-Id: I12769a3385cdcbe71f2e216a405509477b427eca
parent 58e1b52d
Loading
Loading
Loading
Loading
+95 −17
Original line number Diff line number Diff line
@@ -26,25 +26,28 @@ import static com.android.customization.model.ResourceConstants.SETTINGS_PACKAGE
import static com.android.customization.model.ResourceConstants.SYSUI_PACKAGE;
import static com.android.customization.model.ResourceConstants.THEME_SETTING;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.provider.Settings;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.android.customization.model.CustomizationManager.OptionsFetchedListener;
import com.android.customization.model.CustomizationManager.Callback;
import com.android.customization.model.theme.custom.CustomTheme;
import com.android.customization.module.ThemesUserEventLogger;
import com.android.customization.testutils.OverlayManagerMocks;
import com.android.customization.testutils.Wait;
import com.android.wallpaper.module.WallpaperSetter;

import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -60,6 +63,7 @@ public class ThemeManagerTest {
    @Mock OverlayManagerCompat mMockOm;
    @Mock WallpaperSetter mMockWallpaperSetter;
    @Mock ThemesUserEventLogger mThemesUserEventLogger;
    @Mock ThemeBundleProvider mThemeBundleProvider;
    private OverlayManagerMocks mMockOmHelper;
    private ThemeManager mThemeManager;
    private FragmentActivity mActivity;
@@ -71,9 +75,8 @@ public class ThemeManagerTest {
        mActivity = spy(activity);
        mMockOmHelper = new OverlayManagerMocks();
        mMockOmHelper.setUpMock(mMockOm);
        ThemeBundleProvider provider = mock(ThemeBundleProvider.class);
        mThemeManager = new ThemeManager(
                provider, activity, mMockWallpaperSetter, mMockOm, mThemesUserEventLogger);
        mThemeManager = new ThemeManager(mThemeBundleProvider, activity, mMockWallpaperSetter,
                mMockOm, mThemesUserEventLogger);
    }

    @After
@@ -82,7 +85,7 @@ public class ThemeManagerTest {
    }

    @Test
    public void testApply_DefaultTheme() {
    public void apply_WithDefaultTheme_StoresEmptyJsonString() {
        mMockOmHelper.addOverlay("test.package.name_color", ANDROID_PACKAGE,
                OVERLAY_CATEGORY_COLOR, true, 0);
        mMockOmHelper.addOverlay("test.package.name_font", ANDROID_PACKAGE,
@@ -102,13 +105,94 @@ public class ThemeManagerTest {

        applyTheme(defaultTheme);

        assertEquals("Secure Setting should be emtpy after applying default theme",
                "",
        assertEquals("Secure Setting should be empty JSON string after applying default theme",
                new JSONObject().toString(),
                Settings.Secure.getString(mActivity.getContentResolver(), THEME_SETTING));
    }

    @Test
    public void testApply_NonDefault() {
    public void apply_WithOverlayTheme_StoresSerializedPackagesWithTimestamp() {
        ThemeBundle theme = getOverlayTheme();
        final String serializedPackagesWithTimestamp = theme.getSerializedPackagesWithTimestamp();

        theme = spy(theme);
        // Makes it return the fixed serializedPackagesWithTimestamp to test. Since we will get
        // fresh time every time, it's hard to compare for testing.
        when(theme.getSerializedPackagesWithTimestamp())
                .thenReturn(serializedPackagesWithTimestamp);

        applyTheme(theme);

        assertEquals("Secure Setting should be the overlay packages after applying theme",
                serializedPackagesWithTimestamp,
                Settings.Secure.getString(mActivity.getContentResolver(), THEME_SETTING));
    }

    @Test
    public void isAvailable_ThemeBundleProviderAndOverlayManagerAreAvailable_ReturnsTrue() {
        when(mThemeBundleProvider.isAvailable()).thenReturn(true);
        when(mMockOm.isAvailable()).thenReturn(true);

        assertTrue(mThemeManager.isAvailable());
    }

    @Test
    public void isAvailable_ThemeBundleProviderOrOverlayManagerIsAvailable_ReturnsFalse() {
        when(mThemeBundleProvider.isAvailable()).thenReturn(false);
        when(mMockOm.isAvailable()).thenReturn(true);
        assertFalse(mThemeManager.isAvailable());

        when(mThemeBundleProvider.isAvailable()).thenReturn(true);
        when(mMockOm.isAvailable()).thenReturn(false);
        assertFalse(mThemeManager.isAvailable());
    }

    @Test
    public void fetchOptions_ThemeBundleProviderFetches() {
        OptionsFetchedListener listener = mock(OptionsFetchedListener.class);

        mThemeManager.fetchOptions(listener, false);

        verify(mThemeBundleProvider).fetch(listener, false);
    }

    @Test
    public void removeCustomTheme_ThemeBundleProviderRemovesCustomTheme() {
        CustomTheme customTheme = mock(CustomTheme.class);
        mThemeManager.removeCustomTheme(customTheme);

        verify(mThemeBundleProvider).removeCustomTheme(customTheme);
    }

    @Test
    public void findThemeByPackages_ThemeBundleProviderFindsEquivalent() {
        CustomTheme theme = mock(CustomTheme.class);
        mThemeManager.findThemeByPackages(theme);

        verify(mThemeBundleProvider).findEquivalent(theme);
    }

    @Test
    public void storeEmptyTheme_SettingsSecureStoresEmptyTheme() {
        mThemeManager.storeEmptyTheme();

        assertEquals(
                new JSONObject().toString(),
                Settings.Secure.getString(mActivity.getContentResolver(), THEME_SETTING));
    }

    @Test
    public void getStoredOverlays_GetsFromSettingsSecureWithExpectedName() {
        ThemeBundle theme = getOverlayTheme();

        applyTheme(theme);

        assertEquals(
                Settings.Secure.getString(mActivity.getContentResolver(), THEME_SETTING),
                mThemeManager.getStoredOverlays());
    }

    private ThemeBundle getOverlayTheme() {
        final String bundleColorPackage = "test.package.name_color";
        final String bundleFontPackage = "test.package.name_font";
        final String otherPackage = "other.package.name_font";
@@ -120,16 +204,10 @@ public class ThemeManagerTest {
        mMockOmHelper.addOverlay(otherPackage, ANDROID_PACKAGE,
                OVERLAY_CATEGORY_FONT, false, 0);

        ThemeBundle theme = new ThemeBundle.Builder()
        return new ThemeBundle.Builder()
                .addOverlayPackage(OVERLAY_CATEGORY_COLOR, bundleColorPackage)
                .addOverlayPackage(OVERLAY_CATEGORY_FONT, bundleFontPackage)
                .build(mActivity);

        applyTheme(theme);

        assertEquals("Secure Setting was not properly set after applying theme",
                theme.getSerializedPackages(),
                Settings.Secure.getString(mActivity.getContentResolver(), THEME_SETTING));
    }

    private void applyTheme(ThemeBundle theme) {