| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "ash/common/test/test_shelf_delegate.h" | 5 #include "ash/common/test/test_shelf_delegate.h" | 
| 6 | 6 | 
| 7 #include <utility> | 7 #include <utility> | 
| 8 | 8 | 
| 9 #include "ash/common/shelf/shelf_model.h" | 9 #include "ash/common/shelf/shelf_model.h" | 
| 10 #include "ash/common/shelf/wm_shelf.h" | 10 #include "ash/common/shelf/wm_shelf.h" | 
| 11 #include "ash/common/shell_observer.h" | 11 #include "ash/common/shell_observer.h" | 
| 12 #include "ash/common/test/test_shelf_item_delegate.h" | 12 #include "ash/common/test/test_shelf_item_delegate.h" | 
| 13 #include "ash/common/wm_shell.h" | 13 #include "ash/common/wm_shell.h" | 
| 14 #include "ash/common/wm_window.h" | 14 #include "ash/common/wm_window.h" | 
| 15 #include "ash/root_window_controller.h" | 15 #include "ash/root_window_controller.h" | 
| 16 #include "ash/shell.h" | 16 #include "ash/shell.h" | 
| 17 #include "ash/wm/window_properties.h" | 17 #include "ash/wm/window_properties.h" | 
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" | 
| 19 #include "ui/aura/window.h" | 19 #include "ui/aura/window.h" | 
| 20 | 20 | 
| 21 namespace ash { | 21 namespace ash { | 
| 22 namespace test { | 22 namespace test { | 
| 23 | 23 | 
|  | 24 namespace { | 
|  | 25 | 
|  | 26 // Set the |type| of the item with the given |shelf_id|, if the item exists. | 
|  | 27 void SetItemType(ShelfID shelf_id, ShelfItemType type) { | 
|  | 28   ShelfModel* model = Shell::Get()->shelf_model(); | 
|  | 29   ash::ShelfItems::const_iterator item = model->ItemByID(shelf_id); | 
|  | 30   if (item != model->items().end()) { | 
|  | 31     ShelfItem pinned_item = *item; | 
|  | 32     pinned_item.type = type; | 
|  | 33     model->Set(item - model->items().begin(), pinned_item); | 
|  | 34   } | 
|  | 35 } | 
|  | 36 | 
|  | 37 }  // namespace | 
|  | 38 | 
| 24 TestShelfDelegate* TestShelfDelegate::instance_ = nullptr; | 39 TestShelfDelegate* TestShelfDelegate::instance_ = nullptr; | 
| 25 | 40 | 
| 26 // A ShellObserver that sets the shelf alignment and auto hide behavior when the | 41 // A ShellObserver that sets the shelf alignment and auto hide behavior when the | 
| 27 // shelf is created, to simulate ChromeLauncherController's behavior. | 42 // shelf is created, to simulate ChromeLauncherController's behavior. | 
| 28 class ShelfInitializer : public ShellObserver { | 43 class ShelfInitializer : public ShellObserver { | 
| 29  public: | 44  public: | 
| 30   ShelfInitializer() { Shell::GetInstance()->AddShellObserver(this); } | 45   ShelfInitializer() { Shell::GetInstance()->AddShellObserver(this); } | 
| 31   ~ShelfInitializer() override { | 46   ~ShelfInitializer() override { | 
| 32     Shell::GetInstance()->RemoveShellObserver(this); | 47     Shell::GetInstance()->RemoveShellObserver(this); | 
| 33   } | 48   } | 
| (...skipping 18 matching lines...) Expand all  Loading... | 
| 52     : shelf_initializer_(base::MakeUnique<ShelfInitializer>()) { | 67     : shelf_initializer_(base::MakeUnique<ShelfInitializer>()) { | 
| 53   CHECK(!instance_); | 68   CHECK(!instance_); | 
| 54   instance_ = this; | 69   instance_ = this; | 
| 55 } | 70 } | 
| 56 | 71 | 
| 57 TestShelfDelegate::~TestShelfDelegate() { | 72 TestShelfDelegate::~TestShelfDelegate() { | 
| 58   instance_ = nullptr; | 73   instance_ = nullptr; | 
| 59 } | 74 } | 
| 60 | 75 | 
| 61 void TestShelfDelegate::AddShelfItem(WmWindow* window) { | 76 void TestShelfDelegate::AddShelfItem(WmWindow* window) { | 
| 62   AddShelfItem(window, STATUS_CLOSED); | 77   AddShelfItem(window, std::string()); | 
| 63 } | 78 } | 
| 64 | 79 | 
| 65 void TestShelfDelegate::AddShelfItem(WmWindow* window, | 80 void TestShelfDelegate::AddShelfItem(WmWindow* window, | 
| 66                                      const std::string& app_id) { | 81                                      const std::string& app_id) { | 
| 67   AddShelfItem(window, STATUS_CLOSED); |  | 
| 68   ShelfID shelf_id = window->aura_window()->GetProperty(kShelfIDKey); |  | 
| 69   AddShelfIDToAppIDMapping(shelf_id, app_id); |  | 
| 70 } |  | 
| 71 |  | 
| 72 void TestShelfDelegate::AddShelfItem(WmWindow* window, ShelfItemStatus status) { |  | 
| 73   ShelfItem item; | 82   ShelfItem item; | 
|  | 83   if (!app_id.empty()) | 
|  | 84     item.app_launch_id = AppLaunchId(app_id); | 
| 74   if (window->GetType() == ui::wm::WINDOW_TYPE_PANEL) | 85   if (window->GetType() == ui::wm::WINDOW_TYPE_PANEL) | 
| 75     item.type = TYPE_APP_PANEL; | 86     item.type = TYPE_APP_PANEL; | 
| 76   else | 87   else | 
| 77     item.type = TYPE_APP; | 88     item.type = TYPE_APP; | 
| 78   ShelfModel* model = Shell::Get()->shelf_model(); | 89   ShelfModel* model = Shell::Get()->shelf_model(); | 
| 79   ShelfID id = model->next_id(); | 90   ShelfID id = model->next_id(); | 
| 80   item.status = status; | 91   item.status = STATUS_CLOSED; | 
| 81   model->Add(item); | 92   model->Add(item); | 
| 82   window->aura_window()->AddObserver(this); | 93   window->aura_window()->AddObserver(this); | 
| 83 | 94 | 
| 84   model->SetShelfItemDelegate(id, | 95   model->SetShelfItemDelegate(id, | 
| 85                               base::MakeUnique<TestShelfItemDelegate>(window)); | 96                               base::MakeUnique<TestShelfItemDelegate>(window)); | 
| 86   window->aura_window()->SetProperty(kShelfIDKey, id); | 97   window->aura_window()->SetProperty(kShelfIDKey, id); | 
| 87 } | 98 } | 
| 88 | 99 | 
| 89 void TestShelfDelegate::RemoveShelfItemForWindow(WmWindow* window) { | 100 void TestShelfDelegate::RemoveShelfItemForWindow(WmWindow* window) { | 
| 90   ShelfID shelf_id = window->aura_window()->GetProperty(kShelfIDKey); | 101   ShelfID shelf_id = window->aura_window()->GetProperty(kShelfIDKey); | 
| 91   if (shelf_id == 0) | 102   if (shelf_id == 0) | 
| 92     return; | 103     return; | 
| 93   ShelfModel* model = Shell::Get()->shelf_model(); | 104   ShelfModel* model = Shell::Get()->shelf_model(); | 
| 94   int index = model->ItemIndexByID(shelf_id); | 105   int index = model->ItemIndexByID(shelf_id); | 
| 95   DCHECK_NE(-1, index); | 106   DCHECK_NE(-1, index); | 
| 96   model->RemoveItemAt(index); | 107   model->RemoveItemAt(index); | 
| 97   window->aura_window()->RemoveObserver(this); | 108   window->aura_window()->RemoveObserver(this); | 
| 98   if (HasShelfIDToAppIDMapping(shelf_id)) { | 109   const std::string& app_id = GetAppIDForShelfID(shelf_id); | 
| 99     const std::string& app_id = GetAppIDForShelfID(shelf_id); | 110   if (IsAppPinned(app_id)) | 
| 100     if (IsAppPinned(app_id)) | 111     UnpinAppWithID(app_id); | 
| 101       UnpinAppWithID(app_id); |  | 
| 102     if (HasShelfIDToAppIDMapping(shelf_id)) |  | 
| 103       RemoveShelfIDToAppIDMapping(shelf_id); |  | 
| 104   } |  | 
| 105 } | 112 } | 
| 106 | 113 | 
| 107 void TestShelfDelegate::OnWindowDestroying(aura::Window* window) { | 114 void TestShelfDelegate::OnWindowDestroying(aura::Window* window) { | 
| 108   RemoveShelfItemForWindow(WmWindow::Get(window)); | 115   RemoveShelfItemForWindow(WmWindow::Get(window)); | 
| 109 } | 116 } | 
| 110 | 117 | 
| 111 void TestShelfDelegate::OnWindowHierarchyChanging( | 118 void TestShelfDelegate::OnWindowHierarchyChanging( | 
| 112     const HierarchyChangeParams& params) { | 119     const HierarchyChangeParams& params) { | 
| 113   // The window may be legitimately reparented while staying open if it moves | 120   // The window may be legitimately reparented while staying open if it moves | 
| 114   // to another display or container. If the window does not have a new parent | 121   // to another display or container. If the window does not have a new parent | 
| 115   // then remove the shelf item. | 122   // then remove the shelf item. | 
| 116   if (!params.new_parent) | 123   if (!params.new_parent) | 
| 117     RemoveShelfItemForWindow(WmWindow::Get(params.target)); | 124     RemoveShelfItemForWindow(WmWindow::Get(params.target)); | 
| 118 } | 125 } | 
| 119 | 126 | 
| 120 ShelfID TestShelfDelegate::GetShelfIDForAppID(const std::string& app_id) { | 127 ShelfID TestShelfDelegate::GetShelfIDForAppID(const std::string& app_id) { | 
| 121   for (auto const& iter : shelf_id_to_app_id_map_) { | 128   // Get shelf id for |app_id| and an empty |launch_id|. | 
| 122     if (iter.second == app_id) | 129   return GetShelfIDForAppIDAndLaunchID(app_id, std::string()); | 
| 123       return iter.first; |  | 
| 124   } |  | 
| 125   return 0; |  | 
| 126 } | 130 } | 
| 127 | 131 | 
| 128 ShelfID TestShelfDelegate::GetShelfIDForAppIDAndLaunchID( | 132 ShelfID TestShelfDelegate::GetShelfIDForAppIDAndLaunchID( | 
| 129     const std::string& app_id, | 133     const std::string& app_id, | 
| 130     const std::string& launch_id) { | 134     const std::string& launch_id) { | 
| 131   return GetShelfIDForAppID(app_id); | 135   for (const ShelfItem& item : Shell::Get()->shelf_model()->items()) { | 
| 132 } | 136     // Ash's ShelfWindowWatcher handles app panel windows separately. | 
| 133 | 137     if (item.type != TYPE_APP_PANEL && item.app_launch_id.app_id() == app_id && | 
| 134 bool TestShelfDelegate::HasShelfIDToAppIDMapping(ShelfID id) const { | 138         item.app_launch_id.launch_id() == launch_id) { | 
| 135   return shelf_id_to_app_id_map_.find(id) != shelf_id_to_app_id_map_.end(); | 139       return item.id; | 
|  | 140     } | 
|  | 141   } | 
|  | 142   return kInvalidShelfID; | 
| 136 } | 143 } | 
| 137 | 144 | 
| 138 const std::string& TestShelfDelegate::GetAppIDForShelfID(ShelfID id) { | 145 const std::string& TestShelfDelegate::GetAppIDForShelfID(ShelfID id) { | 
| 139   DCHECK_GT(shelf_id_to_app_id_map_.count(id), 0u); | 146   ShelfModel* model = Shell::Get()->shelf_model(); | 
| 140   return shelf_id_to_app_id_map_[id]; | 147   ash::ShelfItems::const_iterator item = model->ItemByID(id); | 
|  | 148   return item != model->items().end() ? item->app_launch_id.app_id() | 
|  | 149                                       : base::EmptyString(); | 
| 141 } | 150 } | 
| 142 | 151 | 
| 143 void TestShelfDelegate::PinAppWithID(const std::string& app_id) { | 152 void TestShelfDelegate::PinAppWithID(const std::string& app_id) { | 
|  | 153   SetItemType(GetShelfIDForAppID(app_id), TYPE_PINNED_APP); | 
| 144   pinned_apps_.insert(app_id); | 154   pinned_apps_.insert(app_id); | 
| 145 } | 155 } | 
| 146 | 156 | 
| 147 bool TestShelfDelegate::IsAppPinned(const std::string& app_id) { | 157 bool TestShelfDelegate::IsAppPinned(const std::string& app_id) { | 
| 148   return pinned_apps_.find(app_id) != pinned_apps_.end(); | 158   return pinned_apps_.find(app_id) != pinned_apps_.end(); | 
| 149 } | 159 } | 
| 150 | 160 | 
| 151 void TestShelfDelegate::UnpinAppWithID(const std::string& app_id) { | 161 void TestShelfDelegate::UnpinAppWithID(const std::string& app_id) { | 
|  | 162   SetItemType(GetShelfIDForAppID(app_id), TYPE_APP); | 
| 152   pinned_apps_.erase(app_id); | 163   pinned_apps_.erase(app_id); | 
| 153 } | 164 } | 
| 154 | 165 | 
| 155 void TestShelfDelegate::AddShelfIDToAppIDMapping(ShelfID shelf_id, |  | 
| 156                                                  const std::string& app_id) { |  | 
| 157   shelf_id_to_app_id_map_[shelf_id] = app_id; |  | 
| 158 } |  | 
| 159 |  | 
| 160 void TestShelfDelegate::RemoveShelfIDToAppIDMapping(ShelfID shelf_id) { |  | 
| 161   shelf_id_to_app_id_map_.erase(shelf_id); |  | 
| 162 } |  | 
| 163 |  | 
| 164 }  // namespace test | 166 }  // namespace test | 
| 165 }  // namespace ash | 167 }  // namespace ash | 
| OLD | NEW | 
|---|