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