1 /*
2  * Copyright (C) 2023 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 package com.android.launcher3.celllayout;
17 
18 import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP;
19 import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import androidx.test.ext.junit.runners.AndroidJUnit4;
24 import androidx.test.filters.SmallTest;
25 
26 import com.android.launcher3.celllayout.CellPosMapper.CellPos;
27 import com.android.launcher3.celllayout.CellPosMapper.TwoPanelCellPosMapper;
28 import com.android.launcher3.model.data.ItemInfo;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 /**
34  * Tests for {@link CellPosMapper}
35  */
36 @SmallTest
37 @RunWith(AndroidJUnit4.class)
38 public class CellPosMapperTest {
39 
40     @Test
testMapModelToPresenter_default()41     public void testMapModelToPresenter_default() {
42         CellPosMapper mapper = CellPosMapper.DEFAULT;
43         assertThat(mapper.mapModelToPresenter(
44                 createInfo(0, 0, 0, CONTAINER_DESKTOP))).isEqualTo(new CellPos(0, 0, 0));
45         assertThat(mapper.mapModelToPresenter(
46                 createInfo(0, 0, 1, CONTAINER_DESKTOP))).isEqualTo(new CellPos(0, 0, 1));
47         assertThat(mapper.mapModelToPresenter(
48                 createInfo(5, 0, 1, CONTAINER_DESKTOP))).isEqualTo(new CellPos(5, 0, 1));
49         assertThat(mapper.mapModelToPresenter(
50                 createInfo(5, 0, 0, CONTAINER_DESKTOP))).isEqualTo(new CellPos(5, 0, 0));
51 
52         assertThat(mapper.mapModelToPresenter(
53                 createInfo(0, 0, 0, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(0, 0, 0));
54         assertThat(mapper.mapModelToPresenter(
55                 createInfo(0, 0, 1, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(0, 0, 1));
56         assertThat(mapper.mapModelToPresenter(
57                 createInfo(5, 0, 1, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(5, 0, 1));
58         assertThat(mapper.mapModelToPresenter(
59                 createInfo(5, 0, 0, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(5, 0, 0));
60     }
61 
62     @Test
testMapPresenterToModel_default()63     public void testMapPresenterToModel_default() {
64         CellPosMapper mapper = CellPosMapper.DEFAULT;
65         assertThat(mapper.mapPresenterToModel(
66                 0, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 0));
67         assertThat(mapper.mapPresenterToModel(
68                 0, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 1));
69         assertThat(mapper.mapPresenterToModel(
70                 5, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 1));
71         assertThat(mapper.mapPresenterToModel(
72                 5, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 0));
73 
74         assertThat(mapper.mapPresenterToModel(
75                 0, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
76         assertThat(mapper.mapPresenterToModel(
77                 0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
78         assertThat(mapper.mapPresenterToModel(
79                 5, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
80         assertThat(mapper.mapPresenterToModel(
81                 5, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
82     }
83 
84     @Test
testMapModelToPresenter_twoPanel()85     public void testMapModelToPresenter_twoPanel() {
86         CellPosMapper mapper = new TwoPanelCellPosMapper(8);
87         assertThat(mapper.mapModelToPresenter(
88                 createInfo(0, 0, 0, CONTAINER_DESKTOP))).isEqualTo(new CellPos(0, 0, 0));
89         assertThat(mapper.mapModelToPresenter(
90                 createInfo(0, 0, 1, CONTAINER_DESKTOP))).isEqualTo(new CellPos(8, 0, 0));
91         assertThat(mapper.mapModelToPresenter(
92                 createInfo(5, 0, 1, CONTAINER_DESKTOP))).isEqualTo(new CellPos(13, 0, 0));
93         assertThat(mapper.mapModelToPresenter(
94                 createInfo(5, 0, 0, CONTAINER_DESKTOP))).isEqualTo(new CellPos(5, 0, 0));
95 
96         assertThat(mapper.mapModelToPresenter(
97                 createInfo(0, 0, 0, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(0, 0, 0));
98         assertThat(mapper.mapModelToPresenter(
99                 createInfo(0, 0, 1, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(0, 0, 1));
100         assertThat(mapper.mapModelToPresenter(
101                 createInfo(5, 0, 1, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(5, 0, 1));
102         assertThat(mapper.mapModelToPresenter(
103                 createInfo(5, 0, 0, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(5, 0, 0));
104     }
105 
106     @Test
testMapPresenterToModel_twoPanel()107     public void testMapPresenterToModel_twoPanel() {
108         CellPosMapper mapper = new TwoPanelCellPosMapper(3);
109         assertThat(mapper.mapPresenterToModel(
110                 0, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 0));
111         assertThat(mapper.mapPresenterToModel(
112                 0, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 1));
113         assertThat(mapper.mapPresenterToModel(
114                 5, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 1));
115         assertThat(mapper.mapPresenterToModel(
116                 5, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(2, 0, 1));
117 
118         assertThat(mapper.mapPresenterToModel(
119                 0, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
120         assertThat(mapper.mapPresenterToModel(
121                 0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
122         assertThat(mapper.mapPresenterToModel(
123                 5, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
124         assertThat(mapper.mapPresenterToModel(
125                 5, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
126     }
127 
128     @Test
testMapPresenterToModel_VerticalHotseat()129     public void testMapPresenterToModel_VerticalHotseat() {
130         CellPosMapper mapper = new CellPosMapper(true, 6);
131         assertThat(mapper.mapPresenterToModel(
132                 0, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 0));
133         assertThat(mapper.mapPresenterToModel(
134                 0, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 1));
135         assertThat(mapper.mapPresenterToModel(
136                 5, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 1));
137         assertThat(mapper.mapPresenterToModel(
138                 5, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 0));
139 
140         assertThat(mapper.mapPresenterToModel(
141                 0, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 5));
142         assertThat(mapper.mapPresenterToModel(
143                 0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 5));
144         assertThat(mapper.mapPresenterToModel(
145                 0, 5, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 5, 0));
146         assertThat(mapper.mapPresenterToModel(
147                 0, 5, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 5, 0));
148     }
149 
createInfo(int cellX, int cellY, int screen, int container)150     private ItemInfo createInfo(int cellX, int cellY, int screen, int container) {
151         ItemInfo info = new ItemInfo();
152         info.cellX = cellX;
153         info.cellY = cellY;
154         info.screenId = screen;
155         info.container = container;
156         return info;
157     }
158 }
159