23
23
* Authored by: Sam Spilsbury <sam.spilsbury@canonical.com>
26
27
#include <test-constrain-to-workarea.h>
27
28
#include <constrain-to-workarea.h>
28
29
#include <iostream>
29
30
#include <stdlib.h>
32
class CompPlaceTestConstrainToWorkarea :
33
namespace cw = compiz::window;
34
namespace cwe = cw::extents;
35
namespace cp = compiz::place;
37
using ::testing::WithParamInterface;
38
using ::testing::ValuesIn;
39
using ::testing::Combine;
41
class PlaceClampGeometryToWorkArea :
33
42
public CompPlaceTest
46
PlaceClampGeometryToWorkArea () :
47
screensize (1000, 2000),
48
workArea (50, 50, 900, 1900),
51
memset (&extents, 0, sizeof (cwe::Extents));
37
TEST_F(CompPlaceTestConstrainToWorkarea, TestConstrainToWorkarea)
39
CompSize screensize (1000, 2000);
40
CompRect workArea (50, 50, 900, 1900);
41
compiz::window::Geometry g (100, 100, 200, 200, 0);
42
compiz::window::extents::Extents extents;
43
unsigned int flags = 0;
45
memset (&extents, 0, sizeof (compiz::window::extents::Extents));
65
const cw::Geometry LimitOfAllowedGeometry (50, 50, 900, 1900, 0);
72
std::ostream & operator<<(std::ostream &os, const Geometry &g)
74
return os << "Window Geometry: (" << g.x () <<
77
", " << g.height () <<
78
", " << g.border () << ")";
82
std::ostream & operator<<(std::ostream &os, const Extents &e)
84
return os << "Window Extents: (left: " << e.left <<
85
", right: " << e.right <<
87
", bottom: " << e.bottom << ")";
93
std::ostream & operator<<(std::ostream &os, const CompPoint &p)
95
return os << "Point: (" << p.x () << ", " << p.y () << ")";
98
TEST_F (PlaceClampGeometryToWorkArea, NoConstrainmentRequired)
100
g = cw::Geometry (100, 100, 200, 200, 0);
48
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
50
EXPECT_EQ (g, compiz::window::Geometry (100, 100, 200, 200, 0));
103
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
105
EXPECT_EQ (g, cw::Geometry (100, 100, 200, 200, 0));
108
TEST_F (PlaceClampGeometryToWorkArea, LargerThanWorkAreaConstrainsToWorkAreaSize)
52
110
/* Larger than workArea */
53
g = compiz::window::Geometry (50, 50, 950, 1950, 0);
55
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
57
EXPECT_EQ (g, compiz::window::Geometry (50, 50, 900, 1900, 0));
111
g = cw::Geometry (50, 50, 950, 1950, 0);
113
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
115
EXPECT_EQ (g, LimitOfAllowedGeometry);
118
TEST_F (PlaceClampGeometryToWorkArea, OutsideTopLeftConstrainment)
59
120
/* Outside top left */
60
g = compiz::window::Geometry (0, 0, 900, 1900, 0);
62
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
64
EXPECT_EQ (g, compiz::window::Geometry (50, 50, 900, 1900, 0));
121
g = cw::Geometry (0, 0, 900, 1900, 0);
123
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
125
EXPECT_EQ (g, LimitOfAllowedGeometry);
128
TEST_F (PlaceClampGeometryToWorkArea, OutsideTopRightConstrainment)
66
130
/* Outside top right */
67
g = compiz::window::Geometry (100, 0, 900, 1900, 0);
69
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
71
EXPECT_EQ (g, compiz::window::Geometry (50, 50, 900, 1900, 0));
131
g = cw::Geometry (100, 0, 900, 1900, 0);
133
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
135
EXPECT_EQ (g, LimitOfAllowedGeometry);
138
TEST_F (PlaceClampGeometryToWorkArea, OutsideBottomLeftConstrainment)
73
140
/* Outside bottom left */
74
g = compiz::window::Geometry (0, 100, 900, 1900, 0);
76
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
78
EXPECT_EQ (g, compiz::window::Geometry (50, 50, 900, 1900, 0));
141
g = cw::Geometry (0, 100, 900, 1900, 0);
143
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
145
EXPECT_EQ (g, LimitOfAllowedGeometry);
148
TEST_F (PlaceClampGeometryToWorkArea, OutsideBottomRightConstrainment)
80
150
/* Outside bottom right */
81
g = compiz::window::Geometry (100, 100, 900, 1900, 0);
83
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
85
EXPECT_EQ (g, compiz::window::Geometry (50, 50, 900, 1900, 0));
151
g = cw::Geometry (100, 100, 900, 1900, 0);
153
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
155
EXPECT_EQ (g, LimitOfAllowedGeometry);
158
TEST_F (PlaceClampGeometryToWorkArea, NoChangePositionIfSizeUnchanged)
87
160
/* For the size only case, we should not
88
161
* change the position of the window if
89
162
* the size does not change */
90
g = compiz::window::Geometry (0, 0, 900, 1900, 0);
91
flags = compiz::place::clampGeometrySizeOnly;
93
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
95
EXPECT_EQ (g, compiz::window::Geometry (0, 0, 900, 1900, 0));
97
g = compiz::window::Geometry (0, 0, 1000, 2000, 0);
98
flags = compiz::place::clampGeometrySizeOnly;
100
compiz::place::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
102
EXPECT_EQ (g, compiz::window::Geometry (50, 50, 900, 1900, 0));
163
g = cw::Geometry (0, 0, 900, 1900, 0);
164
flags = cp::clampGeometrySizeOnly;
166
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
168
EXPECT_EQ (g, cw::Geometry (0, 0, 900, 1900, 0));
171
TEST_F (PlaceClampGeometryToWorkArea, ChangePositionIfSizeChanged)
173
g = cw::Geometry (0, 0, 1000, 2000, 0);
174
flags = cp::clampGeometrySizeOnly;
176
cp::clampGeometryToWorkArea (g, workArea, extents, flags, screensize);
178
EXPECT_EQ (g, LimitOfAllowedGeometry);
183
typedef std::tr1::tuple <cw::Geometry, cwe::Extents> ConstrainPositionToWorkAreaParam;
185
const cw::Geometry & WindowGeometry (const ConstrainPositionToWorkAreaParam &p)
187
return std::tr1::get <0> (p);
190
const cwe::Extents & WindowExtents (const ConstrainPositionToWorkAreaParam &p)
192
return std::tr1::get <1> (p);
195
CompPoint InitialPosition (const ConstrainPositionToWorkAreaParam &p)
197
/* Initial position is where the window is right now */
198
return (std::tr1::get <0> (p)).pos ();
201
const CompRect WArea (50, 50, 900, 1900);
202
const CompPoint ExpectedPosition (WArea.pos ());
205
class PlaceConstrainPositionToWorkArea :
206
public CompPlaceTest,
207
public WithParamInterface <ConstrainPositionToWorkAreaParam>
211
PlaceConstrainPositionToWorkArea ()
213
memset (&extents, 0, sizeof (cwe::Extents));
220
cwe::Extents extents;
223
TEST_P (PlaceConstrainPositionToWorkArea, PositionConstrainedWithExtents)
225
g = WindowGeometry (GetParam ());
226
extents = WindowExtents (GetParam ());
228
CompPoint pos = InitialPosition (GetParam ());
229
pos = cp::constrainPositionToWorkArea (pos, g, extents, WArea);
231
const CompPoint expectedAfterExtentsAdjustment = ExpectedPosition +
232
CompPoint (extents.left,
235
EXPECT_EQ (expectedAfterExtentsAdjustment, pos);
240
cwe::Extents PossibleExtents[] =
242
cwe::Extents (0, 0, 0, 0),
243
cwe::Extents (1, 0, 0, 0),
244
cwe::Extents (0, 1, 0, 0),
245
cwe::Extents (0, 0, 1, 0),
246
cwe::Extents (0, 0, 0, 1)
249
cw::Geometry PossibleGeometries[] =
251
cw::Geometry (WArea.x (), WArea.y (),
252
WArea.width (), WArea.height (), 0),
253
cw::Geometry (WArea.x () - 1, WArea.y (),
254
WArea.width (), WArea.height (), 0),
255
cw::Geometry (WArea.x (), WArea.y () - 1,
256
WArea.width (), WArea.height (), 0),
257
cw::Geometry (WArea.x () + 1, WArea.y (),
258
WArea.width (), WArea.height (), 0),
259
cw::Geometry (WArea.x (), WArea.y () + 1,
260
WArea.width (), WArea.height (), 0)
264
INSTANTIATE_TEST_CASE_P (PlacementData,
265
PlaceConstrainPositionToWorkArea,
266
Combine (ValuesIn (PossibleGeometries),
267
ValuesIn (PossibleExtents)));
268
class PlaceGetEdgePositions :
273
PlaceGetEdgePositions () :
274
geom (100, 200, 300, 400, 1),
287
TEST_F (PlaceGetEdgePositions, GetEdgePositions)
289
int left = geom.x () - border.left;
290
int right = left + (geom.widthIncBorders ()) +
291
(border.left + border.right);
292
int top = geom.y () - border.top;
293
int bottom = top + (geom.heightIncBorders ()) +
294
(border.top + border.bottom);
296
const cwe::Extents ExpectedExtents (left, right, top, bottom);
297
cwe::Extents actualExtents (cp::getWindowEdgePositions (pos,
301
EXPECT_EQ (ExpectedExtents, actualExtents);
306
const CompSize SCREEN_SIZE (1000, 1000);
307
const unsigned int WINDOW_SIZE = 250;
310
TEST (PlaceGetViewportRelativeCoordinates, WithinScreenWidth)
312
cw::Geometry geom (SCREEN_SIZE.width () / 2,
313
SCREEN_SIZE.height () / 2,
314
WINDOW_SIZE, WINDOW_SIZE,
317
CompPoint position (cp::getViewportRelativeCoordinates (geom,
319
EXPECT_EQ (geom.pos ().x (), position.x ());
322
TEST (PlaceGetViewportRelativeCoordinates, WithinScreenHeight)
324
cw::Geometry geom (SCREEN_SIZE.width () / 2,
325
SCREEN_SIZE.height () / 2,
326
WINDOW_SIZE, WINDOW_SIZE,
329
CompPoint position (cp::getViewportRelativeCoordinates (geom,
331
EXPECT_EQ (geom.pos ().y (), position.y ());
334
TEST (PlaceGetViewportRelativeCoordinates, OutsideOuterScreenWidth)
336
cw::Geometry geom (SCREEN_SIZE.width () + 1,
337
SCREEN_SIZE.height () / 2,
338
WINDOW_SIZE, WINDOW_SIZE,
341
CompPoint position (cp::getViewportRelativeCoordinates (geom,
343
EXPECT_EQ (1, position.x ());
346
TEST (PlaceGetViewportRelativeCoordinates, OutsideInnerScreenWidth)
348
cw::Geometry geom (-(WINDOW_SIZE + 1),
349
SCREEN_SIZE.height () / 2,
350
WINDOW_SIZE, WINDOW_SIZE,
353
CompPoint position (cp::getViewportRelativeCoordinates (geom,
355
EXPECT_EQ (SCREEN_SIZE.width () - (WINDOW_SIZE + 1),
359
TEST (PlaceGetViewportRelativeCoordinates, OutsideOuterScreenHeight)
361
cw::Geometry geom (SCREEN_SIZE.width () / 2,
362
SCREEN_SIZE.height () + 1,
363
WINDOW_SIZE, WINDOW_SIZE,
366
CompPoint position (cp::getViewportRelativeCoordinates (geom,
368
EXPECT_EQ (1, position.y ());
371
TEST (PlaceGetViewportRelativeCoordinates, OutsideInnerScreenHeight)
373
cw::Geometry geom (SCREEN_SIZE.width () / 2,
375
WINDOW_SIZE, WINDOW_SIZE,
378
CompPoint position (cp::getViewportRelativeCoordinates (geom,
380
EXPECT_EQ (SCREEN_SIZE.height () - (WINDOW_SIZE + 1),
386
const CompRect WORK_AREA (25, 25, 1000, 1000);
388
const cwe::Extents EdgePositions[] =
391
cwe::Extents (WORK_AREA.left (), WORK_AREA.right (),
392
WORK_AREA.top (), WORK_AREA.bottom ()),
394
cwe::Extents (WORK_AREA.left () + 1, WORK_AREA.right () - 1,
395
WORK_AREA.top () + 1, WORK_AREA.bottom () - 1),
396
/* Just outside right */
397
cwe::Extents (WORK_AREA.left () + 1, WORK_AREA.right () + 1,
398
WORK_AREA.top (), WORK_AREA.bottom ()),
399
/* Just outside left */
400
cwe::Extents (WORK_AREA.left () - 1, WORK_AREA.right () - 1,
401
WORK_AREA.top (), WORK_AREA.bottom ()),
402
/* Just outside top */
403
cwe::Extents (WORK_AREA.left (), WORK_AREA.right (),
404
WORK_AREA.top () - 1, WORK_AREA.bottom () - 1),
405
/* Just outside bottom */
406
cwe::Extents (WORK_AREA.left (), WORK_AREA.right (),
407
WORK_AREA.top () + 1, WORK_AREA.bottom () + 1),
411
class PlaceClampEdgePositions :
412
public CompPlaceTest,
413
public WithParamInterface <CompWindowExtents>
417
TEST_P (PlaceClampEdgePositions, WithinWorkAreaWidth)
419
CompWindowExtents edgePositions (GetParam ());
420
cp::clampHorizontalEdgePositionsToWorkArea (edgePositions,
423
const int edgePositionsWidth = edgePositions.right - edgePositions.left;
425
ASSERT_GT (edgePositionsWidth, 0);
426
EXPECT_LE (edgePositionsWidth, WORK_AREA.width ());
429
TEST_P (PlaceClampEdgePositions, OutsideLeft)
431
CompWindowExtents edgePositions (GetParam ());
432
cp::clampHorizontalEdgePositionsToWorkArea (edgePositions,
435
ASSERT_GE (edgePositions.left, WORK_AREA.left ());
436
ASSERT_GE (edgePositions.right, WORK_AREA.left ());
439
TEST_P (PlaceClampEdgePositions, OutsideRight)
441
CompWindowExtents edgePositions (GetParam ());
442
cp::clampHorizontalEdgePositionsToWorkArea (edgePositions,
445
ASSERT_LE (edgePositions.left, WORK_AREA.right ());
446
ASSERT_LE (edgePositions.right, WORK_AREA.right ());
449
TEST_P (PlaceClampEdgePositions, WithinWorkAreaHeight)
451
CompWindowExtents edgePositions (GetParam ());
452
cp::clampVerticalEdgePositionsToWorkArea (edgePositions,
455
const int edgePositionsHeight = edgePositions.bottom - edgePositions.top;
457
ASSERT_GT (edgePositionsHeight, 0);
458
EXPECT_LE (edgePositionsHeight, WORK_AREA.height ());
461
TEST_P (PlaceClampEdgePositions, OutsideTop)
463
CompWindowExtents edgePositions (GetParam ());
464
cp::clampVerticalEdgePositionsToWorkArea (edgePositions,
467
ASSERT_GE (edgePositions.top, WORK_AREA.top ());
468
ASSERT_GE (edgePositions.bottom, WORK_AREA.top ());
471
TEST_P (PlaceClampEdgePositions, OutsideBottom)
473
CompWindowExtents edgePositions (GetParam ());
474
cp::clampVerticalEdgePositionsToWorkArea (edgePositions,
477
ASSERT_LE (edgePositions.top, WORK_AREA.bottom ());
478
ASSERT_LE (edgePositions.bottom, WORK_AREA.bottom ());
481
INSTANTIATE_TEST_CASE_P (WAEdgePositions, PlaceClampEdgePositions,
482
ValuesIn (EdgePositions));
484
TEST (PlaceSubtractBordersFromEdgePositions, NormalGravity)
486
const CompWindowExtents borders (1, 2, 3, 4);
487
const CompWindowExtents edgePositions (100, 200, 100, 200);
488
const unsigned int legacyBorder = 1;
490
CompWindowExtents expectedEdgePositions (edgePositions.left + (borders.left),
491
edgePositions.right - (borders.right + 2 * legacyBorder),
492
edgePositions.top + (borders.top),
493
edgePositions.bottom - (borders.bottom + 2 * legacyBorder));
495
CompWindowExtents modifiedEdgePositions (edgePositions);
497
cp::subtractBordersFromEdgePositions (modifiedEdgePositions,
501
EXPECT_EQ (expectedEdgePositions, modifiedEdgePositions);
504
TEST (PlaceSizeAndPositionChanged, PositionChangedReturnsFalse)
506
EXPECT_FALSE (cp::onlySizeChanged (CWX | CWY));
509
TEST (PlaceSizeAndPositionChanged, SizeChangedReturnsTrue)
511
EXPECT_TRUE (cp::onlySizeChanged (CWWidth | CWHeight));
514
TEST (PlaceSizeAndPositionChanged, XAndWidthChangedReturnsFalse)
516
EXPECT_FALSE (cp::onlySizeChanged (CWX | CWWidth));
519
TEST (PlaceSizeAndPositionChanged, YAndHeightChangedReturnsFalse)
521
EXPECT_FALSE (cp::onlySizeChanged (CWY | CWHeight));
524
TEST (PlaceApplyWidthChange, ReturnFalseIfNoChange)
526
CompWindowExtents edgePositions (100, 200, 100, 200);
529
unsigned int mask = CWWidth;
530
EXPECT_FALSE (cp::applyWidthChange(edgePositions, xwc, mask));
533
TEST (PlaceApplyWidthChange, ApplyWidthAndSetFlag)
535
CompWindowExtents edgePositions (100, 200, 100, 200);
537
unsigned int mask = 0;
538
ASSERT_TRUE (cp::applyWidthChange(edgePositions, xwc, mask));
539
EXPECT_EQ (edgePositions.right - edgePositions.left, xwc.width);
540
EXPECT_EQ (CWWidth, mask);
543
TEST (PlaceApplyHeightChange, ReturnFalseIfNoChange)
545
CompWindowExtents edgePositions (100, 200, 100, 200);
548
unsigned int mask = CWHeight;
549
EXPECT_FALSE (cp::applyHeightChange(edgePositions, xwc, mask));
552
TEST (PlaceApplyWidthChange, ApplyHeightAndSetFlag)
554
CompWindowExtents edgePositions (100, 200, 100, 200);
556
unsigned int mask = 0;
557
ASSERT_TRUE (cp::applyHeightChange(edgePositions, xwc, mask));
558
EXPECT_EQ (edgePositions.bottom - edgePositions.top, xwc.height);
559
EXPECT_EQ (CWHeight, mask);