Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / test / test_shelf_delegate.cc
blobb3666b7b6616a2d32577a9d1bdc42c08f1eb0b53
1 // Copyright 2013 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.
5 #include "ash/test/test_shelf_delegate.h"
7 #include "ash/shelf/shelf_item_delegate_manager.h"
8 #include "ash/shelf/shelf_model.h"
9 #include "ash/shelf/shelf_util.h"
10 #include "ash/shell.h"
11 #include "ash/test/test_shelf_item_delegate.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/aura/window.h"
16 namespace ash {
17 namespace test {
19 TestShelfDelegate* TestShelfDelegate::instance_ = NULL;
21 TestShelfDelegate::TestShelfDelegate(ShelfModel* model)
22 : model_(model) {
23 CHECK(!instance_);
24 instance_ = this;
27 TestShelfDelegate::~TestShelfDelegate() {
28 instance_ = NULL;
31 void TestShelfDelegate::AddShelfItem(aura::Window* window) {
32 AddShelfItem(window, STATUS_CLOSED);
35 void TestShelfDelegate::AddShelfItem(aura::Window* window,
36 const std::string& app_id) {
37 AddShelfItem(window, STATUS_CLOSED);
38 AddShelfIDToAppIDMapping(GetShelfIDForWindow(window), app_id);
41 void TestShelfDelegate::AddShelfItem(aura::Window* window,
42 ShelfItemStatus status) {
43 ShelfItem item;
44 if (window->type() == ui::wm::WINDOW_TYPE_PANEL)
45 item.type = TYPE_APP_PANEL;
46 else
47 item.type = TYPE_PLATFORM_APP;
48 ShelfID id = model_->next_id();
49 item.status = status;
50 model_->Add(item);
51 window->AddObserver(this);
53 ShelfItemDelegateManager* manager =
54 Shell::GetInstance()->shelf_item_delegate_manager();
55 // |manager| owns TestShelfItemDelegate.
56 scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(window));
57 manager->SetShelfItemDelegate(id, delegate.Pass());
58 SetShelfIDForWindow(id, window);
61 void TestShelfDelegate::RemoveShelfItemForWindow(aura::Window* window) {
62 ShelfID shelf_id = GetShelfIDForWindow(window);
63 if (shelf_id == 0)
64 return;
65 int index = model_->ItemIndexByID(shelf_id);
66 DCHECK_NE(-1, index);
67 model_->RemoveItemAt(index);
68 window->RemoveObserver(this);
69 if (HasShelfIDToAppIDMapping(shelf_id)) {
70 const std::string& app_id = GetAppIDForShelfID(shelf_id);
71 if (IsAppPinned(app_id))
72 UnpinAppWithID(app_id);
73 if (HasShelfIDToAppIDMapping(shelf_id))
74 RemoveShelfIDToAppIDMapping(shelf_id);
78 void TestShelfDelegate::OnWindowDestroying(aura::Window* window) {
79 RemoveShelfItemForWindow(window);
82 void TestShelfDelegate::OnWindowHierarchyChanging(
83 const HierarchyChangeParams& params) {
84 // The window may be legitimately reparented while staying open if it moves
85 // to another display or container. If the window does not have a new parent
86 // then remove the shelf item.
87 if (!params.new_parent)
88 RemoveShelfItemForWindow(params.target);
91 void TestShelfDelegate::OnShelfCreated(Shelf* shelf) {
94 void TestShelfDelegate::OnShelfDestroyed(Shelf* shelf) {
97 ShelfID TestShelfDelegate::GetShelfIDForAppID(const std::string& app_id) {
98 for (auto const& iter : shelf_id_to_app_id_map_) {
99 if (iter.second == app_id)
100 return iter.first;
102 return 0;
105 bool TestShelfDelegate::HasShelfIDToAppIDMapping(ShelfID id) const {
106 return shelf_id_to_app_id_map_.find(id) != shelf_id_to_app_id_map_.end();
109 const std::string& TestShelfDelegate::GetAppIDForShelfID(ShelfID id) {
110 DCHECK_GT(shelf_id_to_app_id_map_.count(id), 0u);
111 return shelf_id_to_app_id_map_[id];
114 void TestShelfDelegate::PinAppWithID(const std::string& app_id) {
115 pinned_apps_.insert(app_id);
118 bool TestShelfDelegate::CanPin() const {
119 return true;
122 bool TestShelfDelegate::IsAppPinned(const std::string& app_id) {
123 return pinned_apps_.find(app_id) != pinned_apps_.end();
126 void TestShelfDelegate::UnpinAppWithID(const std::string& app_id) {
127 pinned_apps_.erase(app_id);
130 void TestShelfDelegate::AddShelfIDToAppIDMapping(ShelfID shelf_id,
131 const std::string& app_id) {
132 shelf_id_to_app_id_map_[shelf_id] = app_id;
135 void TestShelfDelegate::RemoveShelfIDToAppIDMapping(ShelfID shelf_id) {
136 shelf_id_to_app_id_map_.erase(shelf_id);
139 } // namespace test
140 } // namespace ash