Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4805)

Unified Diff: chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc

Issue 11072002: Combined window positioning and show state determiniation in the WindowSizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc
diff --git a/chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc b/chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d934a51542d0b92021ac473186e3b60ff95b9183
--- /dev/null
+++ b/chrome/browser/ui/window_sizer/window_sizer_common_unittest.cc
@@ -0,0 +1,263 @@
+// 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 "chrome/browser/ui/window_sizer/window_sizer_common_unittest.h"
+
+#include "ash/shell.h"
+#include "ash/test/test_shell_delegate.h"
+#include "ash/wm/window_resizer.h"
+#include "base/compiler_specific.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/render_view_test.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
+#include "ui/aura/env.h"
+#include "ui/aura/root_window.h"
+#include "ui/aura/test/test_windows.h"
+
+// The class function definitions from window_sizer_common_unittest.h
+WindowSizerTestWithBrowser::WindowSizerTestWithBrowser() {
+ // Set up a UI message thread.
+ MessageLoopForUI* ui_loop = message_loop();
+ ui_thread_.reset(
+ new content::TestBrowserThread(content::BrowserThread::UI, ui_loop));
+}
+
+WindowSizerTestWithBrowser::~WindowSizerTestWithBrowser() {
+}
+
+void TestMonitorInfoProvider::AddMonitor(const gfx::Rect& bounds,
+ const gfx::Rect& work_area) {
+ DCHECK(bounds.Contains(work_area));
+ monitor_bounds_.push_back(bounds);
+ work_areas_.push_back(work_area);
+}
+
+// Overridden from WindowSizer::MonitorInfoProvider:
+gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayWorkArea() const {
+ return work_areas_[0];
+}
+
+gfx::Rect TestMonitorInfoProvider::GetPrimaryDisplayBounds() const {
+ return monitor_bounds_[0];
+}
+
+gfx::Rect TestMonitorInfoProvider::GetMonitorWorkAreaMatching(
+ const gfx::Rect& match_rect) const {
+ return work_areas_[GetMonitorIndexMatchingBounds(match_rect)];
+}
+
+size_t TestMonitorInfoProvider::GetMonitorIndexMatchingBounds(
+ const gfx::Rect& match_rect) const {
+ int max_area = 0;
+ size_t max_area_index = 0;
+ // Loop through all the monitors, finding the one that intersects the
+ // largest area of the supplied match rect.
+ for (size_t i = 0; i < work_areas_.size(); ++i) {
+ gfx::Rect overlap(match_rect.Intersect(work_areas_[i]));
+ int area = overlap.width() * overlap.height();
+ if (area > max_area) {
+ max_area = area;
+ max_area_index = i;
+ }
+ }
+ return max_area_index;
+}
+
+TestStateProvider::TestStateProvider():
+ has_persistent_data_(false),
+ persistent_show_state_(ui::SHOW_STATE_DEFAULT),
+ has_last_active_data_(false),
+ last_active_show_state_(ui::SHOW_STATE_DEFAULT) {
+}
+
+void TestStateProvider::SetPersistentState(const gfx::Rect& bounds,
+ const gfx::Rect& work_area,
+ ui::WindowShowState show_state,
+ bool has_persistent_data) {
+ persistent_bounds_ = bounds;
+ persistent_work_area_ = work_area;
+ persistent_show_state_ = show_state;
+ has_persistent_data_ = has_persistent_data;
+}
+
+void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds,
+ ui::WindowShowState show_state,
+ bool has_last_active_data) {
+ last_active_bounds_ = bounds;
+ last_active_show_state_ = show_state;
+ has_last_active_data_ = has_last_active_data;
+}
+
+bool TestStateProvider::GetPersistentState(
+ gfx::Rect* bounds,
+ gfx::Rect* saved_work_area,
+ ui::WindowShowState& show_state) const {
+ *bounds = persistent_bounds_;
+ *saved_work_area = persistent_work_area_;
+ if (show_state == ui::SHOW_STATE_DEFAULT)
+ show_state = persistent_show_state_;
+ return has_persistent_data_;
+}
+
+bool TestStateProvider::GetLastActiveWindowState(
+ gfx::Rect* bounds,
+ ui::WindowShowState& show_state) const {
+ *bounds = last_active_bounds_;
+ if (show_state == ui::SHOW_STATE_DEFAULT)
+ show_state = last_active_show_state_;
+ return has_last_active_data_;
+}
+
+TestBrowserWindowAura::TestBrowserWindowAura(aura::Window *native_window)
+ : native_window_(native_window) {
+}
+
+TestBrowserWindowAura::~TestBrowserWindowAura() {}
+
+gfx::Rect TestBrowserWindowAura::GetBounds() const {
+ return native_window_->bounds();
+}
+
+int kWindowTilePixels = WindowSizer::kWindowTilePixels;
+
+// The window sizer commonly used test functions.
+void GetWindowBoundsAndShowState(
+ const gfx::Rect& monitor1_bounds,
+ const gfx::Rect& monitor1_work_area,
+ const gfx::Rect& monitor2_bounds,
+ const gfx::Rect& bounds,
+ const gfx::Rect& work_area,
+ ui::WindowShowState show_state_persisted,
+ ui::WindowShowState show_state_last,
+ Source source,
+ gfx::Rect* out_bounds,
+ ui::WindowShowState& out_show_state,
+ const Browser* browser,
+ const gfx::Rect& passed_in) {
+ TestMonitorInfoProvider* mip = new TestMonitorInfoProvider;
+ mip->AddMonitor(monitor1_bounds, monitor1_work_area);
+ if (!monitor2_bounds.IsEmpty())
+ mip->AddMonitor(monitor2_bounds, monitor2_bounds);
+ TestStateProvider* sp = new TestStateProvider;
+ if (source == PERSISTED || source == BOTH)
+ sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
+ if (source == LAST_ACTIVE || source == BOTH)
+ sp->SetLastActiveState(bounds, show_state_last, true);
+
+ WindowSizer sizer(sp, mip, browser);
+ sizer.DetermineWindowBoundsAndShowState(passed_in,
+ out_bounds,
+ out_show_state);
+}
+
+void GetWindowBounds(const gfx::Rect& monitor1_bounds,
+ const gfx::Rect& monitor1_work_area,
+ const gfx::Rect& monitor2_bounds,
+ const gfx::Rect& bounds,
+ const gfx::Rect& work_area,
+ Source source,
+ gfx::Rect* out_bounds,
+ const Browser* browser,
+ const gfx::Rect& passed_in) {
+ ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
+ GetWindowBoundsAndShowState(
+ monitor1_bounds, monitor1_work_area, monitor2_bounds, bounds, work_area,
+ ui::SHOW_STATE_DEFAULT, ui::SHOW_STATE_DEFAULT, source, out_bounds,
+ out_show_state, browser, passed_in);
+}
+
+ui::WindowShowState GetWindowShowState(
+ ui::WindowShowState show_state_persisted,
+ ui::WindowShowState show_state_last,
+ Source source,
+ const Browser* browser) {
+ gfx::Rect bounds = tentwentyfour;
+ gfx::Rect work_area = tentwentyfour;
+ TestMonitorInfoProvider* mip = new TestMonitorInfoProvider;
+ mip->AddMonitor(tentwentyfour, tentwentyfour);
+ TestStateProvider* sp = new TestStateProvider;
+ if (source == PERSISTED || source == BOTH)
+ sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
+ if (source == LAST_ACTIVE || source == BOTH)
+ sp->SetLastActiveState(bounds, show_state_last, true);
+
+ WindowSizer sizer(sp, mip, browser);
+
+ ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
+ gfx::Rect out_bounds;
+ sizer.DetermineWindowBoundsAndShowState(
+ gfx::Rect(),
+ &out_bounds,
+ out_show_state);
+ return out_show_state;
+}
+
+// Test that the default show state override behavior is properly handled.
+TEST_F(WindowSizerTestWithBrowser, TestShowStateDefaults) {
+ // Creating a browser & window to play with.
+ scoped_ptr<aura::Window> window(
+ aura::test::CreateTestWindowWithId(0, NULL));
+ window->SetBounds(gfx::Rect(16, 32, 640, 320));
+
+ scoped_ptr<TestingProfile> profile(new TestingProfile());
+
+ scoped_ptr<BrowserWindow> browser_window(
+ new TestBrowserWindowAura(window.get()));
+ Browser::CreateParams window_params(Browser::TYPE_TABBED, profile.get());
+ window_params.window = browser_window.get();
+ scoped_ptr<Browser> browser(new Browser(window_params));
+
+ // Create also a popup browser since that behaves slightly different for
+ // defaults.
+ scoped_ptr<aura::Window> popup(
+ aura::test::CreateTestWindowWithId(1, NULL));
+ popup->SetBounds(gfx::Rect(16, 32, 128, 256));
+
+ scoped_ptr<BrowserWindow> browser_popup(
+ new TestBrowserWindowAura(popup.get()));
+ Browser::CreateParams popup_params(Browser::TYPE_POPUP, profile.get());
+ popup_params.window = browser_window.get();
+ scoped_ptr<Browser> popup_browser(new Browser(popup_params));
+
+ // Check that a browser creation state always get used if not given as
+ // SHOW_STATE_DEFAULT.
+ EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
+ ui::SHOW_STATE_MAXIMIZED,
+ DEFAULT,
+ browser.get()), ui::SHOW_STATE_DEFAULT);
+ browser->set_initial_show_state(ui::SHOW_STATE_MINIMIZED);
+ EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
+ ui::SHOW_STATE_MAXIMIZED,
+ BOTH,
+ browser.get()), ui::SHOW_STATE_MINIMIZED);
+ browser->set_initial_show_state(ui::SHOW_STATE_NORMAL);
+ EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
+ ui::SHOW_STATE_MAXIMIZED,
+ BOTH,
+ browser.get()), ui::SHOW_STATE_NORMAL);
+ browser->set_initial_show_state(ui::SHOW_STATE_MAXIMIZED);
+ EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
+ ui::SHOW_STATE_NORMAL,
+ BOTH,
+ browser.get()), ui::SHOW_STATE_MAXIMIZED);
+
+ // Check that setting the maximized command line option is forcing the
+ // maximized state.
+ CommandLine::ForCurrentProcess()->AppendSwitch(switches::kStartMaximized);
+
+ browser->set_initial_show_state(ui::SHOW_STATE_NORMAL);
+ EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
+ ui::SHOW_STATE_NORMAL,
+ BOTH,
+ browser.get()), ui::SHOW_STATE_MAXIMIZED);
+
+ // The popup should favor the initial show state over the command line.
+ EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
+ ui::SHOW_STATE_NORMAL,
+ BOTH,
+ popup_browser.get()), ui::SHOW_STATE_NORMAL);
+}

Powered by Google App Engine
This is Rietveld 408576698