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

Commit 65f7a75f authored by Kyle Zhang's avatar Kyle Zhang Committed by Android (Google) Code Review
Browse files

Merge "Add test ForceL3FallbackPreferenceControllerTest" into main

parents 391f37f4 bd942cf0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ android_test {
        "kotlinx_coroutines_test",
        "flag-junit",
        "Settings-testutils2",
        "MediaDrmSettingsFlagsLib",
        // Don't add SettingsLib libraries here - you can use them directly as they are in the
        // instrumented Settings app.
    ],
+136 −0
Original line number Diff line number Diff line
/*
* Copyright (C) 2023 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.development.widevine;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.junit.Assume.assumeNoException;

import android.content.Context;
import android.media.MediaDrm;
import android.media.UnsupportedSchemeException;
import android.media.NotProvisionedException;
import android.sysprop.WidevineProperties;
import android.util.Log;
import android.content.Context;

import com.android.settings.media_drm.Flags;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

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

import java.util.UUID;

@RunWith(AndroidJUnit4.class)
public class ForceL3FallbackPreferenceControllerTest {

    private static final String PREF_KEY = "force_l3_fallback";
    private static final UUID WIDEVINE_UUID =
        new UUID(0xEDEF8BA979D64ACEL, 0xA3C827DCD51D21EDL);
    private static final String TAG = "ForceL3FallbackPreferenceControllerTest";

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private Context mContext;
    private ForceL3FallbackPreferenceController mController;
    private SwitchPreference mPreference;

    @Before
    public void setUp() {
        mContext = ApplicationProvider.getApplicationContext();
        mController = new ForceL3FallbackPreferenceController(mContext, PREF_KEY);
        mPreference = new SwitchPreference(mContext);
        WidevineProperties.forcel3_enabled(false);
    }

    @Test
    public void updateState_flagEnabled_checkPreference() {
        mSetFlagsRule.enableFlags(Flags.FLAG_FORCE_L3_ENABLED);
        mController.updateState(mPreference);
        assertThat(mPreference.isEnabled()).isTrue();
        assertThat(mPreference.isChecked()).isFalse();
        assertThat(WidevineProperties.forcel3_enabled().orElse(false)).isFalse();

        // Toggle to true
        mController.setChecked(true);
        mController.updateState(mPreference);
        assertThat(WidevineProperties.forcel3_enabled().orElse(false)).isTrue();
        assertThat(mPreference.isEnabled()).isTrue();
        assertThat(mPreference.isChecked()).isTrue();

        // Toggle to false
        mController.setChecked(false);
        mController.updateState(mPreference);
        assertThat(WidevineProperties.forcel3_enabled().orElse(false)).isFalse();
        assertThat(mPreference.isEnabled()).isTrue();
        assertThat(mPreference.isChecked()).isFalse();

        // Test flag rollback
        mController.setChecked(true);
        mController.updateState(mPreference);
        assertThat(mPreference.isChecked()).isTrue();
        assertThat(WidevineProperties.forcel3_enabled().orElse(false)).isTrue();
        mSetFlagsRule.disableFlags(Flags.FLAG_FORCE_L3_ENABLED);
        mController.updateState(mPreference);
        assertThat(mPreference.isEnabled()).isFalse();
        assertThat(mPreference.isChecked()).isFalse();
        assertThat(WidevineProperties.forcel3_enabled().orElse(false)).isFalse();
    }

    @Test
    public void updateState_flagDisabled_checkPreference() {
        mSetFlagsRule.disableFlags(Flags.FLAG_FORCE_L3_ENABLED);
        mController.updateState(mPreference);
        assertThat(mPreference.isEnabled()).isFalse();
    }

    @Test
    public void updateState_checkWidevine() throws Exception {
        MediaDrm drm;
        try {
            drm = new MediaDrm(WIDEVINE_UUID);
            assumeTrue(drm.getPropertyString("securityLevel").equals("L1"));
            mSetFlagsRule.enableFlags(Flags.FLAG_FORCE_L3_ENABLED);
            drm.close();
        } catch (UnsupportedSchemeException ex) {
            assumeNoException(ex);
        }

        // L3 enforced
        mController.setChecked(true);
        mController.updateState(mPreference);
        assertThat(WidevineProperties.forcel3_enabled().orElse(false)).isTrue();
        assertThat(mPreference.isEnabled()).isTrue();
        assertThat(mPreference.isChecked()).isTrue();
        drm = new MediaDrm(WIDEVINE_UUID);
        assertThat(drm.getPropertyString("securityLevel")).isEqualTo("L3");

        // Switch back to L1
        mController.setChecked(false);
        mController.updateState(mPreference);
        drm.close();
        drm = new MediaDrm(WIDEVINE_UUID);
        assertThat(drm.getPropertyString("securityLevel")).isEqualTo("L1");
    }
}