| OLD | NEW |
| (Empty) |
| 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/common/test/test_shelf_delegate.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "ash/common/shelf/shelf_model.h" | |
| 10 #include "ash/common/shelf/wm_shelf.h" | |
| 11 #include "ash/common/shell_observer.h" | |
| 12 #include "ash/common/test/test_shelf_item_delegate.h" | |
| 13 #include "ash/common/wm_shell.h" | |
| 14 #include "ash/common/wm_window.h" | |
| 15 #include "ash/root_window_controller.h" | |
| 16 #include "ash/wm/window_properties.h" | |
| 17 #include "base/memory/ptr_util.h" | |
| 18 #include "ui/aura/window.h" | |
| 19 | |
| 20 namespace ash { | |
| 21 namespace test { | |
| 22 | |
| 23 TestShelfDelegate* TestShelfDelegate::instance_ = nullptr; | |
| 24 | |
| 25 // A ShellObserver that sets the shelf alignment and auto hide behavior when the | |
| 26 // shelf is created, to simulate ChromeLauncherController's behavior. | |
| 27 class ShelfInitializer : public ShellObserver { | |
| 28 public: | |
| 29 ShelfInitializer() { WmShell::Get()->AddShellObserver(this); } | |
| 30 ~ShelfInitializer() override { WmShell::Get()->RemoveShellObserver(this); } | |
| 31 | |
| 32 // ShellObserver: | |
| 33 void OnShelfCreatedForRootWindow(WmWindow* root_window) override { | |
| 34 WmShelf* shelf = root_window->GetRootWindowController()->GetShelf(); | |
| 35 // Do not override the custom initialization performed by some unit tests. | |
| 36 if (shelf->alignment() == SHELF_ALIGNMENT_BOTTOM_LOCKED && | |
| 37 shelf->auto_hide_behavior() == SHELF_AUTO_HIDE_ALWAYS_HIDDEN) { | |
| 38 shelf->SetAlignment(SHELF_ALIGNMENT_BOTTOM); | |
| 39 shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER); | |
| 40 shelf->UpdateVisibilityState(); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 DISALLOW_COPY_AND_ASSIGN(ShelfInitializer); | |
| 46 }; | |
| 47 | |
| 48 TestShelfDelegate::TestShelfDelegate() | |
| 49 : shelf_initializer_(base::MakeUnique<ShelfInitializer>()) { | |
| 50 CHECK(!instance_); | |
| 51 instance_ = this; | |
| 52 } | |
| 53 | |
| 54 TestShelfDelegate::~TestShelfDelegate() { | |
| 55 instance_ = nullptr; | |
| 56 } | |
| 57 | |
| 58 void TestShelfDelegate::AddShelfItem(WmWindow* window) { | |
| 59 AddShelfItem(window, STATUS_CLOSED); | |
| 60 } | |
| 61 | |
| 62 void TestShelfDelegate::AddShelfItem(WmWindow* window, | |
| 63 const std::string& app_id) { | |
| 64 AddShelfItem(window, STATUS_CLOSED); | |
| 65 ShelfID shelf_id = window->aura_window()->GetProperty(kShelfIDKey); | |
| 66 AddShelfIDToAppIDMapping(shelf_id, app_id); | |
| 67 } | |
| 68 | |
| 69 void TestShelfDelegate::AddShelfItem(WmWindow* window, ShelfItemStatus status) { | |
| 70 ShelfItem item; | |
| 71 if (window->GetType() == ui::wm::WINDOW_TYPE_PANEL) | |
| 72 item.type = TYPE_APP_PANEL; | |
| 73 else | |
| 74 item.type = TYPE_APP; | |
| 75 ShelfModel* model = WmShell::Get()->shelf_model(); | |
| 76 ShelfID id = model->next_id(); | |
| 77 item.status = status; | |
| 78 model->Add(item); | |
| 79 window->aura_window()->AddObserver(this); | |
| 80 | |
| 81 model->SetShelfItemDelegate(id, | |
| 82 base::MakeUnique<TestShelfItemDelegate>(window)); | |
| 83 window->aura_window()->SetProperty(kShelfIDKey, id); | |
| 84 } | |
| 85 | |
| 86 void TestShelfDelegate::RemoveShelfItemForWindow(WmWindow* window) { | |
| 87 ShelfID shelf_id = window->aura_window()->GetProperty(kShelfIDKey); | |
| 88 if (shelf_id == 0) | |
| 89 return; | |
| 90 ShelfModel* model = WmShell::Get()->shelf_model(); | |
| 91 int index = model->ItemIndexByID(shelf_id); | |
| 92 DCHECK_NE(-1, index); | |
| 93 model->RemoveItemAt(index); | |
| 94 window->aura_window()->RemoveObserver(this); | |
| 95 if (HasShelfIDToAppIDMapping(shelf_id)) { | |
| 96 const std::string& app_id = GetAppIDForShelfID(shelf_id); | |
| 97 if (IsAppPinned(app_id)) | |
| 98 UnpinAppWithID(app_id); | |
| 99 if (HasShelfIDToAppIDMapping(shelf_id)) | |
| 100 RemoveShelfIDToAppIDMapping(shelf_id); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void TestShelfDelegate::OnWindowDestroying(aura::Window* window) { | |
| 105 RemoveShelfItemForWindow(WmWindow::Get(window)); | |
| 106 } | |
| 107 | |
| 108 void TestShelfDelegate::OnWindowHierarchyChanging( | |
| 109 const HierarchyChangeParams& params) { | |
| 110 // The window may be legitimately reparented while staying open if it moves | |
| 111 // to another display or container. If the window does not have a new parent | |
| 112 // then remove the shelf item. | |
| 113 if (!params.new_parent) | |
| 114 RemoveShelfItemForWindow(WmWindow::Get(params.target)); | |
| 115 } | |
| 116 | |
| 117 ShelfID TestShelfDelegate::GetShelfIDForAppID(const std::string& app_id) { | |
| 118 for (auto const& iter : shelf_id_to_app_id_map_) { | |
| 119 if (iter.second == app_id) | |
| 120 return iter.first; | |
| 121 } | |
| 122 return 0; | |
| 123 } | |
| 124 | |
| 125 ShelfID TestShelfDelegate::GetShelfIDForAppIDAndLaunchID( | |
| 126 const std::string& app_id, | |
| 127 const std::string& launch_id) { | |
| 128 return GetShelfIDForAppID(app_id); | |
| 129 } | |
| 130 | |
| 131 bool TestShelfDelegate::HasShelfIDToAppIDMapping(ShelfID id) const { | |
| 132 return shelf_id_to_app_id_map_.find(id) != shelf_id_to_app_id_map_.end(); | |
| 133 } | |
| 134 | |
| 135 const std::string& TestShelfDelegate::GetAppIDForShelfID(ShelfID id) { | |
| 136 DCHECK_GT(shelf_id_to_app_id_map_.count(id), 0u); | |
| 137 return shelf_id_to_app_id_map_[id]; | |
| 138 } | |
| 139 | |
| 140 void TestShelfDelegate::PinAppWithID(const std::string& app_id) { | |
| 141 pinned_apps_.insert(app_id); | |
| 142 } | |
| 143 | |
| 144 bool TestShelfDelegate::IsAppPinned(const std::string& app_id) { | |
| 145 return pinned_apps_.find(app_id) != pinned_apps_.end(); | |
| 146 } | |
| 147 | |
| 148 void TestShelfDelegate::UnpinAppWithID(const std::string& app_id) { | |
| 149 pinned_apps_.erase(app_id); | |
| 150 } | |
| 151 | |
| 152 void TestShelfDelegate::AddShelfIDToAppIDMapping(ShelfID shelf_id, | |
| 153 const std::string& app_id) { | |
| 154 shelf_id_to_app_id_map_[shelf_id] = app_id; | |
| 155 } | |
| 156 | |
| 157 void TestShelfDelegate::RemoveShelfIDToAppIDMapping(ShelfID shelf_id) { | |
| 158 shelf_id_to_app_id_map_.erase(shelf_id); | |
| 159 } | |
| 160 | |
| 161 } // namespace test | |
| 162 } // namespace ash | |
| OLD | NEW |