/*
 * Copyright 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 androidx.webkit;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.os.Build;

import androidx.core.graphics.ColorUtils;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.MediumTest;
import androidx.test.filters.SdkSuppress;

import org.junit.Test;
import org.junit.runner.RunWith;


@MediumTest
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public class WebSettingsCompatLightThemeTest extends
        WebSettingsCompatDarkModeTestBase<WebViewLightThemeTestActivity> {
    public WebSettingsCompatLightThemeTest() {
        // targetSdkVersion to T, it is min version the algorithmic darkening works.
        // TODO(http://b/214741472): Use VERSION_CODES.TIRAMISU once available.
        super(WebViewLightThemeTestActivity.class, 33);
    }

    /**
     * Test the algorithmic darkening is disabled by default.
     */
    @Test
    public void testSimplifiedDarkMode_default() throws Throwable {
        WebkitUtils.checkFeature(WebViewFeature.ALGORITHMIC_DARKENING);

        assertFalse("Algorithmic darkening should be disallowed by default",
                WebSettingsCompat.isAlgorithmicDarkeningAllowed(getSettingsOnUiThread()));
    }

    /**
     * Test web content is always light regardless the algorithmic darkening is allowed or not.
     */
    @SdkSuppress(maxSdkVersion = 32) // b/254572377
    @Test
    public void testSimplifiedDarkMode_rendersLight() throws Throwable {
        WebkitUtils.checkFeature(WebViewFeature.ALGORITHMIC_DARKENING);
        WebkitUtils.checkFeature(WebViewFeature.OFF_SCREEN_PRERASTER);
        setWebViewSize();
        // Loading about:blank which doesn't support dark style result in a light background.
        getWebViewOnUiThread().loadUrlAndWaitForCompletion("about:blank");
        assertTrue("Bitmap colour should be light",
                ColorUtils.calculateLuminance(getWebPageColor()) > 0.5f);
        assertFalse("WebView should always prefers light color scheme in light theme app",
                prefersDarkTheme());
        // Allowing algorithmic darkening in dark theme app should result in a dark background.
        WebSettingsCompat.setAlgorithmicDarkeningAllowed(
                getSettingsOnUiThread(), true);
        getWebViewOnUiThread().loadUrlAndWaitForCompletion("about:blank");
        assertTrue("Bitmap colour should be dark",
                ColorUtils.calculateLuminance(getWebPageColor()) > 0.5f);
        assertFalse("WebView should always prefers light color scheme in light theme app",
                prefersDarkTheme());
    }

    /**
     * Test web content is always light (if supported) on the light theme app.
     */
    @SdkSuppress(maxSdkVersion = 32) // b/254572377
    @Test
    public void testSimplifiedDarkMode_pageSupportDarkTheme() {
        WebkitUtils.checkFeature(WebViewFeature.ALGORITHMIC_DARKENING);
        WebkitUtils.checkFeature(WebViewFeature.OFF_SCREEN_PRERASTER);
        setWebViewSize();

        // Loading about:blank which doesn't support dark style result in a light background.
        getWebViewOnUiThread().loadUrlAndWaitForCompletion("about:blank");
        assertTrue("Bitmap colour should be light",
                ColorUtils.calculateLuminance(getWebPageColor()) > 0.5f);
        assertFalse("WebView should always prefers light color scheme in light theme app",
                prefersDarkTheme());

        // Loading a page with dark-theme support should result in a light background (as
        // specified in media-query) in light theme app.
        getWebViewOnUiThread().loadDataAndWaitForCompletion(mDarkThemeSupport, "text/html",
                "base64");
        assertTrue("Bitmap colour should be light",
                ColorUtils.calculateLuminance(getWebPageColor()) > 0.5f);
        assertFalse("WebView should always prefers light color scheme in light theme app",
                prefersDarkTheme());
    }
}

@@ -2012,60 +2012,8 @@
  */%0A
-    @SdkSuppress(maxSdkVersion = 32) // b/254572377%0A
     
@@ -2010,32 +2010,32 @@
    */%0A    @Test%0A
+
     public void 
@@ -2260,16 +2260,257 @@
 Size();%0A
+        float expected_luminance = 0.5f;%0A        // Theme.AppCompat.Light has a darker background in api 33 than previous.%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.TIRAMISU) %7B%0A            expected_luminance = 0.4f;%0A        %7D%0A
         
@@ -2781,36 +2781,50 @@
 bPageColor()) %3E 
-0.5f
+expected_luminance
 );%0A        asser
@@ -2983,20 +2983,21 @@
 ning in 
-dark
+light
  theme a
@@ -3010,35 +3010,27 @@
 uld 
-result in a dark background
+not take any effect
 .%0A  
@@ -3252,20 +3252,21 @@
 ould be 
-dark
+light
 %22,%0A     
@@ -3319,36 +3319,50 @@
 bPageColor()) %3E 
-0.5f
+expected_luminance
 );%0A        asser
@@ -3582,60 +3582,8 @@
  */%0A
-    @SdkSuppress(maxSdkVersion = 32) // b/254572377%0A
     
@@ -3818,24 +3818,265 @@
 iewSize();%0A%0A
+        float expected_luminance = 0.5f;%0A        // Theme.AppCompat.Light has a darker background in api 33 than previous.%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.TIRAMISU) %7B%0A            expected_luminance = 0.4f;%0A        %7D%0A
         // L
@@ -4343,36 +4343,50 @@
 bPageColor()) %3E 
-0.5f
+expected_luminance
 );%0A        asser
@@ -4886,20 +4886,34 @@
 or()) %3E 
-0.5f
+expected_luminance
 );%0A     

Fix SimplifiedDarkMode light theme tests