chromium/chrome/browser/ui/window_sizer/window_sizer_unittest.cc

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

#include "chrome/browser/ui/window_sizer/window_sizer_common_unittest.h"

#include "base/compiler_specific.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
const int kWindowTilePixels =;
}

// Test that the window is sized appropriately for the first run experience
// where the default window bounds calculation is invoked.
TEST(WindowSizerTest, DefaultSizeCase) {}

// Test that the next opened window is positioned appropriately given the
// bounds of an existing window of the same type.
TEST(WindowSizerTest, LastWindowBoundsCase) {}

// Test that the window opened is sized appropriately given persisted sizes.
TEST(WindowSizerTest, PersistedBoundsCase) {}

#if !BUILDFLAG(IS_MAC)
// Tests that there is no crash and the window is sized and positioned correctly
// with extremely small work area and bounds.
TEST(WindowSizerTest, TinyWorkAreaAndBounds) {}
#endif  // !BUILDFLAG(IS_MAC)

//////////////////////////////////////////////////////////////////////////////
// The following unittests have different results on Mac/non-Mac because we
// reposition windows aggressively on Mac.  The *WithAggressiveReposition tests
// are run on Mac, and the *WithNonAggressiveRepositioning tests are run on
// other platforms.

#if BUILDFLAG(IS_MAC)
TEST(WindowSizerTest, LastWindowOffscreenWithAggressiveRepositioning) {
  { // taskbar on left.  The new window overlaps slightly with the taskbar, so
    // it is moved to be flush with the left edge of the work area.
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithMonitorWorkArea(taskbar_left_work_area)
            .WithLastActiveBounds(
                gfx::Rect(kWindowTilePixels, kWindowTilePixels, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(taskbar_left_work_area.x(),
                        kWindowTilePixels * 2, 500, 400), window_bounds);
  }

  { // offset would put the new window offscreen at the bottom
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithLastActiveBounds(gfx::Rect(10, 729, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(10 + kWindowTilePixels,
                        0 /* not 729 + kWindowTilePixels */,
                        500, 400),
              window_bounds);
  }

  { // offset would put the new window offscreen at the right
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithLastActiveBounds(gfx::Rect(985, 10, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not 985 + kWindowTilePixels*/,
                        10 + kWindowTilePixels,
                        500, 400),
              window_bounds);
  }

  { // offset would put the new window offscreen at the bottom right
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithLastActiveBounds(gfx::Rect(985, 729, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not 985 + kWindowTilePixels*/,
                        0 /* not 729 + kWindowTilePixels*/,
                        500, 400),
              window_bounds);
  }
}

TEST(WindowSizerTest, PersistedWindowOffscreenWithAggressiveRepositioning) {
  { // off the left
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(-471, 50, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not -471 */, 50, 500, 400), window_bounds);
  }

  { // off the top
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(50, -370, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(50, 0, 500, 400), window_bounds);
  }

  { // off the right
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(995, 50, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not 995 */, 50, 500, 400), window_bounds);
  }

  { // off the bottom
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(50, 739, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(50, 0 /* not 739 */, 500, 400), window_bounds);
  }

  { // off the topleft
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(-471, -371, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not -471 */, 0 /* not -371 */, 500, 400),
              window_bounds);
  }

  { // off the topright
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(995, -371, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not 995 */, 0 /* not -371 */, 500, 400),
                        window_bounds);
  }

  { // off the bottomleft
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(-471, 739, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not -471 */, 0 /* not 739 */, 500, 400),
                        window_bounds);
  }

  { // off the bottomright
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(995, 739, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not 995 */, 0 /* not 739 */, 500, 400),
                        window_bounds);
  }

  { // entirely off left
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(-700, 50, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not -700 */, 50, 500, 400), window_bounds);
  }

  { // entirely off left (monitor was detached since last run)
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(-700, 50, 500, 400))
            .WithPersistedWorkArea(left_s1024x768)
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0, 50, 500, 400), window_bounds);
  }

  { // entirely off top
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(50, -500, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(50, 0, 500, 400), window_bounds);
  }

  { // entirely off top (monitor was detached since last run)
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(50, -500, 500, 400))
            .WithPersistedWorkArea(top_s1024x768)
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(50, 0, 500, 400), window_bounds);
  }

  { // entirely off right
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(1200, 50, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not 1200 */, 50, 500, 400), window_bounds);
  }

  { // entirely off right (monitor was detached since last run)
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(1200, 50, 500, 400))
            .WithPersistedWorkArea(right_s1024x768)
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(524 /* not 1200 */, 50, 500, 400), window_bounds);
  }

  { // entirely off bottom
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(50, 800, 500, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(50, 0 /* not 800 */, 500, 400), window_bounds);
  }

  { // entirely off bottom (monitor was detached since last run)
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(50, 800, 500, 400))
            .WithPersistedWorkArea(bottom_s1024x768)
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(50, 368 /* not 800 */, 500, 400), window_bounds);
  }

  { // wider than the screen. off both the left and right
    gfx::Rect window_bounds =
        WindowSizerTestUtil()
            .WithMonitorBounds(p1024x768)
            .WithPersistedBounds(gfx::Rect(-100, 50, 2000, 400))
            .GetWindowBounds();
    EXPECT_EQ(gfx::Rect(0 /* not -100 */, 50, 2000, 400), window_bounds);
  }
}
#else
TEST(WindowSizerTest, LastWindowOffscreenWithNonAggressiveRepositioning) {}

#endif  // BUILDFLAG(IS_MAC)