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"
19 TestShelfDelegate
* TestShelfDelegate::instance_
= NULL
;
21 TestShelfDelegate::TestShelfDelegate(ShelfModel
* model
)
27 TestShelfDelegate::~TestShelfDelegate() {
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
) {
44 if (window
->type() == ui::wm::WINDOW_TYPE_PANEL
)
45 item
.type
= TYPE_APP_PANEL
;
47 item
.type
= TYPE_PLATFORM_APP
;
48 ShelfID id
= model_
->next_id();
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
);
65 int index
= model_
->ItemIndexByID(shelf_id
);
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
)
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 {
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
);