Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2012 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 
      5 #include "ui/aura/test/test_screen.h"
      6 
      7 #include "base/logging.h"
      8 #include "ui/aura/env.h"
      9 #include "ui/aura/root_window.h"
     10 #include "ui/aura/root_window_host.h"
     11 #include "ui/aura/window.h"
     12 #include "ui/gfx/native_widget_types.h"
     13 #include "ui/gfx/rect_conversions.h"
     14 #include "ui/gfx/screen.h"
     15 
     16 namespace aura {
     17 
     18 // static
     19 TestScreen* TestScreen::Create() {
     20   // Use (0,0) because the desktop aura tests are executed in
     21   // native environment where the display's origin is (0,0).
     22   return new TestScreen(gfx::Rect(0, 0, 800, 600));
     23 }
     24 
     25 // static
     26 TestScreen* TestScreen::CreateFullscreen() {
     27   return new TestScreen(gfx::Rect(RootWindowHost::GetNativeScreenSize()));
     28 }
     29 
     30 TestScreen::~TestScreen() {
     31 }
     32 
     33 RootWindow* TestScreen::CreateRootWindowForPrimaryDisplay() {
     34   DCHECK(!root_window_);
     35   root_window_ = new RootWindow(RootWindow::CreateParams(display_.bounds()));
     36   root_window_->AddObserver(this);
     37   root_window_->Init();
     38   return root_window_;
     39 }
     40 
     41 void TestScreen::SetDeviceScaleFactor(float device_scale_factor) {
     42   gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
     43   display_.SetScaleAndBounds(device_scale_factor, bounds_in_pixel);
     44   root_window_->OnHostResized(bounds_in_pixel.size());
     45 }
     46 
     47 void TestScreen::SetDisplayRotation(gfx::Display::Rotation rotation) {
     48   display_.set_rotation(rotation);
     49   // TODO(oshima|mukai): Update the display_ as well.
     50   root_window_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
     51 }
     52 
     53 void TestScreen::SetUIScale(float ui_scale) {
     54   ui_scale_ = ui_scale;
     55   gfx::Rect bounds_in_pixel(display_.GetSizeInPixel());
     56   gfx::Rect new_bounds = gfx::ToNearestRect(
     57       gfx::ScaleRect(bounds_in_pixel, 1.0f / ui_scale));
     58   display_.SetScaleAndBounds(display_.device_scale_factor(), new_bounds);
     59   root_window_->SetTransform(GetRotationTransform() * GetUIScaleTransform());
     60 }
     61 
     62 gfx::Transform TestScreen::GetRotationTransform() const {
     63   gfx::Transform rotate;
     64   float one_pixel = 1.0f / display_.device_scale_factor();
     65   switch (display_.rotation()) {
     66     case gfx::Display::ROTATE_0:
     67       break;
     68     case gfx::Display::ROTATE_90:
     69       rotate.Translate(display_.bounds().height() - one_pixel, 0);
     70       rotate.Rotate(90);
     71       break;
     72     case gfx::Display::ROTATE_270:
     73       rotate.Translate(0, display_.bounds().width() - one_pixel);
     74       rotate.Rotate(270);
     75       break;
     76     case gfx::Display::ROTATE_180:
     77       rotate.Translate(display_.bounds().width() - one_pixel,
     78                        display_.bounds().height() - one_pixel);
     79       rotate.Rotate(180);
     80       break;
     81   }
     82 
     83   return rotate;
     84 }
     85 
     86 gfx::Transform TestScreen::GetUIScaleTransform() const {
     87   gfx::Transform ui_scale;
     88   ui_scale.Scale(1.0f / ui_scale_, 1.0f / ui_scale_);
     89   return ui_scale;
     90 }
     91 
     92 bool TestScreen::IsDIPEnabled() {
     93   return true;
     94 }
     95 
     96 void TestScreen::OnWindowBoundsChanged(
     97     Window* window, const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
     98   DCHECK_EQ(root_window_, window);
     99   display_.SetSize(new_bounds.size());
    100 }
    101 
    102 void TestScreen::OnWindowDestroying(Window* window) {
    103   if (root_window_ == window)
    104     root_window_ = NULL;
    105 }
    106 
    107 gfx::Point TestScreen::GetCursorScreenPoint() {
    108   return Env::GetInstance()->last_mouse_location();
    109 }
    110 
    111 gfx::NativeWindow TestScreen::GetWindowAtCursorScreenPoint() {
    112   const gfx::Point point = GetCursorScreenPoint();
    113   return root_window_->GetTopWindowContainingPoint(point);
    114 }
    115 
    116 int TestScreen::GetNumDisplays() {
    117   return 1;
    118 }
    119 
    120 gfx::Display TestScreen::GetDisplayNearestWindow(
    121     gfx::NativeWindow window) const {
    122   return display_;
    123 }
    124 
    125 gfx::Display TestScreen::GetDisplayNearestPoint(const gfx::Point& point) const {
    126   return display_;
    127 }
    128 
    129 gfx::Display TestScreen::GetDisplayMatching(const gfx::Rect& match_rect) const {
    130   return display_;
    131 }
    132 
    133 gfx::Display TestScreen::GetPrimaryDisplay() const {
    134   return display_;
    135 }
    136 
    137 void TestScreen::AddObserver(gfx::DisplayObserver* observer) {
    138 }
    139 
    140 void TestScreen::RemoveObserver(gfx::DisplayObserver* observer) {
    141 }
    142 
    143 TestScreen::TestScreen(const gfx::Rect& screen_bounds)
    144     : root_window_(NULL),
    145       ui_scale_(1.0f) {
    146   static int64 synthesized_display_id = 2000;
    147   display_.set_id(synthesized_display_id++);
    148   display_.SetScaleAndBounds(1.0f, screen_bounds);
    149 }
    150 
    151 }  // namespace aura
    152