OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/shelf/shelf_controller.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "ash/public/cpp/config.h" |
| 11 #include "ash/public/interfaces/shelf.mojom.h" |
| 12 #include "ash/shelf/shelf.h" |
| 13 #include "ash/shelf/shelf_model.h" |
| 14 #include "ash/shelf/shelf_model_observer.h" |
| 15 #include "ash/shell.h" |
| 16 #include "ash/test/ash_test_base.h" |
| 17 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 18 #include "ui/display/types/display_constants.h" |
| 19 |
| 20 namespace ash { |
| 21 namespace { |
| 22 |
| 23 // A test implementation of the ShelfObserver mojo interface. |
| 24 class TestShelfObserver : public mojom::ShelfObserver { |
| 25 public: |
| 26 TestShelfObserver() = default; |
| 27 ~TestShelfObserver() override = default; |
| 28 |
| 29 // mojom::ShelfObserver: |
| 30 void OnShelfInitialized(int64_t display_id) override { |
| 31 display_id_ = display_id; |
| 32 } |
| 33 void OnAlignmentChanged(ShelfAlignment alignment, |
| 34 int64_t display_id) override { |
| 35 alignment_ = alignment; |
| 36 display_id_ = display_id; |
| 37 } |
| 38 void OnAutoHideBehaviorChanged(ShelfAutoHideBehavior auto_hide, |
| 39 int64_t display_id) override { |
| 40 auto_hide_ = auto_hide; |
| 41 display_id_ = display_id; |
| 42 } |
| 43 void OnShelfItemAdded(int32_t, const ShelfItem&) override { added_count_++; } |
| 44 void OnShelfItemRemoved(const ShelfID&) override { removed_count_++; } |
| 45 void OnShelfItemMoved(const ShelfID&, int32_t) override {} |
| 46 void OnShelfItemUpdated(const ShelfItem&) override {} |
| 47 void OnShelfItemDelegateChanged(const ShelfID&, |
| 48 mojom::ShelfItemDelegatePtr) override {} |
| 49 |
| 50 int64_t display_id() const { return display_id_; } |
| 51 ShelfAlignment alignment() const { return alignment_; } |
| 52 ShelfAutoHideBehavior auto_hide() const { return auto_hide_; } |
| 53 size_t added_count() const { return added_count_; } |
| 54 size_t removed_count() const { return removed_count_; } |
| 55 |
| 56 private: |
| 57 int64_t display_id_ = display::kInvalidDisplayId; |
| 58 ShelfAlignment alignment_ = SHELF_ALIGNMENT_BOTTOM_LOCKED; |
| 59 ShelfAutoHideBehavior auto_hide_ = SHELF_AUTO_HIDE_ALWAYS_HIDDEN; |
| 60 size_t added_count_ = 0; |
| 61 size_t removed_count_ = 0; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(TestShelfObserver); |
| 64 }; |
| 65 |
| 66 using ShelfControllerTest = test::AshTestBase; |
| 67 using NoSessionShelfControllerTest = test::NoSessionAshTestBase; |
| 68 |
| 69 TEST_F(ShelfControllerTest, IntializesAppListItemDelegate) { |
| 70 ShelfModel* model = Shell::Get()->shelf_controller()->model(); |
| 71 EXPECT_EQ(1, model->item_count()); |
| 72 EXPECT_EQ(kAppListId, model->items()[0].id.app_id); |
| 73 EXPECT_TRUE(model->GetShelfItemDelegate(ShelfID(kAppListId))); |
| 74 } |
| 75 |
| 76 TEST_F(NoSessionShelfControllerTest, AlignmentAndAutoHide) { |
| 77 ShelfController* controller = Shell::Get()->shelf_controller(); |
| 78 TestShelfObserver observer; |
| 79 mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 80 mojo::AssociatedBinding<mojom::ShelfObserver> binding( |
| 81 &observer, mojo::MakeIsolatedRequest(&observer_ptr)); |
| 82 controller->AddObserver(observer_ptr.PassInterface()); |
| 83 |
| 84 // Simulated login should initialize the primary shelf and notify |observer|. |
| 85 EXPECT_EQ(display::kInvalidDisplayId, observer.display_id()); |
| 86 EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM_LOCKED, observer.alignment()); |
| 87 EXPECT_EQ(SHELF_AUTO_HIDE_ALWAYS_HIDDEN, observer.auto_hide()); |
| 88 SetUserLoggedIn(true); |
| 89 SetSessionStarted(true); |
| 90 base::RunLoop().RunUntilIdle(); |
| 91 EXPECT_EQ(GetPrimaryDisplay().id(), observer.display_id()); |
| 92 EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM, observer.alignment()); |
| 93 EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_NEVER, observer.auto_hide()); |
| 94 |
| 95 // Changing shelf properties should notify |observer|. |
| 96 GetPrimaryShelf()->SetAlignment(SHELF_ALIGNMENT_LEFT); |
| 97 base::RunLoop().RunUntilIdle(); |
| 98 EXPECT_EQ(SHELF_ALIGNMENT_LEFT, observer.alignment()); |
| 99 GetPrimaryShelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); |
| 100 base::RunLoop().RunUntilIdle(); |
| 101 EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS, observer.auto_hide()); |
| 102 } |
| 103 |
| 104 TEST_F(ShelfControllerTest, ShelfModelChangesInClassicAsh) { |
| 105 if (Shell::GetAshConfig() == Config::MASH) |
| 106 return; |
| 107 |
| 108 ShelfController* controller = Shell::Get()->shelf_controller(); |
| 109 TestShelfObserver observer; |
| 110 mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 111 mojo::AssociatedBinding<mojom::ShelfObserver> binding( |
| 112 &observer, mojo::MakeIsolatedRequest(&observer_ptr)); |
| 113 controller->AddObserver(observer_ptr.PassInterface()); |
| 114 |
| 115 // The ShelfModel should be initialized with a single item for the AppList. |
| 116 // In classic ash, the observer should not be notified of ShelfModel changes. |
| 117 EXPECT_EQ(1, controller->model()->item_count()); |
| 118 EXPECT_EQ(0u, observer.added_count()); |
| 119 EXPECT_EQ(0u, observer.removed_count()); |
| 120 |
| 121 // Add a ShelfModel item; |observer| should not be notified in classic ash. |
| 122 ShelfItem item; |
| 123 item.type = TYPE_PINNED_APP; |
| 124 item.id = ShelfID("foo"); |
| 125 int index = controller->model()->Add(item); |
| 126 base::RunLoop().RunUntilIdle(); |
| 127 EXPECT_EQ(2, controller->model()->item_count()); |
| 128 EXPECT_EQ(0u, observer.added_count()); |
| 129 EXPECT_EQ(0u, observer.removed_count()); |
| 130 |
| 131 // Remove a ShelfModel item; |observer| should not be notified in classic ash. |
| 132 controller->model()->RemoveItemAt(index); |
| 133 base::RunLoop().RunUntilIdle(); |
| 134 EXPECT_EQ(1, controller->model()->item_count()); |
| 135 EXPECT_EQ(0u, observer.added_count()); |
| 136 EXPECT_EQ(0u, observer.removed_count()); |
| 137 } |
| 138 |
| 139 TEST_F(ShelfControllerTest, ShelfModelChangesInMash) { |
| 140 if (Shell::GetAshConfig() != Config::MASH) |
| 141 return; |
| 142 |
| 143 ShelfController* controller = Shell::Get()->shelf_controller(); |
| 144 TestShelfObserver observer; |
| 145 mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 146 mojo::AssociatedBinding<mojom::ShelfObserver> binding( |
| 147 &observer, mojo::MakeIsolatedRequest(&observer_ptr)); |
| 148 controller->AddObserver(observer_ptr.PassInterface()); |
| 149 base::RunLoop().RunUntilIdle(); |
| 150 |
| 151 // The ShelfModel should be initialized with a single item for the AppList. |
| 152 // In mash, the observer is immediately notified of existing shelf items. |
| 153 EXPECT_EQ(1, controller->model()->item_count()); |
| 154 EXPECT_EQ(1u, observer.added_count()); |
| 155 EXPECT_EQ(0u, observer.removed_count()); |
| 156 |
| 157 // Add a ShelfModel item; |observer| should be notified in mash. |
| 158 ShelfItem item; |
| 159 item.type = TYPE_PINNED_APP; |
| 160 item.id = ShelfID("foo"); |
| 161 int index = controller->model()->Add(item); |
| 162 base::RunLoop().RunUntilIdle(); |
| 163 EXPECT_EQ(2, controller->model()->item_count()); |
| 164 EXPECT_EQ(2u, observer.added_count()); |
| 165 EXPECT_EQ(0u, observer.removed_count()); |
| 166 |
| 167 // Remove a ShelfModel item; |observer| should be notified in mash. |
| 168 controller->model()->RemoveItemAt(index); |
| 169 base::RunLoop().RunUntilIdle(); |
| 170 EXPECT_EQ(1, controller->model()->item_count()); |
| 171 EXPECT_EQ(2u, observer.added_count()); |
| 172 EXPECT_EQ(1u, observer.removed_count()); |
| 173 |
| 174 // Simulate adding an item remotely; Ash should apply the change. |
| 175 // |observer| is not notified; see mojom::ShelfController for rationale. |
| 176 controller->AddShelfItem(index, item); |
| 177 base::RunLoop().RunUntilIdle(); |
| 178 EXPECT_EQ(2, controller->model()->item_count()); |
| 179 EXPECT_EQ(2u, observer.added_count()); |
| 180 EXPECT_EQ(1u, observer.removed_count()); |
| 181 |
| 182 // Simulate removing an item remotely; Ash should apply the change. |
| 183 // |observer| is not notified; see mojom::ShelfController for rationale. |
| 184 controller->RemoveShelfItem(item.id); |
| 185 base::RunLoop().RunUntilIdle(); |
| 186 EXPECT_EQ(1, controller->model()->item_count()); |
| 187 EXPECT_EQ(2u, observer.added_count()); |
| 188 EXPECT_EQ(1u, observer.removed_count()); |
| 189 } |
| 190 |
| 191 } // namespace |
| 192 } // namespace ash |
OLD | NEW |