File: test-head.c

package info (click to toggle)
phosh 0.53.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 9,868 kB
  • sloc: ansic: 83,377; xml: 3,981; python: 717; sh: 449; makefile: 34; lisp: 22; javascript: 6
file content (143 lines) | stat: -rw-r--r-- 3,990 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2021 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#include "monitor/head-priv.h"

PhoshHeadMode small_mode = {
  .width = 1024,
  .height = 768,
};

PhoshHeadMode fourk_mode = {
  .width = 3840,
  .height = 2160,
};

static PhoshHead *
stub_head_new (PhoshHeadMode *mode)
{
  /* We don't use `phosh_head_new()` so we don't have to bother about wayland protocols, etc */
  PhoshHead *head = g_new0 (PhoshHead, 1);

  head->pending.scale = 1.0;
  head->pending.mode = mode;
  return head;
}

static void
stub_head_destroy (PhoshHead *head)
{
  g_free (head);
}


static GPtrArray *
get_layout (float scale)
{
  PhoshHead *head;
  PhoshHead *other_head;
  GPtrArray *heads;

  head = stub_head_new (&small_mode);
  head->pending.scale = scale;

  other_head = stub_head_new (&fourk_mode);
  heads = g_ptr_array_new_with_free_func ((GDestroyNotify) (stub_head_destroy));

  g_assert_nonnull (head->pending.mode);
  g_assert_nonnull (other_head->pending.mode);

  g_ptr_array_add (heads, head);
  g_ptr_array_add (heads, other_head);

  return heads;
}


static void
test_phosh_head_set_transform (void)
{
  PhoshHead *head;
  PhoshHead *other_head;
  GPtrArray *heads;

  /* other_head right of head */
  for (float scale = 1.0; scale <= 2.0; scale += 0.5) {
    heads = get_layout (scale);
    head = g_ptr_array_index (heads, 0);
    other_head = g_ptr_array_index (heads, 1);

    other_head->pending.x = head->pending.mode->width / scale;
    g_assert_cmpint (head->pending.x, ==, 0);
    g_assert_cmpint (head->pending.y, ==, 0);
    g_assert_cmpint (other_head->pending.x, ==, small_mode.width / scale);
    g_assert_cmpint (other_head->pending.y, ==, 0);

    phosh_head_set_pending_transform (head, PHOSH_MONITOR_TRANSFORM_90, heads);
    g_assert_cmpint (head->pending.x, ==, 0);
    g_assert_cmpint (head->pending.y, ==, 0);
    g_assert_cmpint (head->pending.transform, ==, PHOSH_MONITOR_TRANSFORM_90);
    g_assert_cmpint (other_head->pending.x, ==, small_mode.height / scale);
    g_assert_cmpint (other_head->pending.y, ==, 0);

    g_ptr_array_free (heads, TRUE);
  }

  /* other_head below head */
  heads = get_layout (1.0);
  head = g_ptr_array_index (heads, 0);
  other_head = g_ptr_array_index (heads, 1);

  other_head->pending.y = head->pending.mode->height;
  g_assert_cmpint (head->pending.x, ==, 0);
  g_assert_cmpint (head->pending.y, ==, 0);
  g_assert_cmpint (other_head->pending.x, ==, 0);
  g_assert_cmpint (other_head->pending.y, ==, small_mode.height);

  phosh_head_set_pending_transform (head, PHOSH_MONITOR_TRANSFORM_90, heads);
  g_assert_cmpint (head->pending.x, ==, 0);
  g_assert_cmpint (head->pending.y, ==, 0);
  g_assert_cmpint (head->pending.transform, ==, PHOSH_MONITOR_TRANSFORM_90);
  g_assert_cmpint (other_head->pending.x, ==, 0);
  g_assert_cmpint (other_head->pending.y, ==, small_mode.width);

  g_ptr_array_free (heads, TRUE);

  /* other_head left of head */
  heads = get_layout (1.0);
  head = g_ptr_array_index (heads, 0);
  other_head = g_ptr_array_index (heads, 1);

  head->pending.x = other_head->pending.mode->width;
  g_assert_cmpint (head->pending.x, ==, fourk_mode.width);
  g_assert_cmpint (head->pending.y, ==, 0);
  g_assert_cmpint (other_head->pending.x, ==, 0);
  g_assert_cmpint (other_head->pending.y, ==, 0);

  phosh_head_set_pending_transform (head, PHOSH_MONITOR_TRANSFORM_90, heads);
  /* Nothing should have changed */
  g_assert_cmpint (head->pending.x, ==, fourk_mode.width);
  g_assert_cmpint (head->pending.y, ==, 0);
  g_assert_cmpint (head->pending.transform, ==, PHOSH_MONITOR_TRANSFORM_90);
  g_assert_cmpint (other_head->pending.x, ==, 0);
  g_assert_cmpint (other_head->pending.y, ==, 0);

  g_ptr_array_free (heads, TRUE);
}


int
main (int   argc,
      char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func("/phosh/head/layout/set_transform", test_phosh_head_set_transform);

  return g_test_run();
}