summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/video/HapticInterfaceManagerTest.java
blob: 6a7637ea618dd9f3c5d64c22c05a91620a3c1abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/***************************************************************************************************
 * Copyright © 2017 Xevo Inc.
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
 *    and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific prior written
 *    permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 **************************************************************************************************/
package com.smartdevicelink.managers.video;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.test.platform.app.InstrumentationRegistry;

import com.smartdevicelink.managers.ISdl;
import com.smartdevicelink.managers.lifecycle.OnSystemCapabilityListener;
import com.smartdevicelink.managers.lifecycle.SystemCapabilityManager;
import com.smartdevicelink.proxy.rpc.HapticRect;
import com.smartdevicelink.proxy.rpc.Rectangle;
import com.smartdevicelink.proxy.rpc.SendHapticData;
import com.smartdevicelink.proxy.rpc.VideoStreamingCapability;
import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created on 9/26/2017.
 */
@RunWith(MockitoJUnitRunner.Strict.class)
public class HapticInterfaceManagerTest extends TestCase {
    @Mock
    private ISdl interalInterface;

    @Captor
    private ArgumentCaptor<SendHapticData> captor;

    private HapticInterfaceManager hapticMgr;

    @Before
    public void setUp() throws Exception {
        hapticMgr = new HapticInterfaceManager(interalInterface);
    }

    @After
    public void tearDown() throws Exception {
        hapticMgr = null;
    }

    @Test
    public void testSetHapticData() throws Exception {
        List<HapticRect> rects = new ArrayList<>();
        Rectangle rect = new Rectangle();
        rect.setX(10f);
        rect.setY(10f);
        rect.setWidth(50f);
        rect.setHeight(20f);
        HapticRect hRect = new HapticRect();
        hRect.setRect(rect);
        rects.add(hRect);
        hapticMgr.setHapticData(rects);
        verify(interalInterface).sendRPC(any(SendHapticData.class));
    }

    @Test
    public void testRefreshHapticData() throws Exception {
        View root = createViews();
        hapticMgr.refreshHapticData(root);
        verify(interalInterface).sendRPC(captor.capture());
        SendHapticData data = captor.getValue();
        assertNotNull("SendHapticData RPC", data);
        List<HapticRect> list = data.getHapticRectData();
        assertNotNull("List", list);
        assertEquals("Haptic Rects", 4, list.size());
    }

    private View createViews() {

        View view = mock(View.class);

        ViewGroup parent1 = mock(ViewGroup.class);
        ViewGroup parent2 = mock(ViewGroup.class);

        when(parent1.getChildCount()).thenReturn(5);

        when(parent1.getChildAt(0)).thenReturn(view);
        when(parent1.getChildAt(1)).thenReturn(view);
        when(parent1.getChildAt(2)).thenReturn(view);
        when(parent1.getChildAt(3)).thenReturn(parent2);
        when(parent1.getChildAt(4)).thenReturn(view);

        when(parent2.getChildCount()).thenReturn(2);
        when(parent2.getChildAt(0)).thenReturn(view);
        when(parent2.getChildAt(1)).thenReturn(view);


        doAnswer(new Answer<Boolean>() {
            private int count = 0;

            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                int curCount = count++;
                return (curCount >= 1) && (curCount <= 4);
            }
        }).when(view).isClickable();

        return parent1;
    }


    private View createView(final int x, final int y, int w, int h) {
        View button = new Button(InstrumentationRegistry.getInstrumentation().getContext()) {
            @Override
            public void getLocationOnScreen(int[] outLocation) {
                super.getLocationOnScreen(outLocation);
                outLocation[0] = x;
                outLocation[1] = y;
            }
        };
        button.setClickable(true);
        button.setRight(w);
        button.setBottom(h);
        return button;
    }
}