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 "ash/test/display_manager_test_api.h"
      6 
      7 #include <vector>
      8 
      9 #include "ash/display/display_info.h"
     10 #include "ash/display/display_manager.h"
     11 #include "ash/shell.h"
     12 #include "base/strings/string_split.h"
     13 #include "ui/aura/window_event_dispatcher.h"
     14 #include "ui/gfx/display.h"
     15 
     16 namespace ash {
     17 namespace test {
     18 typedef std::vector<gfx::Display> DisplayList;
     19 typedef DisplayInfo DisplayInfo;
     20 typedef std::vector<DisplayInfo> DisplayInfoList;
     21 
     22 namespace {
     23 
     24 std::vector<DisplayInfo> CreateDisplayInfoListFromString(
     25     const std::string specs,
     26     DisplayManager* display_manager) {
     27   std::vector<DisplayInfo> display_info_list;
     28   std::vector<std::string> parts;
     29   base::SplitString(specs, ',', &parts);
     30   size_t index = 0;
     31   for (std::vector<std::string>::const_iterator iter = parts.begin();
     32        iter != parts.end(); ++iter, ++index) {
     33     int64 id = index < display_manager->GetNumDisplays() ?
     34         display_manager->GetDisplayAt(index).id() :
     35         gfx::Display::kInvalidDisplayID;
     36     display_info_list.push_back(
     37         DisplayInfo::CreateFromSpecWithID(*iter, id));
     38   }
     39   return display_info_list;
     40 }
     41 
     42 }  // namespace
     43 
     44 DisplayManagerTestApi::DisplayManagerTestApi(DisplayManager* display_manager)
     45     : display_manager_(display_manager) {}
     46 
     47 DisplayManagerTestApi::~DisplayManagerTestApi() {}
     48 
     49 void DisplayManagerTestApi::UpdateDisplay(
     50     const std::string& display_specs) {
     51   std::vector<DisplayInfo> display_info_list =
     52       CreateDisplayInfoListFromString(display_specs, display_manager_);
     53   bool is_host_origin_set = false;
     54   for (size_t i = 0; i < display_info_list.size(); ++i) {
     55     const DisplayInfo& display_info = display_info_list[i];
     56     if (display_info.bounds_in_native().origin() != gfx::Point(0, 0)) {
     57       is_host_origin_set = true;
     58       break;
     59     }
     60   }
     61 
     62   // On non-testing environment, when a secondary display is connected, a new
     63   // native (i.e. X) window for the display is always created below the
     64   // previous one for GPU performance reasons. Try to emulate the behavior
     65   // unless host origins are explicitly set.
     66   if (!is_host_origin_set) {
     67     // Sart from (1,1) so that windows won't overlap with native mouse cursor.
     68     // See |AshTestBase::SetUp()|.
     69     int next_y = 1;
     70     for (std::vector<DisplayInfo>::iterator iter = display_info_list.begin();
     71          iter != display_info_list.end(); ++iter) {
     72       gfx::Rect bounds(iter->bounds_in_native().size());
     73       bounds.set_x(1);
     74       bounds.set_y(next_y);
     75       next_y += bounds.height();
     76       iter->SetBounds(bounds);
     77     }
     78   }
     79 
     80   display_manager_->OnNativeDisplaysChanged(display_info_list);
     81 }
     82 
     83 int64 DisplayManagerTestApi::SetFirstDisplayAsInternalDisplay() {
     84   const gfx::Display& internal = display_manager_->displays_[0];
     85   gfx::Display::SetInternalDisplayId(internal.id());
     86   return gfx::Display::InternalDisplayId();
     87 }
     88 
     89 void DisplayManagerTestApi::DisableChangeDisplayUponHostResize() {
     90   display_manager_->set_change_display_upon_host_resize(false);
     91 }
     92 
     93 void DisplayManagerTestApi::SetAvailableColorProfiles(
     94     int64 display_id,
     95     const std::vector<ui::ColorCalibrationProfile>& profiles) {
     96   display_manager_->display_info_[display_id].set_available_color_profiles(
     97       profiles);
     98 }
     99 
    100 }  // namespace test
    101 }  // namespace ash
    102