| 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" |
| 11 #include "ash/common/shell_observer.h" |
| 10 #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" |
| 11 #include "ash/common/wm_window.h" | 14 #include "ash/common/wm_window.h" |
| 12 #include "ash/common/wm_window_property.h" | 15 #include "ash/common/wm_window_property.h" |
| 16 #include "ash/root_window_controller.h" |
| 13 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 14 | 18 |
| 15 namespace ash { | 19 namespace ash { |
| 16 namespace test { | 20 namespace test { |
| 17 | 21 |
| 18 TestShelfDelegate* TestShelfDelegate::instance_ = nullptr; | 22 TestShelfDelegate* TestShelfDelegate::instance_ = nullptr; |
| 19 | 23 |
| 20 TestShelfDelegate::TestShelfDelegate(ShelfModel* model) : model_(model) { | 24 // A ShellObserver that sets the shelf alignment and auto hide behavior when the |
| 25 // shelf is created, to simulate ChromeLauncherController's behavior. |
| 26 class ShelfInitializer : public ShellObserver { |
| 27 public: |
| 28 ShelfInitializer() { WmShell::Get()->AddShellObserver(this); } |
| 29 ~ShelfInitializer() override { WmShell::Get()->RemoveShellObserver(this); } |
| 30 |
| 31 // ShellObserver: |
| 32 void OnShelfCreatedForRootWindow(WmWindow* root_window) override { |
| 33 WmShelf* shelf = root_window->GetRootWindowController()->GetShelf(); |
| 34 // Do not override the custom initialization performed by some unit tests. |
| 35 if (shelf->alignment() == SHELF_ALIGNMENT_BOTTOM_LOCKED && |
| 36 shelf->auto_hide_behavior() == SHELF_AUTO_HIDE_ALWAYS_HIDDEN) { |
| 37 shelf->SetAlignment(SHELF_ALIGNMENT_BOTTOM); |
| 38 shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER); |
| 39 shelf->UpdateVisibilityState(); |
| 40 } |
| 41 } |
| 42 |
| 43 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(ShelfInitializer); |
| 45 }; |
| 46 |
| 47 TestShelfDelegate::TestShelfDelegate() |
| 48 : shelf_initializer_(base::MakeUnique<ShelfInitializer>()) { |
| 21 CHECK(!instance_); | 49 CHECK(!instance_); |
| 22 instance_ = this; | 50 instance_ = this; |
| 23 } | 51 } |
| 24 | 52 |
| 25 TestShelfDelegate::~TestShelfDelegate() { | 53 TestShelfDelegate::~TestShelfDelegate() { |
| 26 instance_ = nullptr; | 54 instance_ = nullptr; |
| 27 } | 55 } |
| 28 | 56 |
| 29 void TestShelfDelegate::AddShelfItem(WmWindow* window) { | 57 void TestShelfDelegate::AddShelfItem(WmWindow* window) { |
| 30 AddShelfItem(window, STATUS_CLOSED); | 58 AddShelfItem(window, STATUS_CLOSED); |
| 31 } | 59 } |
| 32 | 60 |
| 33 void TestShelfDelegate::AddShelfItem(WmWindow* window, | 61 void TestShelfDelegate::AddShelfItem(WmWindow* window, |
| 34 const std::string& app_id) { | 62 const std::string& app_id) { |
| 35 AddShelfItem(window, STATUS_CLOSED); | 63 AddShelfItem(window, STATUS_CLOSED); |
| 36 ShelfID shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); | 64 ShelfID shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); |
| 37 AddShelfIDToAppIDMapping(shelf_id, app_id); | 65 AddShelfIDToAppIDMapping(shelf_id, app_id); |
| 38 } | 66 } |
| 39 | 67 |
| 40 void TestShelfDelegate::AddShelfItem(WmWindow* window, ShelfItemStatus status) { | 68 void TestShelfDelegate::AddShelfItem(WmWindow* window, ShelfItemStatus status) { |
| 41 ShelfItem item; | 69 ShelfItem item; |
| 42 if (window->GetType() == ui::wm::WINDOW_TYPE_PANEL) | 70 if (window->GetType() == ui::wm::WINDOW_TYPE_PANEL) |
| 43 item.type = TYPE_APP_PANEL; | 71 item.type = TYPE_APP_PANEL; |
| 44 else | 72 else |
| 45 item.type = TYPE_APP; | 73 item.type = TYPE_APP; |
| 46 ShelfID id = model_->next_id(); | 74 ShelfModel* model = WmShell::Get()->shelf_model(); |
| 75 ShelfID id = model->next_id(); |
| 47 item.status = status; | 76 item.status = status; |
| 48 model_->Add(item); | 77 model->Add(item); |
| 49 window->AddObserver(this); | 78 window->AddObserver(this); |
| 50 | 79 |
| 51 model_->SetShelfItemDelegate(id, | 80 model->SetShelfItemDelegate(id, |
| 52 base::MakeUnique<TestShelfItemDelegate>(window)); | 81 base::MakeUnique<TestShelfItemDelegate>(window)); |
| 53 window->SetIntProperty(WmWindowProperty::SHELF_ID, id); | 82 window->SetIntProperty(WmWindowProperty::SHELF_ID, id); |
| 54 } | 83 } |
| 55 | 84 |
| 56 void TestShelfDelegate::RemoveShelfItemForWindow(WmWindow* window) { | 85 void TestShelfDelegate::RemoveShelfItemForWindow(WmWindow* window) { |
| 57 ShelfID shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); | 86 ShelfID shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); |
| 58 if (shelf_id == 0) | 87 if (shelf_id == 0) |
| 59 return; | 88 return; |
| 60 int index = model_->ItemIndexByID(shelf_id); | 89 ShelfModel* model = WmShell::Get()->shelf_model(); |
| 90 int index = model->ItemIndexByID(shelf_id); |
| 61 DCHECK_NE(-1, index); | 91 DCHECK_NE(-1, index); |
| 62 model_->RemoveItemAt(index); | 92 model->RemoveItemAt(index); |
| 63 window->RemoveObserver(this); | 93 window->RemoveObserver(this); |
| 64 if (HasShelfIDToAppIDMapping(shelf_id)) { | 94 if (HasShelfIDToAppIDMapping(shelf_id)) { |
| 65 const std::string& app_id = GetAppIDForShelfID(shelf_id); | 95 const std::string& app_id = GetAppIDForShelfID(shelf_id); |
| 66 if (IsAppPinned(app_id)) | 96 if (IsAppPinned(app_id)) |
| 67 UnpinAppWithID(app_id); | 97 UnpinAppWithID(app_id); |
| 68 if (HasShelfIDToAppIDMapping(shelf_id)) | 98 if (HasShelfIDToAppIDMapping(shelf_id)) |
| 69 RemoveShelfIDToAppIDMapping(shelf_id); | 99 RemoveShelfIDToAppIDMapping(shelf_id); |
| 70 } | 100 } |
| 71 } | 101 } |
| 72 | 102 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 const std::string& app_id) { | 152 const std::string& app_id) { |
| 123 shelf_id_to_app_id_map_[shelf_id] = app_id; | 153 shelf_id_to_app_id_map_[shelf_id] = app_id; |
| 124 } | 154 } |
| 125 | 155 |
| 126 void TestShelfDelegate::RemoveShelfIDToAppIDMapping(ShelfID shelf_id) { | 156 void TestShelfDelegate::RemoveShelfIDToAppIDMapping(ShelfID shelf_id) { |
| 127 shelf_id_to_app_id_map_.erase(shelf_id); | 157 shelf_id_to_app_id_map_.erase(shelf_id); |
| 128 } | 158 } |
| 129 | 159 |
| 130 } // namespace test | 160 } // namespace test |
| 131 } // namespace ash | 161 } // namespace ash |
| OLD | NEW |