Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(662)

Unified Diff: ash/test/test_shelf_delegate.cc

Issue 2825533003: mash: Prerequisites for removing ShelfDelegate. (Closed)
Patch Set: Expand CheckWindowAndItemPlacement comment. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ash/test/test_shelf_delegate.h ('k') | ash/test/test_shell_delegate.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ash/test/test_shelf_delegate.cc
diff --git a/ash/test/test_shelf_delegate.cc b/ash/test/test_shelf_delegate.cc
index c438fe49765bbbe70869ba7bcc52992280e09995..0fef1c73cb0f44e86fb6972b895d068a74c0df38 100644
--- a/ash/test/test_shelf_delegate.cc
+++ b/ash/test/test_shelf_delegate.cc
@@ -4,64 +4,16 @@
#include "ash/test/test_shelf_delegate.h"
-#include <utility>
-
-#include "ash/root_window_controller.h"
#include "ash/shelf/shelf_model.h"
-#include "ash/shelf/wm_shelf.h"
#include "ash/shell.h"
-#include "ash/shell_observer.h"
-#include "ash/test/test_shelf_item_delegate.h"
-#include "ash/wm/window_properties.h"
-#include "ash/wm_window.h"
-#include "base/memory/ptr_util.h"
-#include "ui/aura/window.h"
+#include "base/strings/string_util.h"
namespace ash {
namespace test {
-namespace {
-
-// Set the |type| of the item with the given |shelf_id|, if the item exists.
-void SetItemType(ShelfID shelf_id, ShelfItemType type) {
- ShelfModel* model = Shell::Get()->shelf_model();
- ash::ShelfItems::const_iterator item = model->ItemByID(shelf_id);
- if (item != model->items().end()) {
- ShelfItem pinned_item = *item;
- pinned_item.type = type;
- model->Set(item - model->items().begin(), pinned_item);
- }
-}
-
-} // namespace
-
TestShelfDelegate* TestShelfDelegate::instance_ = nullptr;
-// A ShellObserver that sets the shelf alignment and auto hide behavior when the
-// shelf is created, to simulate ChromeLauncherController's behavior.
-class ShelfInitializer : public ShellObserver {
- public:
- ShelfInitializer() { Shell::Get()->AddShellObserver(this); }
- ~ShelfInitializer() override { Shell::Get()->RemoveShellObserver(this); }
-
- // ShellObserver:
- void OnShelfCreatedForRootWindow(WmWindow* root_window) override {
- WmShelf* shelf = root_window->GetRootWindowController()->GetShelf();
- // Do not override the custom initialization performed by some unit tests.
- if (shelf->alignment() == SHELF_ALIGNMENT_BOTTOM_LOCKED &&
- shelf->auto_hide_behavior() == SHELF_AUTO_HIDE_ALWAYS_HIDDEN) {
- shelf->SetAlignment(SHELF_ALIGNMENT_BOTTOM);
- shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
- shelf->UpdateVisibilityState();
- }
- }
-
- private:
- DISALLOW_COPY_AND_ASSIGN(ShelfInitializer);
-};
-
-TestShelfDelegate::TestShelfDelegate()
- : shelf_initializer_(base::MakeUnique<ShelfInitializer>()) {
+TestShelfDelegate::TestShelfDelegate() {
CHECK(!instance_);
instance_ = this;
}
@@ -70,57 +22,6 @@ TestShelfDelegate::~TestShelfDelegate() {
instance_ = nullptr;
}
-void TestShelfDelegate::AddShelfItem(WmWindow* window) {
- AddShelfItem(window, std::string());
-}
-
-void TestShelfDelegate::AddShelfItem(WmWindow* window,
- const std::string& app_id) {
- ShelfItem item;
- if (!app_id.empty())
- item.app_launch_id = AppLaunchId(app_id);
- if (window->GetType() == ui::wm::WINDOW_TYPE_PANEL)
- item.type = TYPE_APP_PANEL;
- else
- item.type = TYPE_APP;
- ShelfModel* model = Shell::Get()->shelf_model();
- ShelfID id = model->next_id();
- item.status = STATUS_CLOSED;
- model->Add(item);
- window->aura_window()->AddObserver(this);
-
- model->SetShelfItemDelegate(id,
- base::MakeUnique<TestShelfItemDelegate>(window));
- window->aura_window()->SetProperty(kShelfIDKey, id);
-}
-
-void TestShelfDelegate::RemoveShelfItemForWindow(WmWindow* window) {
- ShelfID shelf_id = window->aura_window()->GetProperty(kShelfIDKey);
- if (shelf_id == 0)
- return;
- ShelfModel* model = Shell::Get()->shelf_model();
- int index = model->ItemIndexByID(shelf_id);
- DCHECK_NE(-1, index);
- model->RemoveItemAt(index);
- window->aura_window()->RemoveObserver(this);
- const std::string& app_id = GetAppIDForShelfID(shelf_id);
- if (IsAppPinned(app_id))
- UnpinAppWithID(app_id);
-}
-
-void TestShelfDelegate::OnWindowDestroying(aura::Window* window) {
- RemoveShelfItemForWindow(WmWindow::Get(window));
-}
-
-void TestShelfDelegate::OnWindowHierarchyChanging(
- const HierarchyChangeParams& params) {
- // The window may be legitimately reparented while staying open if it moves
- // to another display or container. If the window does not have a new parent
- // then remove the shelf item.
- if (!params.new_parent)
- RemoveShelfItemForWindow(WmWindow::Get(params.target));
-}
-
ShelfID TestShelfDelegate::GetShelfIDForAppID(const std::string& app_id) {
// Get shelf id for |app_id| and an empty |launch_id|.
return GetShelfIDForAppIDAndLaunchID(app_id, std::string());
@@ -141,23 +42,57 @@ ShelfID TestShelfDelegate::GetShelfIDForAppIDAndLaunchID(
const std::string& TestShelfDelegate::GetAppIDForShelfID(ShelfID id) {
ShelfModel* model = Shell::Get()->shelf_model();
- ash::ShelfItems::const_iterator item = model->ItemByID(id);
+ ShelfItems::const_iterator item = model->ItemByID(id);
return item != model->items().end() ? item->app_launch_id.app_id()
: base::EmptyString();
}
void TestShelfDelegate::PinAppWithID(const std::string& app_id) {
- SetItemType(GetShelfIDForAppID(app_id), TYPE_PINNED_APP);
- pinned_apps_.insert(app_id);
+ // If the app is already pinned, do nothing and return.
+ if (IsAppPinned(app_id))
+ return;
+
+ // Convert an existing item to be pinned, or create a new pinned item.
+ ShelfModel* model = Shell::Get()->shelf_model();
+ const int index = model->ItemIndexByID(GetShelfIDForAppID(app_id));
+ if (index >= 0) {
+ ShelfItem item = model->items()[index];
+ DCHECK_EQ(item.type, TYPE_APP);
+ DCHECK(!item.pinned_by_policy);
+ item.type = TYPE_PINNED_APP;
+ model->Set(index, item);
+ } else if (!app_id.empty()) {
+ ShelfItem item;
+ item.type = TYPE_PINNED_APP;
+ item.app_launch_id = AppLaunchId(app_id);
+ model->Add(item);
+ }
}
bool TestShelfDelegate::IsAppPinned(const std::string& app_id) {
- return pinned_apps_.find(app_id) != pinned_apps_.end();
+ ShelfID shelf_id = GetShelfIDForAppID(app_id);
+ ShelfModel* model = Shell::Get()->shelf_model();
+ ShelfItems::const_iterator item = model->ItemByID(shelf_id);
+ return item != model->items().end() && item->type == TYPE_PINNED_APP;
}
void TestShelfDelegate::UnpinAppWithID(const std::string& app_id) {
- SetItemType(GetShelfIDForAppID(app_id), TYPE_APP);
- pinned_apps_.erase(app_id);
+ // If the app is already not pinned, do nothing and return.
+ if (!IsAppPinned(app_id))
+ return;
+
+ // Remove the item if it is closed, or mark it as unpinned.
+ ShelfModel* model = Shell::Get()->shelf_model();
+ const int index = model->ItemIndexByID(GetShelfIDForAppID(app_id));
+ ShelfItem item = model->items()[index];
+ DCHECK_EQ(item.type, TYPE_PINNED_APP);
+ DCHECK(!item.pinned_by_policy);
+ if (item.status == STATUS_CLOSED) {
+ model->RemoveItemAt(index);
+ } else {
+ item.type = TYPE_APP;
+ model->Set(index, item);
+ }
}
} // namespace test
« no previous file with comments | « ash/test/test_shelf_delegate.h ('k') | ash/test/test_shell_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698