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

Commit 436873f1 authored by Bartosz Fabianowski's avatar Bartosz Fabianowski
Browse files

Add DynamicAvailabilityPreferenceController

This is a PreferenceController that correctly handles the case of
isAvailable() changing dynamically, while the Settings page containing
the preference in question is open.

Bug: 32692748
Test: m RunSettingsRoboTests

Change-Id: I95e22bbc7bdc17bacd2bf6b82fa5b535494d5bce
parent e11cb90e
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.core;

import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;

import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnResume;

public abstract class DynamicAvailabilityPreferenceController extends PreferenceController
        implements LifecycleObserver, OnResume {

    private Preference mPreference;
    private PreferenceScreen mScreen;

    public DynamicAvailabilityPreferenceController(Context context, Lifecycle lifecycle) {
        super(context);
        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        mScreen = screen;
        mPreference = screen.findPreference(getPreferenceKey());
        super.displayPreference(screen);
    }

    @Override
    public void onResume() {
        if (!isAvailable()) {
            removePreference(mScreen, getPreferenceKey());
            return;
        }

        updateState(mPreference);
        if (mScreen.findPreference(getPreferenceKey()) == null) {
            mScreen.addPreference(mPreference);
        }
    }
}
+140 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.core;

import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;

import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.lifecycle.Lifecycle;

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

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Tests for {@link DynamicAvailabilityPreferenceController}.
 */
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class DynamicAvailabilityPreferenceControllerTest {

    private final String PREFERENCE_KEY = "preference_key";

    private @Mock Context mContext;
    private @Mock Preference mPreference;
    private @Mock PreferenceScreen mScreen;
    private @Mock Lifecycle mLifecycle;

    private boolean mIsAvailable;
    private Preference mUpdatedPreference = null;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        when(mPreference.getKey()).thenReturn(PREFERENCE_KEY);
        when(mScreen.findPreference(PREFERENCE_KEY)).thenReturn(mPreference);
        when(mScreen.getPreferenceCount()).thenReturn(1);
        when(mScreen.getPreference(0)).thenReturn(mPreference);
    }

    @Test
    public void testAvailableToUnavailable() {
        mIsAvailable = true;

        final DynamicAvailabilityPreferenceController controller
                = new DynamicAvailabilityPreferenceControllerTestable(mLifecycle);
        verify(mLifecycle).addObserver(controller);

        controller.displayPreference(mScreen);
        verify(mScreen, never()).removePreference(mPreference);
        verify(mScreen, never()).addPreference(mPreference);
        assertThat(mUpdatedPreference).isNull();

        controller.onResume();
        verify(mScreen, never()).removePreference(mPreference);
        verify(mScreen, never()).addPreference(mPreference);
        assertThat(mUpdatedPreference).isEqualTo(mPreference);

        mUpdatedPreference = null;
        mIsAvailable = false;
        controller.onResume();
        verify(mScreen).removePreference(mPreference);
        verify(mScreen, never()).addPreference(mPreference);
        assertThat(mUpdatedPreference).isNull();
    }

    @Test
    public void testUnavailableToAvailable() {
        mIsAvailable = false;

        final DynamicAvailabilityPreferenceController controller
                = new DynamicAvailabilityPreferenceControllerTestable(mLifecycle);
        verify(mLifecycle).addObserver(controller);

        controller.displayPreference(mScreen);
        verify(mScreen).removePreference(mPreference);
        verify(mScreen, never()).addPreference(mPreference);
        assertThat(mUpdatedPreference).isNull();

        reset(mScreen);
        controller.onResume();
        verify(mScreen, never()).removePreference(mPreference);
        verify(mScreen, never()).addPreference(mPreference);
        assertThat(mUpdatedPreference).isNull();

        mIsAvailable = true;
        controller.onResume();
        verify(mScreen, never()).removePreference(mPreference);
        verify(mScreen).addPreference(mPreference);
        assertThat(mUpdatedPreference).isEqualTo(mPreference);
    }


    private class DynamicAvailabilityPreferenceControllerTestable
            extends DynamicAvailabilityPreferenceController {
        public DynamicAvailabilityPreferenceControllerTestable(Lifecycle lifecycle) {
            super(DynamicAvailabilityPreferenceControllerTest.this.mContext, lifecycle);
        }

        @Override
        public boolean isAvailable() {
            return mIsAvailable;
        }

        @Override
        public void updateState(Preference preference) {
            mUpdatedPreference = preference;
        }

        @Override
        public String getPreferenceKey() {
            return PREFERENCE_KEY;
        }
    }
}