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