1 /*
2  * Copyright (C) 2022 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.accessibility.floatingmenu;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.res.Resources;
22 import android.testing.AndroidTestingRunner;
23 import android.testing.TestableLooper;
24 import android.view.WindowManager;
25 import android.widget.TextView;
26 
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.systemui.R;
30 import com.android.systemui.SysuiTestCase;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /** Tests for {@link MenuEduTooltipView}. */
37 @SmallTest
38 @RunWith(AndroidTestingRunner.class)
39 @TestableLooper.RunWithLooper
40 public class MenuEduTooltipViewTest extends SysuiTestCase {
41     private MenuViewAppearance mMenuViewAppearance;
42     private MenuEduTooltipView mMenuEduTooltipView;
43 
44     @Before
setUp()45     public void setUp() throws Exception {
46         final WindowManager windowManager = mContext.getSystemService(WindowManager.class);
47         mMenuViewAppearance = new MenuViewAppearance(mContext, windowManager);
48         mMenuEduTooltipView = new MenuEduTooltipView(mContext, mMenuViewAppearance);
49     }
50 
51     @Test
show_matchMessageText()52     public void show_matchMessageText() {
53         final CharSequence text = "Message";
54 
55         mMenuEduTooltipView.show(text);
56 
57         final TextView messageView = mMenuEduTooltipView.findViewById(R.id.text);
58         assertThat(messageView.getText().toString().contentEquals(text)).isTrue();
59     }
60 
61     @Test
show_menuOnLeft_onRightOfAnchor()62     public void show_menuOnLeft_onRightOfAnchor() {
63         mMenuViewAppearance.setPercentagePosition(
64                 new Position(/* percentageX= */ 0.0f, /* percentageY= */ 0.0f));
65         final CharSequence text = "Message";
66 
67         mMenuEduTooltipView.show(text);
68         final int tooltipViewX = (int) (mMenuViewAppearance.getMenuPosition().x
69                 + mMenuViewAppearance.getMenuWidth());
70 
71         assertThat(mMenuEduTooltipView.getX()).isEqualTo(tooltipViewX);
72     }
73 
74     @Test
show_menuCloseToLeftOfCenter_onLeftOfAnchor()75     public void show_menuCloseToLeftOfCenter_onLeftOfAnchor() {
76         mMenuViewAppearance.setPercentagePosition(
77                 new Position(/* percentageX= */ 0.4f, /* percentageY= */ 0.0f));
78         final CharSequence text = "Message";
79 
80         mMenuEduTooltipView.show(text);
81         final int tooltipViewX = (int) (mMenuViewAppearance.getMenuPosition().x
82                 + mMenuViewAppearance.getMenuWidth());
83 
84         assertThat(mMenuEduTooltipView.getX()).isEqualTo(tooltipViewX);
85     }
86 
87     @Test
show_menuOnRight_onLeftOfAnchor()88     public void show_menuOnRight_onLeftOfAnchor() {
89         mMenuViewAppearance.setPercentagePosition(
90                 new Position(/* percentageX= */ 1.0f, /* percentageY= */ 0.0f));
91         final Resources res = getContext().getResources();
92         final int arrowWidth =
93                 res.getDimensionPixelSize(R.dimen.accessibility_floating_tooltip_arrow_width);
94         final int arrowMargin =
95                 res.getDimensionPixelSize(R.dimen.accessibility_floating_tooltip_arrow_margin);
96         final CharSequence text = "Message";
97 
98         mMenuEduTooltipView.show(text);
99         final TextView messageView = mMenuEduTooltipView.findViewById(R.id.text);
100         final int layoutWidth = messageView.getMeasuredWidth() + arrowWidth + arrowMargin;
101 
102         assertThat(mMenuEduTooltipView.getX()).isEqualTo(
103                 mMenuViewAppearance.getMenuPosition().x - layoutWidth);
104     }
105 }
106