1/*
2 * Copyright (C) 2018 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
17package com.android.systemui.statusbar.stack;
18
19import static org.mockito.Mockito.atLeast;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.verify;
22
23import android.support.test.filters.SmallTest;
24import android.testing.AndroidTestingRunner;
25import android.testing.TestableLooper.RunWithLooper;
26import android.view.View;
27
28import com.android.systemui.SysuiTestCase;
29import com.android.systemui.statusbar.ExpandableNotificationRow;
30import com.android.systemui.statusbar.NotificationTestHelper;
31
32import org.junit.Assert;
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
37import java.util.HashSet;
38
39@SmallTest
40@RunWith(AndroidTestingRunner.class)
41@RunWithLooper(setAsMainLooper = true)
42public class NotificationRoundnessManagerTest extends SysuiTestCase {
43
44    private NotificationRoundnessManager mRoundnessManager = new NotificationRoundnessManager();
45    private HashSet<View> mAnimatedChildren = new HashSet<>();
46    private Runnable mRoundnessCallback = mock(Runnable.class);
47    private ExpandableNotificationRow mFirst;
48    private ExpandableNotificationRow mSecond;
49
50    @Before
51    public void setUp() throws Exception {
52        NotificationTestHelper testHelper = new NotificationTestHelper(getContext());
53        mFirst = testHelper.createRow();
54        mFirst.setHeadsUpAnimatingAwayListener(animatingAway
55                -> mRoundnessManager.onHeadsupAnimatingAwayChanged(mFirst, animatingAway));
56        mSecond = testHelper.createRow();
57        mSecond.setHeadsUpAnimatingAwayListener(animatingAway
58                -> mRoundnessManager.onHeadsupAnimatingAwayChanged(mSecond, animatingAway));
59        mRoundnessManager.setOnRoundingChangedCallback(mRoundnessCallback);
60        mRoundnessManager.setAnimatedChildren(mAnimatedChildren);
61        mRoundnessManager.setFirstAndLastBackgroundChild(mFirst, mFirst);
62        mRoundnessManager.setExpanded(1.0f, 1.0f);
63    }
64
65    @Test
66    public void testCallbackCalledWhenSecondChanged() {
67        mRoundnessManager.setFirstAndLastBackgroundChild(mFirst, mSecond);
68        verify(mRoundnessCallback, atLeast(1)).run();
69    }
70
71    @Test
72    public void testCallbackCalledWhenFirstChanged() {
73        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mFirst);
74        verify(mRoundnessCallback, atLeast(1)).run();
75    }
76
77    @Test
78    public void testRoundnessSetOnLast() {
79        mRoundnessManager.setFirstAndLastBackgroundChild(mFirst, mSecond);
80        Assert.assertEquals(1.0f, mSecond.getCurrentBottomRoundness(), 0.0f);
81        Assert.assertEquals(0.0f, mSecond.getCurrentTopRoundness(), 0.0f);
82    }
83
84    @Test
85    public void testRoundnessSetOnNew() {
86        mRoundnessManager.setFirstAndLastBackgroundChild(mFirst, null);
87        Assert.assertEquals(0.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
88        Assert.assertEquals(1.0f, mFirst.getCurrentTopRoundness(), 0.0f);
89    }
90
91    @Test
92    public void testCompleteReplacement() {
93        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
94        Assert.assertEquals(0.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
95        Assert.assertEquals(0.0f, mFirst.getCurrentTopRoundness(), 0.0f);
96    }
97
98    @Test
99    public void testNotCalledWhenRemoved() {
100        mFirst.setRemoved();
101        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
102        Assert.assertEquals(1.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
103        Assert.assertEquals(1.0f, mFirst.getCurrentTopRoundness(), 0.0f);
104    }
105
106    @Test
107    public void testRoundedWhenPinnedAndCollapsed() {
108        mFirst.setPinned(true);
109        mRoundnessManager.setExpanded(0.0f /* expandedHeight */, 0.0f /* appearFraction */);
110        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
111        Assert.assertEquals(1.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
112        Assert.assertEquals(1.0f, mFirst.getCurrentTopRoundness(), 0.0f);
113    }
114
115    @Test
116    public void testRoundedWhenGoingAwayAndCollapsed() {
117        mFirst.setHeadsUpAnimatingAway(true);
118        mRoundnessManager.setExpanded(0.0f /* expandedHeight */, 0.0f /* appearFraction */);
119        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
120        Assert.assertEquals(1.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
121        Assert.assertEquals(1.0f, mFirst.getCurrentTopRoundness(), 0.0f);
122    }
123
124    @Test
125    public void testRoundedNormalRoundingWhenExpanded() {
126        mFirst.setHeadsUpAnimatingAway(true);
127        mRoundnessManager.setExpanded(1.0f /* expandedHeight */, 0.0f /* appearFraction */);
128        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
129        Assert.assertEquals(0.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
130        Assert.assertEquals(0.0f, mFirst.getCurrentTopRoundness(), 0.0f);
131    }
132
133    @Test
134    public void testTrackingHeadsUpRoundedIfPushingUp() {
135        mRoundnessManager.setExpanded(1.0f /* expandedHeight */, -0.5f /* appearFraction */);
136        mRoundnessManager.setTrackingHeadsUp(mFirst);
137        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
138        Assert.assertEquals(1.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
139        Assert.assertEquals(1.0f, mFirst.getCurrentTopRoundness(), 0.0f);
140    }
141
142    @Test
143    public void testTrackingHeadsUpNotRoundedIfPushingDown() {
144        mRoundnessManager.setExpanded(1.0f /* expandedHeight */, 0.5f /* appearFraction */);
145        mRoundnessManager.setTrackingHeadsUp(mFirst);
146        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
147        Assert.assertEquals(0.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
148        Assert.assertEquals(0.0f, mFirst.getCurrentTopRoundness(), 0.0f);
149    }
150
151    @Test
152    public void testRoundingUpdatedWhenAnimatingAwayTrue() {
153        mRoundnessManager.setExpanded(0.0f, 0.0f);
154        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
155        mFirst.setHeadsUpAnimatingAway(true);
156        Assert.assertEquals(1.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
157        Assert.assertEquals(1.0f, mFirst.getCurrentTopRoundness(), 0.0f);
158    }
159
160
161    @Test
162    public void testRoundingUpdatedWhenAnimatingAwayFalse() {
163        mRoundnessManager.setExpanded(0.0f, 0.0f);
164        mRoundnessManager.setFirstAndLastBackgroundChild(mSecond, mSecond);
165        mFirst.setHeadsUpAnimatingAway(true);
166        mFirst.setHeadsUpAnimatingAway(false);
167        Assert.assertEquals(0.0f, mFirst.getCurrentBottomRoundness(), 0.0f);
168        Assert.assertEquals(0.0f, mFirst.getCurrentTopRoundness(), 0.0f);
169    }
170}
171