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

Commit 70cb0832 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Change SlashDrawable semantics to fix QS crash" into oc-dr1-dev

parents 807c9ae5 418ffe4a
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -18,12 +18,14 @@ import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.plugins.qs.QSTile.SlashState;
import com.android.systemui.qs.SlashDrawable;

public class SlashImageView extends ImageView {

    private SlashDrawable mSlash;
    @VisibleForTesting
    protected SlashDrawable mSlash;

    public SlashImageView(Context context) {
        super(context);
@@ -38,10 +40,13 @@ public class SlashImageView extends ImageView {

    @Override
    public void setImageDrawable(Drawable drawable) {
        if (mSlash != null) {
            mSlash.setDrawable(drawable);
        } else {
        if (drawable == null) {
            mSlash = null;
            super.setImageDrawable(null);
        } else if (mSlash == null) {
            super.setImageDrawable(drawable);
        } else {
            mSlash.setDrawable(drawable);
        }
    }

+76 −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.systemui.qs;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.test.filters.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper.RunWithLooper;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.plugins.qs.QSTile.SlashState;
import com.android.systemui.qs.tileimpl.SlashImageView;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;


@SmallTest
@RunWith(AndroidTestingRunner.class)
@RunWithLooper
public class SlashImageViewTest extends SysuiTestCase {
    private TestableSlashImageView mSlashView;

    @Test
    public void testSetSlashStateCreatesSlashDrawable() {
        SlashState mockState = mock(SlashState.class);
        Drawable mockDrawable = mock(Drawable.class);
        mSlashView = new TestableSlashImageView(mContext);
        assertTrue(mSlashView.getSlashDrawable() == null);

        mSlashView.setImageDrawable(mockDrawable);
        mSlashView.setState(mockState);

        assertTrue(mSlashView.getSlashDrawable() != null);
    }

    @Test
    public void testSetNullDrawableRemovesSlashDrawable() {
        SlashState mockState = mock(SlashState.class);
        Drawable mockDrawable = mock(Drawable.class);

        mSlashView = new TestableSlashImageView(mContext);
        mSlashView.setImageDrawable(mockDrawable);
        mSlashView.setState(mockState);
        mSlashView.setImageDrawable(null);

        assertTrue(mSlashView.getSlashDrawable() == null);
    }

    // Expose getSlashDrawable
    private static class TestableSlashImageView extends SlashImageView {
        TestableSlashImageView(Context c) {
            super(c);
        }

        private SlashDrawable getSlashDrawable() {
            return mSlash;
        }
    }
}