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

Commit 88051e73 authored by Kunhung Li's avatar Kunhung Li Committed by Android (Google) Code Review
Browse files

Merge "Migrate color related tests into AOSP" into tm-dev

parents eb04ab87 3966f143
Loading
Loading
Loading
Loading
+175 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.customization.model.color

import android.app.WallpaperColors
import android.graphics.Color
import com.android.customization.model.CustomizationManager
import com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_SYSTEM_PALETTE
import com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_COLOR
import com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_HOME
import com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_PRESET
import com.android.customization.model.color.ColorOptionsProvider.OVERLAY_COLOR_BOTH
import com.android.customization.model.color.ColorOptionsProvider.OVERLAY_COLOR_INDEX
import com.android.customization.model.color.ColorOptionsProvider.OVERLAY_COLOR_SOURCE
import com.android.customization.model.theme.OverlayManagerCompat
import com.android.systemui.monet.Style
import com.google.common.truth.Truth.assertThat
import org.json.JSONObject
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

/**
 * Tests of {@link ColorCustomizationManager}.
 */
// TODO(b/222433744): most of these tests are failing due to the manager apk missing in the image
@RunWith(RobolectricTestRunner::class)
class ColorCustomizationManagerTest {

    @get:Rule
    val rule: MockitoRule = MockitoJUnit.rule()

    @Mock private lateinit var provider: ColorOptionsProvider
    @Mock private lateinit var mockOM: OverlayManagerCompat

    private lateinit var manager: ColorCustomizationManager

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        val application = RuntimeEnvironment.application
        manager = ColorCustomizationManager(provider, application.contentResolver, mockOM)
    }

    @Test
    fun testParseSettings() {
        val source = COLOR_SOURCE_HOME
        val style = Style.SPRITZ
        val someColor = "aabbcc"
        val someOtherColor = "bbccdd"
        val settings = mapOf(OVERLAY_CATEGORY_SYSTEM_PALETTE to someColor,
                OVERLAY_CATEGORY_COLOR to someOtherColor,
                OVERLAY_COLOR_SOURCE to source,
                ColorOption.TIMESTAMP_FIELD to "12345")
        val json = JSONObject(settings).toString()

        manager.parseSettings(json)

        assertThat(manager.currentColorSource).isEqualTo(source)
        assertThat(manager.currentStyle).isEqualTo(style)
        assertThat(manager.currentOverlays.size).isEqualTo(2)
        assertThat(manager.currentOverlays.get(OVERLAY_CATEGORY_COLOR)).isEqualTo(someOtherColor)
        assertThat(manager.currentOverlays.get(OVERLAY_CATEGORY_SYSTEM_PALETTE))
                .isEqualTo(someColor)
    }

    @Test
    fun apply_ColorBundle_index() {
        testApplyColorBundle(1, "1")
        testApplyColorBundle(2, "2")
        testApplyColorBundle(3, "3")
        testApplyColorBundle(4, "4")
    }

    private fun testApplyColorBundle(index: Int, value: String) {
        manager.apply(getColorBundle(index), object : CustomizationManager.Callback {
            override fun onSuccess() {}
            override fun onError(throwable: Throwable?) {}
        })

        val overlaysJson = JSONObject(manager.storedOverlays)

        assertThat(overlaysJson.getString(OVERLAY_COLOR_INDEX)).isEqualTo(value)
    }

    private fun getColorBundle(index: Int): ColorBundle {
        return ColorBundle(
            "fake color", mapOf("fake_package" to "fake_color"),
            /* isDefault= */ false,
            null,
            /* index= */ index,
            null
        )
    }

    @Test
    fun apply_ColorSeed_index() {
        testApplyColorSeed(1, "1")
        testApplyColorSeed(2, "2")
        testApplyColorSeed(3, "3")
        testApplyColorSeed(4, "4")
    }

    private fun testApplyColorSeed(index: Int, value: String) {
        manager.apply(getColorSeed(index), object : CustomizationManager.Callback {
            override fun onSuccess() {}
            override fun onError(throwable: Throwable?) {}
        })

        val overlaysJson = JSONObject(manager.storedOverlays)
        assertThat(overlaysJson.getString(OVERLAY_COLOR_INDEX)).isEqualTo(value)
    }

    private fun getColorSeed(index: Int): ColorSeedOption {
        return ColorSeedOption(
            "fake color",
            mapOf("fake_package" to "fake_color"),
            /* isDefault= */ false,
            COLOR_SOURCE_PRESET,
            null,
            index,
            null
        )
    }

    @Test
    fun testApply_colorSeedFromWallpaperBoth_shouldReturnBothValue() {
        val wallpaperColor = WallpaperColors(Color.valueOf(Color.RED), null, null)
        manager.setWallpaperColors(wallpaperColor, wallpaperColor)

        manager.apply(getColorSeed(anyInt()), object : CustomizationManager.Callback {
            override fun onSuccess() {}
            override fun onError(throwable: Throwable?) {}
        })

        val overlaysJson = JSONObject(manager.storedOverlays)
        assertThat(overlaysJson.getString(OVERLAY_COLOR_BOTH)).isEqualTo("1")
    }

    @Test
    fun testApply_colorSeedFromWallpaperDifferent_shouldReturnNonBothValue() {
        val wallpaperColor1 = WallpaperColors(Color.valueOf(Color.RED), null, null)
        val wallpaperColor2 = WallpaperColors(Color.valueOf(Color.BLUE), null, null)
        manager.setWallpaperColors(wallpaperColor1, wallpaperColor2)

        manager.apply(getColorSeed(anyInt()), object : CustomizationManager.Callback {
            override fun onSuccess() {}
            override fun onError(throwable: Throwable?) {}
        })

        val overlaysJson = JSONObject(manager.storedOverlays)
        assertThat(overlaysJson.getString(OVERLAY_COLOR_BOTH)).isEqualTo("0")
    }
}
+180 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.customization.model.color

import com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_HOME
import com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_LOCK
import com.android.customization.model.color.ColorOptionsProvider.COLOR_SOURCE_PRESET
import com.android.systemui.monet.Style
import com.google.common.truth.Truth.assertThat
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.robolectric.RobolectricTestRunner

/**
 * Tests of {@link ColorOption}.
 */
@RunWith(RobolectricTestRunner::class)
class ColorOptionTest {

    @get:Rule
    val rule: MockitoRule = MockitoJUnit.rule()

    @Mock
    private lateinit var manager: ColorCustomizationManager

    @Test
    fun colorOption_Source_Preset() {
        val bundleOption: ColorOption = ColorBundle("fake color",
                mapOf("fake_package" to "fake_color"), false, null, /* index= */ 0, null)
        assertEquals(COLOR_SOURCE_PRESET, bundleOption.source)
    }

    @Test
    fun colorOption_bundle_index() {
        testBundleOptionIndex(1)
        testBundleOptionIndex(2)
        testBundleOptionIndex(3)
        testBundleOptionIndex(4)
    }

    private fun testBundleOptionIndex(index: Int) {
        val bundleOption: ColorBundle = ColorBundle("fake color",
                mapOf("fake_package" to "fake_color"), false, null, /* index= */ index, null)
        assertThat(bundleOption.index).isEqualTo(index)
    }

    @Test
    fun colorOption_Source_Seed() {
        testSeedOptionSource(COLOR_SOURCE_HOME)
        testSeedOptionSource(COLOR_SOURCE_LOCK)
    }

    private fun testSeedOptionSource(source: String) {
        val seedOption: ColorOption = ColorSeedOption("fake color",
                mapOf("fake_package" to "fake_color"), false, source, null, /* index= */ 0, null)
        assertThat(seedOption.source).isEqualTo(source)
    }

    @Test
    fun colorOption_seed_style() {
        testSeedOptionStyle(Style.TONAL_SPOT)
        testSeedOptionStyle(Style.SPRITZ)
        testSeedOptionStyle(Style.VIBRANT)
        testSeedOptionStyle(Style.EXPRESSIVE)
    }

    private fun testSeedOptionStyle(style: Style) {
        val seedOption: ColorOption = ColorSeedOption("fake color",
            mapOf("fake_package" to "fake_color"), /* isDefault= */ false, "fake_source", style,
            0, null)
        assertThat(seedOption.style).isEqualTo(style)
    }

    @Test
    fun colorOption_seed_index() {
        testSeedOptionIndex(1)
        testSeedOptionIndex(2)
        testSeedOptionIndex(3)
        testSeedOptionIndex(4)
    }

    private fun testSeedOptionIndex(index: Int) {
        val seedOption: ColorOption = ColorSeedOption("fake color",
                mapOf("fake_package" to "fake_color"),
                /* isDefault= */ false,
                "fake_source",
                Style.TONAL_SPOT,
                index,
                /* previewInfo= */ null)
        assertThat(seedOption.index).isEqualTo(index)
    }

    private fun setUpSeedOption(isDefault: Boolean, source: String = "some_source")
            : ColorSeedOption {
        val overlays = if (isDefault) {
            HashMap()
        } else {
            mapOf("package" to "value", "otherPackage" to "otherValue")
        }
        `when`(manager.currentOverlays).thenReturn(overlays)
        return ColorSeedOption("seed",
                overlays,
                isDefault,
                source,
                Style.TONAL_SPOT,
                /* index= */ 0,
                /* previewInfo= */ null)
    }

    @Test
    fun seedOption_isActive_notDefault_SourceSet() {
        val source = "some_source"
        val seedOption = setUpSeedOption(false, source)
        `when`(manager.currentColorSource).thenReturn(source)

        assertThat(seedOption.isActive(manager)).isTrue()
    }

    @Test
    fun seedOption_isActive_notDefault_NoSource() {
        val seedOption = setUpSeedOption(false)
        `when`(manager.currentColorSource).thenReturn(null)

        assertThat(seedOption.isActive(manager)).isTrue()
    }

    @Test
    fun seedOption_isActive_notDefault_differentSource() {
        val seedOption = setUpSeedOption(false)
        `when`(manager.currentColorSource).thenReturn("some_other_source")

        assertThat(seedOption.isActive(manager)).isFalse()
    }

    @Test
    fun seedOption_isActive_default_emptyJson() {
        val seedOption = setUpSeedOption(true)
        `when`(manager.storedOverlays).thenReturn("")

        assertThat(seedOption.isActive(manager)).isTrue()
    }

    @Test
    fun seedOption_isActive_default_nonEmptyJson() {
        val seedOption = setUpSeedOption(true)

        `when`(manager.storedOverlays).thenReturn("{non-empty-json}")

        // Should still be Active because overlays is empty
        assertThat(seedOption.isActive(manager)).isTrue()
    }

    @Test
    fun seedOption_isActive_default_nonEmptyOverlays() {
        val seedOption = setUpSeedOption(true)

        `when`(manager.currentOverlays).thenReturn(mapOf("a" to "b"))
        // TODO(b/222433744): failing as it's true
        assertThat(seedOption.isActive(manager)).isFalse()
    }
}
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.customization.model.color;

import static com.google.common.truth.Truth.assertThat;

import androidx.appcompat.app.AppCompatActivity;

import com.android.wallpaper.model.WallpaperColorsViewModel;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

/**
 * Tests of {@link ColorSectionController}.
 */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public final class ColorSectionControllerTest {

    private AppCompatActivity mActivity;
    private ColorSectionController mColorSectionController;

    /**
     * Set up the test case.
     */
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mActivity = Robolectric.buildActivity(AppCompatActivity.class).create().get();
        mColorSectionController = new ColorSectionController(mActivity,
                new WallpaperColorsViewModel(), mActivity, null);
    }

    /**
     * isAvailable()'s test.
     */
    @Test
    @Config(manifest = Config.NONE)
    public void isAvailable_nullContext_shouldReturnFalse() {
        assertThat(mColorSectionController.isAvailable(/* context= */ null)).isFalse();
    }
}