1 /***************************************************************************
2 *
3 * Copyright 2010-2014 BMW Car IT GmbH
4 * Copyright (C) 2012 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
5 * Copyright (C) 2016 Advanced Driver Information Technology Joint Venture GmbH
6 *
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 ****************************************************************************/
21
22 #include <gtest/gtest.h>
23 #include <stdio.h>
24
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include <iostream>
29
30 #include "TestBase.h"
31
32 extern "C" {
33 #include "ilm_control.h"
34 #include "ilm_input.h"
35 }
36
37 template <typename T>
contains(T const * actual,size_t as,T expected)38 bool contains(T const *actual, size_t as, T expected)
39 {
40 for (unsigned i = 0; i < as; i++)
41 if (actual[i] == expected)
42 return true;
43 return false;
44 }
45
46 class IlmInputTest : public TestBase, public ::testing::Test {
47 public:
SetUp()48 void SetUp()
49 {
50 ASSERT_EQ(ILM_SUCCESS, ilm_initWithNativedisplay((t_ilm_nativedisplay)wlDisplay));
51
52 iviSurfaces.reserve(10);
53 struct iviSurface surf;
54 for (int i = 0; i < (int)iviSurfaces.capacity(); ++i)
55 {
56 surf.surface = ivi_application_surface_create(iviApp, i+500, wlSurfaces[i]);
57 surf.surface_id = i+500;
58 iviSurfaces.push_back(surf);
59 }
60
61 wl_display_flush(wlDisplay);
62 }
63
TearDown()64 void TearDown()
65 {
66 //print_lmc_get_scene();
67 t_ilm_layer* layers = NULL;
68 t_ilm_int numLayer=0;
69 EXPECT_EQ(ILM_SUCCESS, ilm_getLayerIDs(&numLayer, &layers));
70 for (t_ilm_int i=0; i<numLayer; i++)
71 {
72 EXPECT_EQ(ILM_SUCCESS, ilm_layerRemove(layers[i]));
73 };
74 free(layers);
75
76 for (std::vector<iviSurface>::reverse_iterator it = iviSurfaces.rbegin();
77 it != iviSurfaces.rend();
78 ++it)
79 {
80 ivi_surface_destroy((*it).surface);
81 }
82 iviSurfaces.clear();
83
84 EXPECT_EQ(ILM_SUCCESS, ilm_commitChanges());
85 EXPECT_EQ(ILM_SUCCESS, ilm_destroy());
86 }
87 };
88
TEST_F(IlmInputTest,ilm_input_focus)89 TEST_F(IlmInputTest, ilm_input_focus) {
90 const uint32_t surfaceCount = 4;
91 t_ilm_surface surfaces[] = {iviSurfaces[0].surface_id,
92 iviSurfaces[1].surface_id,
93 iviSurfaces[2].surface_id,
94 iviSurfaces[3].surface_id};
95 t_ilm_surface *surfaceIDs;
96 ilmInputDevice *bitmasks;
97 t_ilm_uint num_ids;
98 t_ilm_uint layer = 0xbeef;
99
100 /* We have to add surfaces to a layer. Otherwise, they would not have a view.
101 We need a view to be able to set pointer focus. */
102 ASSERT_EQ(ILM_SUCCESS, ilm_layerCreateWithDimension(&layer, 800, 480));
103 EXPECT_EQ(ILM_SUCCESS, ilm_layerSetRenderOrder(layer, surfaces, surfaceCount));
104
105 ASSERT_EQ(ILM_SUCCESS, ilm_getInputFocus(&surfaceIDs, &bitmasks, &num_ids));
106 /* All the surfaces are returned */
107 ASSERT_EQ(num_ids, iviSurfaces.size());
108 int surfaces_found = 0;
109
110 for (unsigned int i = 0; i < num_ids; i++) {
111 /* The bitmasks all start unset */
112 EXPECT_EQ(bitmasks[i], 0);
113 if (contains(surfaceIDs, iviSurfaces.size(), iviSurfaces[i].surface_id))
114 surfaces_found++;
115 }
116 free(surfaceIDs);
117 free(bitmasks);
118 /* The surfaces returned are the correct ones */
119 ASSERT_EQ(surfaces_found, iviSurfaces.size());
120
121 /* Can set all focus to keyboard */
122 ASSERT_EQ(ILM_SUCCESS, ilm_setInputFocus(&surfaces[0], surfaceCount, ILM_INPUT_DEVICE_KEYBOARD, ILM_TRUE));
123
124 ASSERT_EQ(ILM_SUCCESS, ilm_getInputFocus(&surfaceIDs, &bitmasks, &num_ids));
125 for (unsigned int i = 0; i < num_ids; i++) {
126 /* All surfaces now have keyboard focus */
127 for (unsigned int j = 0; j < surfaceCount; j++) {
128 if (surfaceIDs[i] == surfaces[j])
129 EXPECT_EQ(bitmasks[i], ILM_INPUT_DEVICE_KEYBOARD);
130 }
131 }
132 free(surfaceIDs);
133 free(bitmasks);
134
135 /* Can remove keyboard focus from one surface */
136 ASSERT_EQ(ILM_SUCCESS, ilm_setInputFocus(&surfaces[0], 1, ILM_INPUT_DEVICE_KEYBOARD, ILM_FALSE));
137 ASSERT_EQ(ILM_SUCCESS, ilm_getInputFocus(&surfaceIDs, &bitmasks, &num_ids));
138 /* keyboard focus now removed for surfaces[0] */
139 for (unsigned int i = 0; i < num_ids; i++)
140 if (surfaceIDs[i] == surfaces[0])
141 EXPECT_EQ(bitmasks[i], 0);
142 free(surfaceIDs);
143 free(bitmasks);
144
145 /* Pointer focus set for surfaces[1] */
146 ASSERT_EQ(ILM_SUCCESS, ilm_setInputFocus(&surfaces[1], 1, ILM_INPUT_DEVICE_POINTER, ILM_TRUE));
147 ASSERT_EQ(ILM_SUCCESS, ilm_getInputFocus(&surfaceIDs, &bitmasks, &num_ids));
148 /* surfaces[1] now has pointer and keyboard focus */
149 for (unsigned int i = 0; i < num_ids; i++)
150 if (surfaceIDs[i] == surfaces[1])
151 EXPECT_EQ(bitmasks[i], ILM_INPUT_DEVICE_POINTER | ILM_INPUT_DEVICE_KEYBOARD);
152 free(surfaceIDs);
153 free(bitmasks);
154
155 /* Touch focus set for surfaces[2] */
156 ASSERT_EQ(ILM_SUCCESS, ilm_setInputFocus(&surfaces[2], 1, ILM_INPUT_DEVICE_TOUCH, ILM_TRUE));
157 ASSERT_EQ(ILM_SUCCESS, ilm_getInputFocus(&surfaceIDs, &bitmasks, &num_ids));
158 /* surfaces[2] now has keyboard and touch focus */
159 for (unsigned int i = 0; i < num_ids; i++)
160 if (surfaceIDs[i] == surfaces[2])
161 EXPECT_EQ(bitmasks[i], ILM_INPUT_DEVICE_KEYBOARD | ILM_INPUT_DEVICE_TOUCH);
162 free(surfaceIDs);
163 free(bitmasks);
164 }
165
TEST_F(IlmInputTest,ilm_input_event_acceptance)166 TEST_F(IlmInputTest, ilm_input_event_acceptance) {
167 t_ilm_surface surface1 = iviSurfaces[0].surface_id;
168 t_ilm_uint num_seats = 0;
169 t_ilm_string *seats = NULL;
170 char const *set_seats = "default";
171 t_ilm_uint set_seats_count = 1;
172
173 /* All seats accept the "default" seat when created */
174 ASSERT_EQ(ILM_SUCCESS, ilm_getInputAcceptanceOn(surface1, &num_seats,
175 &seats));
176 EXPECT_EQ(1, num_seats);
177 /* googletest doesn't like comparing to null pointers */
178 ASSERT_FALSE(seats == NULL);
179 EXPECT_STREQ("default", seats[0]);
180 free(seats[0]);
181 free(seats);
182
183 /* Can remove a seat from acceptance */
184 ASSERT_EQ(ILM_SUCCESS, ilm_setInputAcceptanceOn(surface1, 0, NULL));
185 ASSERT_EQ(ILM_SUCCESS, ilm_getInputAcceptanceOn(surface1, &num_seats,
186 &seats));
187 EXPECT_EQ(0, num_seats);
188 free(seats);
189
190 /* Can add a seat to acceptance */
191 ASSERT_EQ(ILM_SUCCESS, ilm_setInputAcceptanceOn(surface1, set_seats_count,
192 (t_ilm_string*)&set_seats));
193 ASSERT_EQ(ILM_SUCCESS, ilm_getInputAcceptanceOn(surface1, &num_seats,
194 &seats));
195 EXPECT_EQ(set_seats_count, num_seats);
196 bool found = false;
197 if (!strcmp(*seats, (t_ilm_string)set_seats))
198 found = true;
199 EXPECT_EQ(true, found) << set_seats[0] << " not found in returned seats";
200
201 free(seats[0]);
202 free(seats);
203
204 /* Seats can be set, unset, then reset */
205 ASSERT_EQ(ILM_SUCCESS, ilm_setInputAcceptanceOn(surface1, 1, (t_ilm_string*)&set_seats));
206 ASSERT_EQ(ILM_SUCCESS, ilm_setInputAcceptanceOn(surface1, 0, NULL));
207 ASSERT_EQ(ILM_SUCCESS, ilm_setInputAcceptanceOn(surface1, 1, (t_ilm_string*)&set_seats));
208 }
209