summaryrefslogtreecommitdiffstats
path: root/remoting/host/capturer_helper_unittest.cc
blob: d1c9776499f045d655df23e4540d4f5bbf9ed491 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/capturer_helper.h"

#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

class CapturerHelperTest : public testing::Test {
 protected:
  CapturerHelper capturer_helper_;
};

bool Equals(const SkRegion& region1, const SkRegion& region2) {
  SkRegion::Iterator iter1(region1);
  SkRegion::Iterator iter2(region2);
  while (!iter1.done()) {
    SkIRect rect1 = iter1.rect();
    iter1.next();
    if (iter2.done()) {
      return false;
    }
    SkIRect rect2 = iter2.rect();
    iter2.next();
    if (rect1 != rect2) {
      return false;
    }
  }
  if (!iter2.done()) {
    return false;
  }
  return true;
}

TEST_F(CapturerHelperTest, ClearInvalidRegion) {
  SkRegion region;
  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(1, 2, 3, 4)));
  capturer_helper_.ClearInvalidRegion();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(region.isEmpty());
}

TEST_F(CapturerHelperTest, InvalidateRegion) {
  SkRegion region;
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeEmpty()), region));

  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(1, 2, 3, 4)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(1, 2, 3, 4)), region));

  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(1, 2, 3, 4)));
  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(4, 2, 3, 4)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(1, 2, 6, 4)), region));
}

TEST_F(CapturerHelperTest, InvalidateScreen) {
  SkRegion region;
  capturer_helper_.InvalidateScreen(SkISize::Make(12, 34));
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeWH(12, 34)), region));
}

TEST_F(CapturerHelperTest, InvalidateFullScreen) {
  SkRegion region;
  capturer_helper_.set_size_most_recent(SkISize::Make(12, 34));
  capturer_helper_.InvalidateFullScreen();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeWH(12, 34)), region));
}

TEST_F(CapturerHelperTest, SizeMostRecent) {
  ASSERT_EQ(SkISize::Make(0, 0), capturer_helper_.size_most_recent());
  capturer_helper_.set_size_most_recent(SkISize::Make(12, 34));
  ASSERT_EQ(SkISize::Make(12, 34), capturer_helper_.size_most_recent());
}

TEST_F(CapturerHelperTest, SetLogGridSize) {
  capturer_helper_.set_size_most_recent(SkISize::Make(10, 10));

  SkRegion region;
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeEmpty()), region));

  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)), region));

  capturer_helper_.SetLogGridSize(-1);
  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)), region));

  capturer_helper_.SetLogGridSize(0);
  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)), region));

  capturer_helper_.SetLogGridSize(1);
  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(6, 6, 2, 2)), region));

  capturer_helper_.SetLogGridSize(2);
  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(4, 4, 4, 4)), region));

  capturer_helper_.SetLogGridSize(0);
  capturer_helper_.InvalidateRegion(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)));
  region.setEmpty();
  capturer_helper_.SwapInvalidRegion(&region);
  ASSERT_TRUE(Equals(SkRegion(SkIRect::MakeXYWH(7, 7, 1, 1)), region));
}

void TestExpandRegionToGrid(const SkRegion& region, int log_grid_size,
                            const SkRegion& expandedRegionExpected) {
  scoped_ptr<SkRegion> expandedRegion1(
      CapturerHelper::ExpandToGrid(region, log_grid_size));
  ASSERT_TRUE(Equals(expandedRegionExpected, *expandedRegion1));
  scoped_ptr<SkRegion> expandedRegion2(
      CapturerHelper::ExpandToGrid(*expandedRegion1, log_grid_size));
  ASSERT_TRUE(Equals(*expandedRegion1, *expandedRegion2));
}

void TestExpandRectToGrid(int l, int t, int r, int b, int log_grid_size,
                          int lExpanded, int tExpanded,
                          int rExpanded, int bExpanded) {
  TestExpandRegionToGrid(SkRegion(SkIRect::MakeLTRB(l, t, r, b)), log_grid_size,
                         SkRegion(SkIRect::MakeLTRB(lExpanded, tExpanded,
                                                    rExpanded, bExpanded)));
}

TEST_F(CapturerHelperTest, ExpandToGrid) {
  const int LOG_GRID_SIZE = 4;
  const int GRID_SIZE = 1 << LOG_GRID_SIZE;
  for (int i = -2; i <= 2; i++) {
    int x = i * GRID_SIZE;
    for (int j = -2; j <= 2; j++) {
      int y = j * GRID_SIZE;
      TestExpandRectToGrid(x + 0, y + 0, x + 1, y + 1, LOG_GRID_SIZE,
                           x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE);
      TestExpandRectToGrid(x + 0, y + GRID_SIZE - 1, x + 1, y + GRID_SIZE,
                           LOG_GRID_SIZE,
                           x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE);
      TestExpandRectToGrid(x + GRID_SIZE - 1, y + GRID_SIZE - 1,
                           x + GRID_SIZE, y + GRID_SIZE, LOG_GRID_SIZE,
                           x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE);
      TestExpandRectToGrid(x + GRID_SIZE - 1, y + 0,
                           x + GRID_SIZE, y + 1, LOG_GRID_SIZE,
                           x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE);
      TestExpandRectToGrid(x - 1, y + 0, x + 1, y + 1, LOG_GRID_SIZE,
                           x - GRID_SIZE, y + 0, x + GRID_SIZE, y + GRID_SIZE);
      TestExpandRectToGrid(x - 1, y - 1, x + 1, y + 0, LOG_GRID_SIZE,
                           x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y);
      TestExpandRectToGrid(x + 0, y - 1, x + 1, y + 1, LOG_GRID_SIZE,
                           x, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE);
      TestExpandRectToGrid(x - 1, y - 1, x + 0, y + 1, LOG_GRID_SIZE,
                           x - GRID_SIZE, y - GRID_SIZE, x, y + GRID_SIZE);

      SkRegion region(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1));
      region.op(SkIRect::MakeLTRB(x - 1, y - 1, x + 0, y + 0),
                SkRegion::kDifference_Op);
      SkRegion expandedRegionExpected(SkIRect::MakeLTRB(
          x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE));
      expandedRegionExpected.op(
          SkIRect::MakeLTRB(x - GRID_SIZE, y - GRID_SIZE, x + 0, y + 0),
                            SkRegion::kDifference_Op);
      TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected);

      region.setRect(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1));
      region.op(SkIRect::MakeLTRB(x - 1, y + 0, x + 0, y + 1),
                SkRegion::kDifference_Op);
      expandedRegionExpected.setRect(SkIRect::MakeLTRB(
          x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE));
      expandedRegionExpected.op(
          SkIRect::MakeLTRB(x - GRID_SIZE, y + 0, x + 0, y + GRID_SIZE),
                            SkRegion::kDifference_Op);
      TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected);

      region.setRect(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1));
      region.op(SkIRect::MakeLTRB(x + 0, y + 0, x + 1, y + 1),
                SkRegion::kDifference_Op);
      expandedRegionExpected.setRect(SkIRect::MakeLTRB(
          x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE));
      expandedRegionExpected.op(
          SkIRect::MakeLTRB(x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE),
                            SkRegion::kDifference_Op);
      TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected);

      region.setRect(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1));
      region.op(SkIRect::MakeLTRB(x + 0, y - 1, x + 1, y + 0),
                SkRegion::kDifference_Op);
      expandedRegionExpected.setRect(SkIRect::MakeLTRB(
          x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE));
      expandedRegionExpected.op(
          SkIRect::MakeLTRB(x + 0, y - GRID_SIZE, x + GRID_SIZE, y + 0),
                            SkRegion::kDifference_Op);
      TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected);
    }
  }
}

}  // namespace remoting