Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 19 | import static org.junit.Assert.assertTrue; |
Jason Monk | 1fc931a | 2017-12-14 13:22:58 -0500 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.eq; |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 21 | import static org.mockito.Mockito.mock; |
Jason Monk | 1fc931a | 2017-12-14 13:22:58 -0500 | [diff] [blame] | 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 24 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper.RunWithLooper; |
| 29 | |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | |
| 32 | import com.android.systemui.SysuiTestCase; |
| 33 | import com.android.systemui.plugins.qs.QSTile.SlashState; |
| 34 | import com.android.systemui.qs.tileimpl.SlashImageView; |
| 35 | |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 38 | |
| 39 | @SmallTest |
| 40 | @RunWith(AndroidTestingRunner.class) |
| 41 | @RunWithLooper |
| 42 | public class SlashImageViewTest extends SysuiTestCase { |
| 43 | private TestableSlashImageView mSlashView; |
| 44 | |
| 45 | @Test |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 46 | public void testSetNonNullSlashStateCreatesSlashDrawable() { |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 47 | SlashState mockState = mock(SlashState.class); |
| 48 | Drawable mockDrawable = mock(Drawable.class); |
| 49 | mSlashView = new TestableSlashImageView(mContext); |
| 50 | assertTrue(mSlashView.getSlashDrawable() == null); |
| 51 | |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 52 | mSlashView.setState(mockState, mockDrawable); |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 53 | |
| 54 | assertTrue(mSlashView.getSlashDrawable() != null); |
| 55 | } |
| 56 | |
| 57 | @Test |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 58 | public void testSetNullSlashStateRemovesSlashDrawable() { |
| 59 | SlashState mockState = mock(SlashState.class); |
| 60 | Drawable mockDrawable = mock(Drawable.class); |
| 61 | mSlashView = new TestableSlashImageView(mContext); |
| 62 | mSlashView.setState(mockState, mockDrawable); |
| 63 | |
| 64 | assertTrue(mSlashView.getSlashDrawable() != null); |
| 65 | |
| 66 | mSlashView.setState(null, mockDrawable); |
| 67 | |
| 68 | assertTrue(mSlashView.getSlashDrawable() == null); |
| 69 | } |
| 70 | |
| 71 | @Test |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 72 | public void testSetNullDrawableRemovesSlashDrawable() { |
| 73 | SlashState mockState = mock(SlashState.class); |
| 74 | Drawable mockDrawable = mock(Drawable.class); |
| 75 | |
| 76 | mSlashView = new TestableSlashImageView(mContext); |
| 77 | mSlashView.setImageDrawable(mockDrawable); |
Evan Laird | 929bd54 | 2017-07-12 16:36:06 -0400 | [diff] [blame] | 78 | mSlashView.setState(mockState, mockDrawable); |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 79 | mSlashView.setImageDrawable(null); |
| 80 | |
| 81 | assertTrue(mSlashView.getSlashDrawable() == null); |
| 82 | } |
| 83 | |
Evan Laird | 9d4d73b | 2017-09-12 11:03:15 -0400 | [diff] [blame] | 84 | @Test |
| 85 | public void testSetImageDrawableUsesDrawableLevel() { |
| 86 | SlashImageView iv = new SlashImageView(mContext); |
| 87 | Drawable mockDrawable = mock(Drawable.class); |
Jason Monk | 1fc931a | 2017-12-14 13:22:58 -0500 | [diff] [blame] | 88 | when(mockDrawable.getLevel()).thenReturn(2); |
Evan Laird | 9d4d73b | 2017-09-12 11:03:15 -0400 | [diff] [blame] | 89 | |
| 90 | iv.setImageDrawable(mockDrawable); |
| 91 | |
| 92 | // Make sure setting the drawable didn't reset its level to 0 |
Jason Monk | 1fc931a | 2017-12-14 13:22:58 -0500 | [diff] [blame] | 93 | verify(mockDrawable).setLevel(eq(2)); |
Evan Laird | 9d4d73b | 2017-09-12 11:03:15 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Evan Laird | 418ffe4 | 2017-06-13 15:09:21 -0400 | [diff] [blame] | 96 | // Expose getSlashDrawable |
| 97 | private static class TestableSlashImageView extends SlashImageView { |
| 98 | TestableSlashImageView(Context c) { |
| 99 | super(c); |
| 100 | } |
| 101 | |
| 102 | private SlashDrawable getSlashDrawable() { |
| 103 | return mSlash; |
| 104 | } |
| 105 | } |
| 106 | } |