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.
4
5#include "ash/test/test_shelf_delegate.h"
6
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"
15
16namespace ash {
17namespace test {
18
19TestShelfDelegate* TestShelfDelegate::instance_ = NULL;
20
21TestShelfDelegate::TestShelfDelegate(ShelfModel* model)
22    : model_(model) {
23  CHECK(!instance_);
24  instance_ = this;
25}
26
27TestShelfDelegate::~TestShelfDelegate() {
28  instance_ = NULL;
29}
30
31void TestShelfDelegate::AddShelfItem(aura::Window* window) {
32  AddShelfItem(window, STATUS_CLOSED);
33}
34
35void TestShelfDelegate::AddShelfItem(aura::Window* window,
36                                     ShelfItemStatus status) {
37  ShelfItem item;
38  if (window->type() == ui::wm::WINDOW_TYPE_PANEL)
39    item.type = TYPE_APP_PANEL;
40  else
41    item.type = TYPE_PLATFORM_APP;
42  ShelfID id = model_->next_id();
43  item.status = status;
44  model_->Add(item);
45  window->AddObserver(this);
46
47  ShelfItemDelegateManager* manager =
48      Shell::GetInstance()->shelf_item_delegate_manager();
49  // |manager| owns TestShelfItemDelegate.
50  scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(window));
51  manager->SetShelfItemDelegate(id, delegate.Pass());
52  SetShelfIDForWindow(id, window);
53}
54
55void TestShelfDelegate::RemoveShelfItemForWindow(aura::Window* window) {
56  ShelfID id = GetShelfIDForWindow(window);
57  if (id == 0)
58    return;
59  int index = model_->ItemIndexByID(id);
60  DCHECK_NE(-1, index);
61  model_->RemoveItemAt(index);
62  window->RemoveObserver(this);
63}
64
65void TestShelfDelegate::OnWindowDestroying(aura::Window* window) {
66  RemoveShelfItemForWindow(window);
67}
68
69void TestShelfDelegate::OnWindowHierarchyChanging(
70      const HierarchyChangeParams& params) {
71  // The window may be legitimately reparented while staying open if it moves
72  // to another display or container. If the window does not have a new parent
73  // then remove the shelf item.
74  if (!params.new_parent)
75    RemoveShelfItemForWindow(params.target);
76}
77
78void TestShelfDelegate::OnShelfCreated(Shelf* shelf) {
79}
80
81void TestShelfDelegate::OnShelfDestroyed(Shelf* shelf) {
82}
83
84ShelfID TestShelfDelegate::GetShelfIDForAppID(const std::string& app_id) {
85  return 0;
86}
87
88const std::string& TestShelfDelegate::GetAppIDForShelfID(ShelfID id) {
89  return base::EmptyString();
90}
91
92void TestShelfDelegate::PinAppWithID(const std::string& app_id) {
93}
94
95bool TestShelfDelegate::CanPin() const {
96  return true;
97}
98
99bool TestShelfDelegate::IsAppPinned(const std::string& app_id) {
100  return false;
101}
102
103void TestShelfDelegate::UnpinAppWithID(const std::string& app_id) {
104}
105
106}  // namespace test
107}  // namespace ash
108