This source file includes following definitions.
- GetModalContainer
- AllRootWindowsHaveModalBackgroundsForContainer
- AllRootWindowsHaveLockedModalBackgrounds
- AllRootWindowsHaveModalBackgrounds
- CloseTestWindow
- GetPreferredSize
- GetContentsView
- GetModalType
- mouse_presses_
- OpenTestWindowWithContext
- OpenTestWindowWithParent
- OnMousePressed
- mouse_presses
- destroyed
- OnWindowDestroyed
- OpenToplevelTestWindow
- OpenTestWindowWithParent
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "ash/wm/system_modal_container_layout_manager.h"
#include "ash/root_window_controller.h"
#include "ash/session_state_delegate.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/window_util.h"
#include "base/compiler_specific.h"
#include "base/run_loop.h"
#include "ui/aura/test/event_generator.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/screen.h"
#include "ui/views/test/capture_tracking_view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace test {
namespace {
aura::Window* GetModalContainer() {
return Shell::GetPrimaryRootWindowController()->GetContainer(
ash::kShellWindowId_SystemModalContainer);
}
bool AllRootWindowsHaveModalBackgroundsForContainer(int container_id) {
std::vector<aura::Window*> containers =
Shell::GetContainersFromAllRootWindows(container_id, NULL);
bool has_modal_screen = !containers.empty();
for (std::vector<aura::Window*>::iterator iter = containers.begin();
iter != containers.end(); ++iter) {
has_modal_screen &= static_cast<SystemModalContainerLayoutManager*>(
(*iter)->layout_manager())->has_modal_background();
}
return has_modal_screen;
}
bool AllRootWindowsHaveLockedModalBackgrounds() {
return AllRootWindowsHaveModalBackgroundsForContainer(
kShellWindowId_LockSystemModalContainer);
}
bool AllRootWindowsHaveModalBackgrounds() {
return AllRootWindowsHaveModalBackgroundsForContainer(
kShellWindowId_SystemModalContainer);
}
class TestWindow : public views::WidgetDelegateView {
public:
explicit TestWindow(bool modal) : modal_(modal) {}
virtual ~TestWindow() {}
static void CloseTestWindow(aura::Window* window) {
views::Widget::GetWidgetForNativeWindow(window)->Close();
}
virtual gfx::Size GetPreferredSize() OVERRIDE {
return gfx::Size(50, 50);
}
virtual views::View* GetContentsView() OVERRIDE {
return this;
}
virtual ui::ModalType GetModalType() const OVERRIDE {
return modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_NONE;
}
private:
bool modal_;
DISALLOW_COPY_AND_ASSIGN(TestWindow);
};
class EventTestWindow : public TestWindow {
public:
explicit EventTestWindow(bool modal) : TestWindow(modal),
mouse_presses_(0) {}
virtual ~EventTestWindow() {}
aura::Window* OpenTestWindowWithContext(aura::Window* context) {
views::Widget* widget =
views::Widget::CreateWindowWithContext(this, context);
widget->Show();
return widget->GetNativeView();
}
aura::Window* OpenTestWindowWithParent(aura::Window* parent) {
DCHECK(parent);
views::Widget* widget =
views::Widget::CreateWindowWithParent(this, parent);
widget->Show();
return widget->GetNativeView();
}
virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE {
mouse_presses_++;
return false;
}
int mouse_presses() const { return mouse_presses_; }
private:
int mouse_presses_;
DISALLOW_COPY_AND_ASSIGN(EventTestWindow);
};
class TransientWindowObserver : public aura::WindowObserver {
public:
TransientWindowObserver() : destroyed_(false) {}
virtual ~TransientWindowObserver() {}
bool destroyed() const { return destroyed_; }
virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {
destroyed_ = true;
}
private:
bool destroyed_;
DISALLOW_COPY_AND_ASSIGN(TransientWindowObserver);
};
}
class SystemModalContainerLayoutManagerTest : public AshTestBase {
public:
aura::Window* OpenToplevelTestWindow(bool modal) {
views::Widget* widget = views::Widget::CreateWindowWithContext(
new TestWindow(modal), CurrentContext());
widget->Show();
return widget->GetNativeView();
}
aura::Window* OpenTestWindowWithParent(aura::Window* parent, bool modal) {
views::Widget* widget =
views::Widget::CreateWindowWithParent(new TestWindow(modal), parent);
widget->Show();
return widget->GetNativeView();
}
};
TEST_F(SystemModalContainerLayoutManagerTest, NonModalTransient) {
scoped_ptr<aura::Window> parent(OpenToplevelTestWindow(false));
aura::Window* transient = OpenTestWindowWithParent(parent.get(), false);
TransientWindowObserver destruction_observer;
transient->AddObserver(&destruction_observer);
EXPECT_EQ(parent.get(), ::wm::GetTransientParent(transient));
EXPECT_EQ(parent->parent(), transient->parent());
parent.reset();
EXPECT_TRUE(destruction_observer.destroyed());
}
TEST_F(SystemModalContainerLayoutManagerTest, ModalTransient) {
scoped_ptr<aura::Window> parent(OpenToplevelTestWindow(false));
EXPECT_TRUE(wm::IsActiveWindow(parent.get()));
aura::Window* t1 = OpenTestWindowWithParent(parent.get(), true);
TransientWindowObserver do1;
t1->AddObserver(&do1);
EXPECT_EQ(parent.get(), ::wm::GetTransientParent(t1));
EXPECT_EQ(GetModalContainer(), t1->parent());
EXPECT_TRUE(wm::IsActiveWindow(t1));
aura::test::EventGenerator e1(Shell::GetPrimaryRootWindow(), parent.get());
e1.ClickLeftButton();
EXPECT_TRUE(wm::IsActiveWindow(t1));
aura::Window* t2 = OpenTestWindowWithParent(t1, true);
TransientWindowObserver do2;
t2->AddObserver(&do2);
EXPECT_TRUE(wm::IsActiveWindow(t2));
EXPECT_EQ(t1, ::wm::GetTransientParent(t2));
EXPECT_EQ(GetModalContainer(), t2->parent());
aura::test::EventGenerator e2(Shell::GetPrimaryRootWindow(), t1);
e2.ClickLeftButton();
EXPECT_TRUE(wm::IsActiveWindow(t2));
parent.reset();
EXPECT_TRUE(do1.destroyed());
EXPECT_TRUE(do2.destroyed());
}
TEST_F(SystemModalContainerLayoutManagerTest, ModalNonTransient) {
scoped_ptr<aura::Window> t1(OpenToplevelTestWindow(true));
EXPECT_TRUE(wm::IsActiveWindow(t1.get()));
TransientWindowObserver do1;
t1->AddObserver(&do1);
EXPECT_EQ(NULL, ::wm::GetTransientParent(t1.get()));
EXPECT_EQ(GetModalContainer(), t1->parent());
EXPECT_TRUE(wm::IsActiveWindow(t1.get()));
aura::test::EventGenerator e1(Shell::GetPrimaryRootWindow(),
Shell::GetPrimaryRootWindow());
e1.ClickLeftButton();
EXPECT_TRUE(wm::IsActiveWindow(t1.get()));
aura::Window* t2 = OpenTestWindowWithParent(t1.get(), true);
TransientWindowObserver do2;
t2->AddObserver(&do2);
EXPECT_TRUE(wm::IsActiveWindow(t2));
EXPECT_EQ(t1, ::wm::GetTransientParent(t2));
EXPECT_EQ(GetModalContainer(), t2->parent());
aura::test::EventGenerator e2(Shell::GetPrimaryRootWindow(), t1.get());
e2.ClickLeftButton();
EXPECT_TRUE(wm::IsActiveWindow(t2));
t1.reset();
EXPECT_TRUE(do1.destroyed());
EXPECT_TRUE(do2.destroyed());
}
TEST_F(SystemModalContainerLayoutManagerTest, CanActivateAfterEndModalSession) {
scoped_ptr<aura::Window> unrelated(OpenToplevelTestWindow(false));
unrelated->SetBounds(gfx::Rect(100, 100, 50, 50));
scoped_ptr<aura::Window> parent(OpenToplevelTestWindow(false));
EXPECT_TRUE(wm::IsActiveWindow(parent.get()));
scoped_ptr<aura::Window> transient(
OpenTestWindowWithParent(parent.get(), true));
EXPECT_TRUE(wm::IsActiveWindow(transient.get()));
aura::test::EventGenerator e1(Shell::GetPrimaryRootWindow(), parent.get());
e1.ClickLeftButton();
EXPECT_TRUE(wm::IsActiveWindow(transient.get()));
transient->Hide();
TestWindow::CloseTestWindow(transient.release());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(wm::IsActiveWindow(parent.get()));
aura::test::EventGenerator e2(Shell::GetPrimaryRootWindow(), unrelated.get());
e2.ClickLeftButton();
EXPECT_TRUE(wm::IsActiveWindow(unrelated.get()));
}
TEST_F(SystemModalContainerLayoutManagerTest, EventFocusContainers) {
EventTestWindow* main_delegate = new EventTestWindow(false);
scoped_ptr<aura::Window> main(
main_delegate->OpenTestWindowWithContext(CurrentContext()));
EXPECT_TRUE(wm::IsActiveWindow(main.get()));
aura::test::EventGenerator e1(Shell::GetPrimaryRootWindow(), main.get());
e1.ClickLeftButton();
EXPECT_EQ(1, main_delegate->mouse_presses());
EventTestWindow* transient_delegate = new EventTestWindow(true);
aura::Window* transient =
transient_delegate->OpenTestWindowWithParent(main.get());
EXPECT_TRUE(wm::IsActiveWindow(transient));
e1.ClickLeftButton();
EXPECT_EQ(1, transient_delegate->mouse_presses());
for (int block_reason = FIRST_BLOCK_REASON;
block_reason < NUMBER_OF_BLOCK_REASONS;
++block_reason) {
BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
EventTestWindow* lock_delegate = new EventTestWindow(false);
scoped_ptr<aura::Window> lock(lock_delegate->OpenTestWindowWithParent(
Shell::GetPrimaryRootWindowController()->GetContainer(
ash::kShellWindowId_LockScreenContainer)));
EXPECT_TRUE(wm::IsActiveWindow(lock.get()));
e1.ClickLeftButton();
EXPECT_EQ(1, lock_delegate->mouse_presses());
EventTestWindow* lock_modal_delegate = new EventTestWindow(true);
aura::Window* lock_modal =
lock_modal_delegate->OpenTestWindowWithParent(lock.get());
EXPECT_TRUE(wm::IsActiveWindow(lock_modal));
e1.ClickLeftButton();
EXPECT_EQ(1, lock_modal_delegate->mouse_presses());
EXPECT_EQ(1, lock_delegate->mouse_presses());
EXPECT_EQ(1, main_delegate->mouse_presses());
EXPECT_EQ(1, transient_delegate->mouse_presses());
UnblockUserSession();
}
}
TEST_F(SystemModalContainerLayoutManagerTest, ShowModalWhileHidden) {
Shell::GetPrimaryRootWindowController()
->GetContainer(kShellWindowId_SystemModalContainer)
->layer()
->SetOpacity(0);
scoped_ptr<aura::Window> parent(OpenToplevelTestWindow(false));
scoped_ptr<aura::Window> modal_window(
OpenTestWindowWithParent(parent.get(), true));
parent->Show();
modal_window->Show();
}
TEST_F(SystemModalContainerLayoutManagerTest, ChangeCapture) {
views::Widget* widget = views::Widget::CreateWindowWithContext(
new TestWindow(false), CurrentContext());
scoped_ptr<aura::Window> widget_window(widget->GetNativeView());
views::test::CaptureTrackingView* view = new views::test::CaptureTrackingView;
widget->GetContentsView()->AddChildView(view);
view->SetBoundsRect(widget->GetContentsView()->bounds());
widget->Show();
gfx::Point center(view->width() / 2, view->height() / 2);
views::View::ConvertPointToScreen(view, ¢er);
aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), center);
generator.PressLeftButton();
EXPECT_TRUE(view->got_press());
scoped_ptr<aura::Window> modal_window(
OpenTestWindowWithParent(widget->GetNativeView(), true));
modal_window->Show();
EXPECT_TRUE(view->got_capture_lost());
}
TEST_F(SystemModalContainerLayoutManagerTest, KeepVisible) {
GetModalContainer()->SetBounds(gfx::Rect(0, 0, 1024, 768));
scoped_ptr<aura::Window> main(OpenTestWindowWithParent(GetModalContainer(),
true));
main->SetBounds(gfx::Rect(924, 668, 100, 100));
GetModalContainer()->SetBounds(gfx::Rect(0, 0, 800, 600));
gfx::Rect bounds = main->bounds();
EXPECT_EQ(bounds, gfx::Rect(700, 500, 100, 100));
}
TEST_F(SystemModalContainerLayoutManagerTest, ShowNormalBackgroundOrLocked) {
scoped_ptr<aura::Window> parent(OpenToplevelTestWindow(false));
scoped_ptr<aura::Window> modal_window(
OpenTestWindowWithParent(parent.get(), true));
parent->Show();
modal_window->Show();
EXPECT_TRUE(AllRootWindowsHaveModalBackgrounds());
EXPECT_FALSE(AllRootWindowsHaveLockedModalBackgrounds());
TestWindow::CloseTestWindow(modal_window.release());
EXPECT_FALSE(AllRootWindowsHaveModalBackgrounds());
EXPECT_FALSE(AllRootWindowsHaveLockedModalBackgrounds());
for (int block_reason = FIRST_BLOCK_REASON;
block_reason < NUMBER_OF_BLOCK_REASONS;
++block_reason) {
BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
scoped_ptr<aura::Window> lock_parent(OpenTestWindowWithParent(
Shell::GetPrimaryRootWindowController()->GetContainer(
ash::kShellWindowId_LockScreenContainer),
false));
scoped_ptr<aura::Window> lock_modal_window(OpenTestWindowWithParent(
lock_parent.get(), true));
lock_parent->Show();
lock_modal_window->Show();
EXPECT_FALSE(AllRootWindowsHaveModalBackgrounds());
EXPECT_TRUE(AllRootWindowsHaveLockedModalBackgrounds());
TestWindow::CloseTestWindow(lock_modal_window.release());
scoped_ptr<aura::Window> modal_window(
OpenTestWindowWithParent(parent.get(), true));
modal_window->Show();
EXPECT_TRUE(AllRootWindowsHaveModalBackgrounds());
EXPECT_FALSE(AllRootWindowsHaveLockedModalBackgrounds());
TestWindow::CloseTestWindow(modal_window.release());
UnblockUserSession();
}
}
TEST_F(SystemModalContainerLayoutManagerTest, MultiDisplays) {
if (!SupportsMultipleDisplays())
return;
UpdateDisplay("500x500,500x500");
scoped_ptr<aura::Window> normal(OpenToplevelTestWindow(false));
normal->SetBounds(gfx::Rect(100, 100, 50, 50));
aura::Window::Windows root_windows = Shell::GetAllRootWindows();
EXPECT_EQ(2U, root_windows.size());
aura::Window* container1 = Shell::GetContainer(
root_windows[0], ash::kShellWindowId_SystemModalContainer);
aura::Window* container2 = Shell::GetContainer(
root_windows[1], ash::kShellWindowId_SystemModalContainer);
scoped_ptr<aura::Window> modal1(
OpenTestWindowWithParent(container1, true));
EXPECT_TRUE(AllRootWindowsHaveModalBackgrounds());
EXPECT_TRUE(wm::IsActiveWindow(modal1.get()));
scoped_ptr<aura::Window> modal11(
OpenTestWindowWithParent(container1, true));
EXPECT_TRUE(wm::IsActiveWindow(modal11.get()));
scoped_ptr<aura::Window> modal2(
OpenTestWindowWithParent(container2, true));
EXPECT_TRUE(wm::IsActiveWindow(modal2.get()));
EXPECT_EQ(container1, modal1->parent());
EXPECT_EQ(container1, modal11->parent());
EXPECT_EQ(container2, modal2->parent());
TestWindow::CloseTestWindow(modal2.release());
EXPECT_TRUE(AllRootWindowsHaveModalBackgrounds());
EXPECT_TRUE(wm::IsActiveWindow(modal11.get()));
TestWindow::CloseTestWindow(modal11.release());
EXPECT_TRUE(AllRootWindowsHaveModalBackgrounds());
EXPECT_TRUE(wm::IsActiveWindow(modal1.get()));
UpdateDisplay("500x500");
EXPECT_TRUE(AllRootWindowsHaveModalBackgrounds());
EXPECT_TRUE(wm::IsActiveWindow(modal1.get()));
UpdateDisplay("500x500,600x600");
EXPECT_TRUE(AllRootWindowsHaveModalBackgrounds());
EXPECT_TRUE(wm::IsActiveWindow(modal1.get()));
modal1->Hide();
TestWindow::CloseTestWindow(modal1.release());
EXPECT_FALSE(AllRootWindowsHaveModalBackgrounds());
EXPECT_TRUE(wm::IsActiveWindow(normal.get()));
}
}
}