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
17package com.android.systemui.statusbar.notification;
18
19import static org.mockito.Mockito.mock;
20import static org.mockito.Mockito.verify;
21
22import android.support.test.runner.AndroidJUnit4;
23import android.test.suitebuilder.annotation.SmallTest;
24import android.testing.AndroidTestingRunner;
25import android.testing.TestableLooper.RunWithLooper;
26import android.widget.FrameLayout;
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
37@SmallTest
38@RunWith(AndroidTestingRunner.class)
39@RunWithLooper(setAsMainLooper = true)
40public class AboveShelfObserverTest extends SysuiTestCase {
41
42    private AboveShelfObserver mObserver;
43    private FrameLayout mHostLayout;
44    private NotificationTestHelper mNotificationTestHelper;
45    private AboveShelfObserver.HasViewAboveShelfChangedListener mListener;
46
47    @Before
48    public void setUp() throws Exception {
49        mNotificationTestHelper = new NotificationTestHelper(getContext());
50        mHostLayout = new FrameLayout(getContext());
51        mObserver = new AboveShelfObserver(mHostLayout);
52        ExpandableNotificationRow row = mNotificationTestHelper.createRow();
53        row.setAboveShelfChangedListener(mObserver);
54        mHostLayout.addView(row);
55        row = mNotificationTestHelper.createRow();
56        row.setAboveShelfChangedListener(mObserver);
57        mHostLayout.addView(row);
58        mListener = mock(AboveShelfObserver.HasViewAboveShelfChangedListener.class);
59    }
60
61    @Test
62    public void testObserverChangesWhenGoingAbove() {
63        ExpandableNotificationRow row = (ExpandableNotificationRow) mHostLayout.getChildAt(0);
64        mObserver.setListener(mListener);
65        row.setHeadsUp(true);
66        verify(mListener).onHasViewsAboveShelfChanged(true);
67    }
68
69    @Test
70    public void testObserverChangesWhenGoingBelow() {
71        ExpandableNotificationRow row = (ExpandableNotificationRow) mHostLayout.getChildAt(0);
72        row.setHeadsUp(true);
73        mObserver.setListener(mListener);
74        row.setHeadsUp(false);
75        verify(mListener).onHasViewsAboveShelfChanged(false);
76    }
77
78    @Test
79    public void testStaysAboveWhenOneGoesAway() {
80        ExpandableNotificationRow row = (ExpandableNotificationRow) mHostLayout.getChildAt(0);
81        row.setHeadsUp(true);
82        row = (ExpandableNotificationRow) mHostLayout.getChildAt(1);
83        row.setHeadsUp(true);
84        row.setHeadsUp(false);
85        Assert.assertTrue("There are still views above the shelf but removing one cleared it",
86                mObserver.hasViewsAboveShelf());
87    }
88}
89
90