xdg_shell_surface_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/exo/xdg_shell_surface.h"
  5. #include "components/exo/buffer.h"
  6. #include "components/exo/display.h"
  7. #include "components/exo/shell_surface.h"
  8. #include "components/exo/surface.h"
  9. #include "components/exo/test/exo_test_base.h"
  10. #include "components/exo/test/exo_test_helper.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace exo {
  13. namespace {
  14. struct SurfaceTriplet {
  15. std::unique_ptr<Surface> surface;
  16. std::unique_ptr<ShellSurface> shell_surface;
  17. std::unique_ptr<Buffer> buffer;
  18. };
  19. class XdgShellSurfaceTest : public test::ExoTestBase {
  20. protected:
  21. SurfaceTriplet BuildSurface(int w, int h) {
  22. auto surface = std::make_unique<Surface>();
  23. auto shell_surface = std::make_unique<XdgShellSurface>(
  24. surface.get(), gfx::Point{0, 0},
  25. /*can_minimize=*/true, ash::desks_util::GetActiveDeskContainerId());
  26. auto buffer = std::make_unique<Buffer>(
  27. exo_test_helper()->CreateGpuMemoryBuffer({w, h}));
  28. surface->Attach(buffer.get());
  29. return {std::move(surface), std::move(shell_surface), std::move(buffer)};
  30. }
  31. // Returns the size of the surface associated with a maximized widget. If the
  32. // widget is |decorated| the size will be smaller due to the widget's
  33. // decorations.
  34. gfx::Size GetMaximizedSurfaceSize(bool decorated) {
  35. SurfaceTriplet temp = BuildSurface(1, 1);
  36. temp.surface->SetFrame(decorated ? SurfaceFrameType::NORMAL
  37. : SurfaceFrameType::NONE);
  38. temp.shell_surface->Maximize();
  39. temp.surface->Commit();
  40. EXPECT_TRUE(temp.shell_surface->GetWidget()->IsMaximized());
  41. return temp.shell_surface->GetWidget()->client_view()->size();
  42. }
  43. };
  44. // We don't actually care about the size of decorations. The purpose of this
  45. // test is to ensure that enabling decorations in the way that we do actually
  46. // causes the widget to be drawn with a (nonzero-sized) frame.
  47. TEST_F(XdgShellSurfaceTest, DecoratedSurfaceSmallerThanUndecorated) {
  48. gfx::Size undecorated_size = GetMaximizedSurfaceSize(false);
  49. gfx::Size decorated_size = GetMaximizedSurfaceSize(true);
  50. // The best expectation we can have is that the window decoration must be
  51. // nonzero in one direction.
  52. int decoration_width = undecorated_size.width() - decorated_size.width();
  53. int decoration_height = undecorated_size.height() - decorated_size.height();
  54. EXPECT_GE(decoration_width, 0);
  55. EXPECT_GE(decoration_height, 0);
  56. EXPECT_GT(decoration_width + decoration_height, 0);
  57. }
  58. TEST_F(XdgShellSurfaceTest, UndecoratedSurfaceAutoMaximizes) {
  59. gfx::Size maximized_size = GetMaximizedSurfaceSize(/*decorated=*/false);
  60. SurfaceTriplet max_surface =
  61. BuildSurface(maximized_size.width(), maximized_size.height());
  62. max_surface.surface->Commit();
  63. EXPECT_TRUE(max_surface.shell_surface->GetWidget()->IsMaximized());
  64. SurfaceTriplet narrow_surface =
  65. BuildSurface(maximized_size.width() - 1, maximized_size.height());
  66. narrow_surface.surface->Commit();
  67. EXPECT_FALSE(narrow_surface.shell_surface->GetWidget()->IsMaximized());
  68. SurfaceTriplet short_surface =
  69. BuildSurface(maximized_size.width(), maximized_size.height() - 1);
  70. short_surface.surface->Commit();
  71. EXPECT_FALSE(short_surface.shell_surface->GetWidget()->IsMaximized());
  72. }
  73. TEST_F(XdgShellSurfaceTest, DecoratedSurfaceAutoMaximizes) {
  74. gfx::Size maximized_size = GetMaximizedSurfaceSize(/*decorated=*/true);
  75. SurfaceTriplet max_surface =
  76. BuildSurface(maximized_size.width(), maximized_size.height());
  77. max_surface.surface->SetFrame(SurfaceFrameType::NORMAL);
  78. max_surface.surface->Commit();
  79. EXPECT_TRUE(max_surface.shell_surface->GetWidget()->IsMaximized());
  80. SurfaceTriplet narrow_surface =
  81. BuildSurface(maximized_size.width() - 1, maximized_size.height());
  82. narrow_surface.surface->SetFrame(SurfaceFrameType::NORMAL);
  83. narrow_surface.surface->Commit();
  84. EXPECT_FALSE(narrow_surface.shell_surface->GetWidget()->IsMaximized());
  85. SurfaceTriplet short_surface =
  86. BuildSurface(maximized_size.width(), maximized_size.height() - 1);
  87. short_surface.surface->SetFrame(SurfaceFrameType::NORMAL);
  88. short_surface.surface->Commit();
  89. EXPECT_FALSE(short_surface.shell_surface->GetWidget()->IsMaximized());
  90. }
  91. TEST_F(XdgShellSurfaceTest, DontMaximizeIfStateWasModified) {
  92. gfx::Size maximized_size = GetMaximizedSurfaceSize(/*decorated=*/true);
  93. SurfaceTriplet test_surface =
  94. BuildSurface(maximized_size.width() + 1, maximized_size.height() + 1);
  95. // Explicitly restoring the window should prevent auto maximize.
  96. test_surface.shell_surface->Restore();
  97. test_surface.surface->Commit();
  98. EXPECT_FALSE(test_surface.shell_surface->GetWidget()->IsMaximized());
  99. }
  100. } // namespace
  101. } // namespace exo