tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

screen_capturer_helper_unittest.cc (8007B)


      1 /*
      2 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "modules/desktop_capture/screen_capturer_helper.h"
     12 
     13 #include "modules/desktop_capture/desktop_geometry.h"
     14 #include "modules/desktop_capture/desktop_region.h"
     15 #include "test/gtest.h"
     16 
     17 namespace webrtc {
     18 
     19 class ScreenCapturerHelperTest : public ::testing::Test {
     20 protected:
     21  ScreenCapturerHelper capturer_helper_;
     22 };
     23 
     24 TEST_F(ScreenCapturerHelperTest, ClearInvalidRegion) {
     25  DesktopRegion region(DesktopRect::MakeXYWH(1, 2, 3, 4));
     26  capturer_helper_.InvalidateRegion(region);
     27  capturer_helper_.ClearInvalidRegion();
     28  capturer_helper_.TakeInvalidRegion(&region);
     29  EXPECT_TRUE(region.is_empty());
     30 }
     31 
     32 TEST_F(ScreenCapturerHelperTest, InvalidateRegion) {
     33  DesktopRegion region;
     34  capturer_helper_.TakeInvalidRegion(&region);
     35  EXPECT_TRUE(region.is_empty());
     36 
     37  region.SetRect(DesktopRect::MakeXYWH(1, 2, 3, 4));
     38  capturer_helper_.InvalidateRegion(region);
     39  capturer_helper_.TakeInvalidRegion(&region);
     40  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(1, 2, 3, 4)).Equals(region));
     41 
     42  capturer_helper_.InvalidateRegion(
     43      DesktopRegion(DesktopRect::MakeXYWH(1, 2, 3, 4)));
     44  capturer_helper_.InvalidateRegion(
     45      DesktopRegion(DesktopRect::MakeXYWH(4, 2, 3, 4)));
     46  capturer_helper_.TakeInvalidRegion(&region);
     47  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(1, 2, 6, 4)).Equals(region));
     48 }
     49 
     50 TEST_F(ScreenCapturerHelperTest, InvalidateScreen) {
     51  DesktopRegion region;
     52  capturer_helper_.InvalidateScreen(DesktopSize(12, 34));
     53  capturer_helper_.TakeInvalidRegion(&region);
     54  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeWH(12, 34)).Equals(region));
     55 }
     56 
     57 TEST_F(ScreenCapturerHelperTest, SizeMostRecent) {
     58  EXPECT_TRUE(capturer_helper_.size_most_recent().is_empty());
     59  capturer_helper_.set_size_most_recent(DesktopSize(12, 34));
     60  EXPECT_TRUE(DesktopSize(12, 34).equals(capturer_helper_.size_most_recent()));
     61 }
     62 
     63 TEST_F(ScreenCapturerHelperTest, SetLogGridSize) {
     64  capturer_helper_.set_size_most_recent(DesktopSize(10, 10));
     65 
     66  DesktopRegion region;
     67  capturer_helper_.TakeInvalidRegion(&region);
     68  EXPECT_TRUE(DesktopRegion().Equals(region));
     69 
     70  capturer_helper_.InvalidateRegion(
     71      DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
     72  capturer_helper_.TakeInvalidRegion(&region);
     73  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
     74 
     75  capturer_helper_.SetLogGridSize(-1);
     76  capturer_helper_.InvalidateRegion(
     77      DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
     78  capturer_helper_.TakeInvalidRegion(&region);
     79  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
     80 
     81  capturer_helper_.SetLogGridSize(0);
     82  capturer_helper_.InvalidateRegion(
     83      DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
     84  capturer_helper_.TakeInvalidRegion(&region);
     85  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
     86 
     87  capturer_helper_.SetLogGridSize(1);
     88  capturer_helper_.InvalidateRegion(
     89      DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
     90  capturer_helper_.TakeInvalidRegion(&region);
     91 
     92  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(6, 6, 2, 2)).Equals(region));
     93 
     94  capturer_helper_.SetLogGridSize(2);
     95  capturer_helper_.InvalidateRegion(
     96      DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
     97  capturer_helper_.TakeInvalidRegion(&region);
     98  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(4, 4, 4, 4)).Equals(region));
     99 
    100  capturer_helper_.SetLogGridSize(0);
    101  capturer_helper_.InvalidateRegion(
    102      DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
    103  capturer_helper_.TakeInvalidRegion(&region);
    104  EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
    105 }
    106 
    107 void TestExpandRegionToGrid(const DesktopRegion& region,
    108                            int log_grid_size,
    109                            const DesktopRegion& expanded_region_expected) {
    110  DesktopRegion expanded_region1;
    111  ScreenCapturerHelper::ExpandToGrid(region, log_grid_size, &expanded_region1);
    112  EXPECT_TRUE(expanded_region_expected.Equals(expanded_region1));
    113 
    114  DesktopRegion expanded_region2;
    115  ScreenCapturerHelper::ExpandToGrid(expanded_region1, log_grid_size,
    116                                     &expanded_region2);
    117  EXPECT_TRUE(expanded_region1.Equals(expanded_region2));
    118 }
    119 
    120 void TestExpandRectToGrid(int l,
    121                          int t,
    122                          int r,
    123                          int b,
    124                          int log_grid_size,
    125                          int lExpanded,
    126                          int tExpanded,
    127                          int rExpanded,
    128                          int bExpanded) {
    129  TestExpandRegionToGrid(DesktopRegion(DesktopRect::MakeLTRB(l, t, r, b)),
    130                         log_grid_size,
    131                         DesktopRegion(DesktopRect::MakeLTRB(
    132                             lExpanded, tExpanded, rExpanded, bExpanded)));
    133 }
    134 
    135 TEST_F(ScreenCapturerHelperTest, ExpandToGrid) {
    136  const int kLogGridSize = 4;
    137  const int kGridSize = 1 << kLogGridSize;
    138  for (int i = -2; i <= 2; i++) {
    139    int x = i * kGridSize;
    140    for (int j = -2; j <= 2; j++) {
    141      int y = j * kGridSize;
    142      TestExpandRectToGrid(x + 0, y + 0, x + 1, y + 1, kLogGridSize, x + 0,
    143                           y + 0, x + kGridSize, y + kGridSize);
    144      TestExpandRectToGrid(x + 0, y + kGridSize - 1, x + 1, y + kGridSize,
    145                           kLogGridSize, x + 0, y + 0, x + kGridSize,
    146                           y + kGridSize);
    147      TestExpandRectToGrid(x + kGridSize - 1, y + kGridSize - 1, x + kGridSize,
    148                           y + kGridSize, kLogGridSize, x + 0, y + 0,
    149                           x + kGridSize, y + kGridSize);
    150      TestExpandRectToGrid(x + kGridSize - 1, y + 0, x + kGridSize, y + 1,
    151                           kLogGridSize, x + 0, y + 0, x + kGridSize,
    152                           y + kGridSize);
    153      TestExpandRectToGrid(x - 1, y + 0, x + 1, y + 1, kLogGridSize,
    154                           x - kGridSize, y + 0, x + kGridSize, y + kGridSize);
    155      TestExpandRectToGrid(x - 1, y - 1, x + 1, y + 0, kLogGridSize,
    156                           x - kGridSize, y - kGridSize, x + kGridSize, y);
    157      TestExpandRectToGrid(x + 0, y - 1, x + 1, y + 1, kLogGridSize, x,
    158                           y - kGridSize, x + kGridSize, y + kGridSize);
    159      TestExpandRectToGrid(x - 1, y - 1, x + 0, y + 1, kLogGridSize,
    160                           x - kGridSize, y - kGridSize, x, y + kGridSize);
    161 
    162      // Construct a region consisting of 3 pixels and verify that it's expanded
    163      // properly to 3 squares that are kGridSize by kGridSize.
    164      for (int q = 0; q < 4; ++q) {
    165        DesktopRegion region;
    166        DesktopRegion expanded_region_expected;
    167 
    168        if (q != 0) {
    169          region.AddRect(DesktopRect::MakeXYWH(x - 1, y - 1, 1, 1));
    170          expanded_region_expected.AddRect(DesktopRect::MakeXYWH(
    171              x - kGridSize, y - kGridSize, kGridSize, kGridSize));
    172        }
    173        if (q != 1) {
    174          region.AddRect(DesktopRect::MakeXYWH(x, y - 1, 1, 1));
    175          expanded_region_expected.AddRect(
    176              DesktopRect::MakeXYWH(x, y - kGridSize, kGridSize, kGridSize));
    177        }
    178        if (q != 2) {
    179          region.AddRect(DesktopRect::MakeXYWH(x - 1, y, 1, 1));
    180          expanded_region_expected.AddRect(
    181              DesktopRect::MakeXYWH(x - kGridSize, y, kGridSize, kGridSize));
    182        }
    183        if (q != 3) {
    184          region.AddRect(DesktopRect::MakeXYWH(x, y, 1, 1));
    185          expanded_region_expected.AddRect(
    186              DesktopRect::MakeXYWH(x, y, kGridSize, kGridSize));
    187        }
    188 
    189        TestExpandRegionToGrid(region, kLogGridSize, expanded_region_expected);
    190      }
    191    }
    192  }
    193 }
    194 
    195 }  // namespace webrtc