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

ash / wm / multi_display / multi_display_metrics_controller_unittest.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/multi_display/multi_display_metrics_controller.h"

#include "ash/accessibility/magnifier/docked_magnifier_controller.h"
#include "ash/display/screen_orientation_controller_test_api.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/test/metrics/histogram_tester.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/wm/core/window_util.h"

namespace ash {

class MultiDisplayMetricsControllerTest : public AshTestBase {
 public:
  MultiDisplayMetricsControllerTest() = default;
  MultiDisplayMetricsControllerTest(const MultiDisplayMetricsControllerTest&) =
      delete;
  MultiDisplayMetricsControllerTest& operator=(
      const MultiDisplayMetricsControllerTest&) = delete;
  ~MultiDisplayMetricsControllerTest() override = default;

  void MaybeFireTimerNow() {
    base::OneShotTimer* timer =
        &Shell::Get()->multi_display_metrics_controller()->timer_;
    if (timer->IsRunning()) {
      timer->FireNow();
    }
  }

  // Helpers to move and resize a given window using the event generator.
  // Asserts that the window bounds have changed.
  void MoveWindow(aura::Window* window) {
    wm::ActivateWindow(window);

    const gfx::Rect old_bounds = window->GetBoundsInScreen();
    const gfx::Point point_on_header_in_screen(old_bounds.CenterPoint().x(),
                                               old_bounds.y() + 10);
    GetEventGenerator()->set_current_screen_location(point_on_header_in_screen);
    GetEventGenerator()->DragMouseBy(10, 10);
    const gfx::Rect expected_new_bounds = old_bounds + gfx::Vector2d(10, 10);
    ASSERT_EQ(expected_new_bounds, window->GetBoundsInScreen());
  }

  void ResizeWindow(aura::Window* window) {
    wm::ActivateWindow(window);

    const gfx::Rect old_bounds = window->GetBoundsInScreen();
    GetEventGenerator()->set_current_screen_location(old_bounds.bottom_right());
    GetEventGenerator()->DragMouseBy(5, 5);
    gfx::Rect expected_new_bounds(old_bounds);
    expected_new_bounds.Outset(gfx::Outsets::TLBR(0, 0, 5, 5));
    ASSERT_EQ(expected_new_bounds, window->GetBoundsInScreen());
  }

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();
    for (int i = 0; i < 4; ++i) {
      test_windows_.push_back(CreateAppWindow());
    }
  }

  void TearDown() override {
    test_windows_.clear();
    AshTestBase::TearDown();
  }

 protected:
  // Most tests in this suite use multiple windows per test. This vector stores
  // the needed app windows, which are resizable by default and would be in the
  // MRU list.
  std::vector<std::unique_ptr<aura::Window>> test_windows_;

  base::HistogramTester histogram_tester_;
};

TEST_F(MultiDisplayMetricsControllerTest, DisplayRotated) {
  // Rotate the display. Do a double rotation so our move and resize helpers
  // work properly.
  const int64_t display_id =
      display::Screen::GetScreen()->GetPrimaryDisplay().id();
  display::DisplayManager* display_manager = Shell::Get()->display_manager();
  display::test::ScopedSetInternalDisplayId set_internal(display_manager,
                                                         display_id);
  ScreenOrientationControllerTestApi orientation_test_api(
      Shell::Get()->screen_orientation_controller());
  orientation_test_api.SetDisplayRotation(
      display::Display::ROTATE_90, display::Display::RotationSource::ACTIVE);
  orientation_test_api.SetDisplayRotation(
      display::Display::ROTATE_0, display::Display::RotationSource::ACTIVE);

  MoveWindow(test_windows_[0].get());
  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kRotatedHistogram, static_cast<int>(true),
      1);
}

TEST_F(MultiDisplayMetricsControllerTest, DisplaySizeChanged) {
  UpdateDisplay("800x600");

  UpdateDisplay("2000x1000");
  MoveWindow(test_windows_[0].get());
  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kWorkAreaChangedHistogram,
      static_cast<int>(true), 1);
}

TEST_F(MultiDisplayMetricsControllerTest, DisplayWorkAreaChanged) {
  // We will use the docked magnifier to modify the work area in this test.
  DockedMagnifierController* docked_magnifier_controller =
      Shell::Get()->docked_magnifier_controller();
  docked_magnifier_controller->SetEnabled(true);
  ASSERT_GT(docked_magnifier_controller->GetMagnifierHeightForTesting(), 0);

  MoveWindow(test_windows_[0].get());

  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kWorkAreaChangedHistogram,
      static_cast<int>(true), 1);
}

// The following tests use display size change to test several edge cases. The
// logic between all the display changes is the same and display size changed is
// the simplest scenario to test.

// Tests that if no windows are moved or resized after a display change, the
// histogram reflects that.
TEST_F(MultiDisplayMetricsControllerTest, NoWindowsMovedOrResized) {
  UpdateDisplay("800x600");

  UpdateDisplay("2000x1000");
  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kWorkAreaChangedHistogram,
      static_cast<int>(false), 1);
}

TEST_F(MultiDisplayMetricsControllerTest, MultipleWindowsMoved) {
  UpdateDisplay("800x600");

  UpdateDisplay("2000x1000");

  // Move a couple of different windows after resizing a display. Test that we
  // only record it once.
  MoveWindow(test_windows_[0].get());
  MoveWindow(test_windows_[1].get());
  MoveWindow(test_windows_[2].get());
  MoveWindow(test_windows_[3].get());

  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kWorkAreaChangedHistogram,
      static_cast<int>(true), 1);
}

// Tests that resizing window after a display changes records properly.
TEST_F(MultiDisplayMetricsControllerTest, WindowsResized) {
  UpdateDisplay("800x600");

  UpdateDisplay("2000x1000");
  ResizeWindow(test_windows_[0].get());

  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kWorkAreaChangedHistogram,
      static_cast<int>(true), 1);
}

TEST_F(MultiDisplayMetricsControllerTest, MultipleDisplayChanges) {
  UpdateDisplay("800x600");

  UpdateDisplay("1200x800");
  UpdateDisplay("2000x1000");
  UpdateDisplay("3000x2000");

  // Move the window after changing the display size multiple times. Test that
  // we only record it once.
  MoveWindow(test_windows_[0].get());

  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kWorkAreaChangedHistogram,
      static_cast<int>(true), 1);
}

// Tests that windows that are added after a display change and then moved
// and/or resized do not count.
TEST_F(MultiDisplayMetricsControllerTest, WindowAddedAfterDisplayChangeMoved) {
  UpdateDisplay("800x600");

  UpdateDisplay("1200x800");

  // Add a new window after the display has changed and move the window.
  auto new_window = CreateAppWindow();
  MoveWindow(new_window.get());
  ResizeWindow(new_window.get());

  // Verify we don't record the window change, since it was added after the
  // display change.
  MaybeFireTimerNow();
  histogram_tester_.ExpectUniqueSample(
      MultiDisplayMetricsController::kWorkAreaChangedHistogram,
      static_cast<int>(false), 1);
}

}  // namespace ash