This source file includes following definitions.
- OnWMEvent
- GetType
- AttachState
- DetachState
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "ash/wm/window_state.h"
#include "ash/screen_util.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/window_state.h"
#include "ash/wm/wm_event.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
namespace ash {
namespace wm {
namespace {
class AlwaysMaximizeTestState : public WindowState::State {
public:
explicit AlwaysMaximizeTestState(WindowStateType initial_state_type)
: state_type_(initial_state_type) {}
virtual ~AlwaysMaximizeTestState() {}
virtual void OnWMEvent(WindowState* window_state,
const WMEvent* event) OVERRIDE {
}
virtual WindowStateType GetType() const OVERRIDE {
return state_type_;
}
virtual void AttachState(
WindowState* window_state,
WindowState::State* previous_state) OVERRIDE {
if (state_type_ != WINDOW_STATE_TYPE_MAXIMIZED) {
window_state->Maximize();
state_type_ = WINDOW_STATE_TYPE_MAXIMIZED;
}
}
virtual void DetachState(WindowState* window_state) OVERRIDE {}
private:
WindowStateType state_type_;
DISALLOW_COPY_AND_ASSIGN(AlwaysMaximizeTestState);
};
}
typedef test::AshTestBase WindowStateTest;
TEST_F(WindowStateTest, SnapWindowBasic) {
if (!SupportsMultipleDisplays())
return;
UpdateDisplay("0+0-500x400, 0+500-600x400");
const gfx::Rect kPrimaryDisplayWorkAreaBounds =
ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
const gfx::Rect kSecondaryDisplayWorkAreaBounds =
ScreenUtil::GetSecondaryDisplay().work_area();
scoped_ptr<aura::Window> window(
CreateTestWindowInShellWithBounds(gfx::Rect(100, 100, 100, 100)));
WindowState* window_state = GetWindowState(window.get());
const WMEvent snap_left(WM_EVENT_SNAP_LEFT);
window_state->OnWMEvent(&snap_left);
gfx::Rect expected = gfx::Rect(
kPrimaryDisplayWorkAreaBounds.x(),
kPrimaryDisplayWorkAreaBounds.y(),
kPrimaryDisplayWorkAreaBounds.width() / 2,
kPrimaryDisplayWorkAreaBounds.height());
EXPECT_EQ(expected.ToString(), window->GetBoundsInScreen().ToString());
const WMEvent snap_right(WM_EVENT_SNAP_RIGHT);
window_state->OnWMEvent(&snap_right);
expected.set_x(kPrimaryDisplayWorkAreaBounds.right() - expected.width());
EXPECT_EQ(expected.ToString(), window->GetBoundsInScreen().ToString());
window->SetBoundsInScreen(gfx::Rect(600, 0, 100, 100),
ScreenUtil::GetSecondaryDisplay());
window_state->OnWMEvent(&snap_right);
expected = gfx::Rect(
kSecondaryDisplayWorkAreaBounds.x() +
kSecondaryDisplayWorkAreaBounds.width() / 2,
kSecondaryDisplayWorkAreaBounds.y(),
kSecondaryDisplayWorkAreaBounds.width() / 2,
kSecondaryDisplayWorkAreaBounds.height());
EXPECT_EQ(expected.ToString(), window->GetBoundsInScreen().ToString());
window_state->OnWMEvent(&snap_left);
expected.set_x(kSecondaryDisplayWorkAreaBounds.x());
EXPECT_EQ(expected.ToString(), window->GetBoundsInScreen().ToString());
}
TEST_F(WindowStateTest, SnapWindowMinimumSize) {
if (!SupportsHostWindowResize())
return;
UpdateDisplay("0+0-600x900");
const gfx::Rect kWorkAreaBounds =
ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
aura::test::TestWindowDelegate delegate;
scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
&delegate, -1, gfx::Rect(0, 100, kWorkAreaBounds.width() - 1, 100)));
delegate.set_minimum_size(gfx::Size(kWorkAreaBounds.width() - 1, 0));
WindowState* window_state = GetWindowState(window.get());
EXPECT_TRUE(window_state->CanSnap());
const WMEvent snap_right(WM_EVENT_SNAP_RIGHT);
window_state->OnWMEvent(&snap_right);
gfx::Rect expected = gfx::Rect(kWorkAreaBounds.x() + 1,
kWorkAreaBounds.y(),
kWorkAreaBounds.width() - 1,
kWorkAreaBounds.height());
EXPECT_EQ(expected.ToString(), window->GetBoundsInScreen().ToString());
delegate.set_minimum_size(gfx::Size());
delegate.set_maximum_size(gfx::Size(kWorkAreaBounds.width() - 1, INT_MAX));
EXPECT_FALSE(window_state->CanSnap());
}
TEST_F(WindowStateTest, SnapWindowSetBounds) {
if (!SupportsHostWindowResize())
return;
UpdateDisplay("0+0-900x600");
const gfx::Rect kWorkAreaBounds =
ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
scoped_ptr<aura::Window> window(
CreateTestWindowInShellWithBounds(gfx::Rect(100, 100, 100, 100)));
WindowState* window_state = GetWindowState(window.get());
const WMEvent snap_left(WM_EVENT_SNAP_LEFT);
window_state->OnWMEvent(&snap_left);
EXPECT_EQ(WINDOW_STATE_TYPE_LEFT_SNAPPED, window_state->GetStateType());
gfx::Rect expected = gfx::Rect(kWorkAreaBounds.x(),
kWorkAreaBounds.y(),
kWorkAreaBounds.width() / 2,
kWorkAreaBounds.height());
EXPECT_EQ(expected.ToString(), window->GetBoundsInScreen().ToString());
expected.set_width(500);
window->SetBounds(gfx::Rect(10, 10, 500, 300));
EXPECT_EQ(expected.ToString(), window->GetBoundsInScreen().ToString());
EXPECT_EQ(WINDOW_STATE_TYPE_LEFT_SNAPPED, window_state->GetStateType());
}
TEST_F(WindowStateTest, RestoreBounds) {
scoped_ptr<aura::Window> window(
CreateTestWindowInShellWithBounds(gfx::Rect(100, 100, 100, 100)));
WindowState* window_state = GetWindowState(window.get());
EXPECT_TRUE(window_state->IsNormalStateType());
gfx::Rect restore_bounds = window->GetBoundsInScreen();
restore_bounds.set_width(restore_bounds.width() + 1);
window_state->SetRestoreBoundsInScreen(restore_bounds);
const WMEvent snap_left(WM_EVENT_SNAP_LEFT);
window_state->OnWMEvent(&snap_left);
const WMEvent snap_right(WM_EVENT_SNAP_RIGHT);
window_state->OnWMEvent(&snap_right);
EXPECT_NE(restore_bounds.ToString(), window->GetBoundsInScreen().ToString());
EXPECT_EQ(restore_bounds.ToString(),
window_state->GetRestoreBoundsInScreen().ToString());
window_state->Restore();
EXPECT_EQ(restore_bounds.ToString(), window->GetBoundsInScreen().ToString());
window_state->Maximize();
gfx::Rect maximized_bounds = window->GetBoundsInScreen();
EXPECT_NE(maximized_bounds.ToString(), restore_bounds.ToString());
EXPECT_EQ(restore_bounds.ToString(),
window_state->GetRestoreBoundsInScreen().ToString());
window_state->OnWMEvent(&snap_left);
EXPECT_NE(restore_bounds.ToString(), window->GetBoundsInScreen().ToString());
EXPECT_NE(maximized_bounds.ToString(),
window->GetBoundsInScreen().ToString());
EXPECT_EQ(restore_bounds.ToString(),
window_state->GetRestoreBoundsInScreen().ToString());
window_state->Restore();
EXPECT_EQ(restore_bounds.ToString(), window->GetBoundsInScreen().ToString());
}
TEST_F(WindowStateTest, AutoManaged) {
scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
WindowState* window_state = GetWindowState(window.get());
window_state->set_window_position_managed(true);
window->Hide();
window->SetBounds(gfx::Rect(100, 100, 100, 100));
window->Show();
window_state->Maximize();
const WMEvent snap_right(WM_EVENT_SNAP_RIGHT);
window_state->OnWMEvent(&snap_right);
const gfx::Rect kWorkAreaBounds =
ash::Shell::GetScreen()->GetPrimaryDisplay().work_area();
gfx::Rect expected_snapped_bounds(
kWorkAreaBounds.x() + kWorkAreaBounds.width() / 2,
kWorkAreaBounds.y(),
kWorkAreaBounds.width() / 2,
kWorkAreaBounds.height());
EXPECT_EQ(expected_snapped_bounds.ToString(),
window->GetBoundsInScreen().ToString());
EXPECT_TRUE(window_state->window_position_managed());
}
TEST_F(WindowStateTest, SimpleStateSwap) {
scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
WindowState* window_state = GetWindowState(window.get());
EXPECT_FALSE(window_state->IsMaximized());
window_state->SetStateObject(
scoped_ptr<WindowState::State> (new AlwaysMaximizeTestState(
window_state->GetStateType())));
EXPECT_TRUE(window_state->IsMaximized());
}
TEST_F(WindowStateTest, StateSwapRestore) {
scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
WindowState* window_state = GetWindowState(window.get());
EXPECT_FALSE(window_state->IsMaximized());
scoped_ptr<WindowState::State> old(window_state->SetStateObject(
scoped_ptr<WindowState::State> (new AlwaysMaximizeTestState(
window_state->GetStateType()))).Pass());
EXPECT_TRUE(window_state->IsMaximized());
window_state->SetStateObject(old.Pass());
EXPECT_FALSE(window_state->IsMaximized());
}
}
}