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 constexpr char kAppListId[] = "jlfapfmkapbjlfbpjedlinehodkccjee"; |
| 24 |
| 25 // A test implementation of the ShelfObserver mojo interface. |
| 26 class TestShelfObserver : public mojom::ShelfObserver { |
| 27 public: |
| 28 TestShelfObserver() = default; |
| 29 ~TestShelfObserver() override = default; |
| 30 |
| 31 // mojom::ShelfObserver: |
| 32 void OnShelfInitialized(int64_t display_id) override { |
| 33 display_id_ = display_id; |
| 34 } |
| 35 void OnAlignmentChanged(ShelfAlignment alignment, |
| 36 int64_t display_id) override { |
| 37 alignment_ = alignment; |
| 38 display_id_ = display_id; |
| 39 } |
| 40 void OnAutoHideBehaviorChanged(ShelfAutoHideBehavior auto_hide, |
| 41 int64_t display_id) override { |
| 42 auto_hide_ = auto_hide; |
| 43 display_id_ = display_id; |
| 44 } |
| 45 |
| 46 int64_t display_id() const { return display_id_; } |
| 47 ShelfAlignment alignment() const { return alignment_; } |
| 48 ShelfAutoHideBehavior auto_hide() const { return auto_hide_; } |
| 49 |
| 50 private: |
| 51 int64_t display_id_ = display::kInvalidDisplayId; |
| 52 ShelfAlignment alignment_ = SHELF_ALIGNMENT_BOTTOM_LOCKED; |
| 53 ShelfAutoHideBehavior auto_hide_ = SHELF_AUTO_HIDE_ALWAYS_HIDDEN; |
| 54 DISALLOW_COPY_AND_ASSIGN(TestShelfObserver); |
| 55 }; |
| 56 |
| 57 // A test implementation of the ShelfModelObserver mojo interface. |
| 58 class TestShelfModelObserver : public mojom::ShelfModelObserver { |
| 59 public: |
| 60 TestShelfModelObserver() = default; |
| 61 ~TestShelfModelObserver() override = default; |
| 62 |
| 63 // mojom::ShelfModelObserver: |
| 64 void OnShelfItemAdded(int32_t, const ShelfItem&) override { added_count_++; } |
| 65 void OnShelfItemRemoved(int32_t, const ShelfItem&) override { |
| 66 removed_count_++; |
| 67 } |
| 68 void OnShelfItemMoved(int32_t, int32_t) override {} |
| 69 void OnShelfItemChanged(int32_t, const ShelfItem&) override {} |
| 70 void OnShelfItemDelegateChanged(const ShelfID&, |
| 71 mojom::ShelfItemDelegatePtr) override {} |
| 72 |
| 73 size_t added_count() const { return added_count_; } |
| 74 size_t removed_count() const { return removed_count_; } |
| 75 |
| 76 private: |
| 77 size_t added_count_ = 0; |
| 78 size_t removed_count_ = 0; |
| 79 DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver); |
| 80 }; |
| 81 |
| 82 using ShelfControllerTest = test::AshTestBase; |
| 83 using NoSessionShelfControllerTest = test::NoSessionAshTestBase; |
| 84 |
| 85 TEST_F(ShelfControllerTest, IntializesAppListItem) { |
| 86 ShelfController* controller = Shell::Get()->shelf_controller(); |
| 87 EXPECT_EQ(1, controller->model()->item_count()); |
| 88 EXPECT_EQ(kAppListId, controller->model()->items()[0].id.app_id); |
| 89 } |
| 90 |
| 91 TEST_F(NoSessionShelfControllerTest, NotifiesShelfObserver) { |
| 92 TestShelfObserver observer; |
| 93 mojom::ShelfObserverAssociatedPtr observer_ptr; |
| 94 mojo::AssociatedBinding<mojom::ShelfObserver> binding( |
| 95 &observer, mojo::MakeIsolatedRequest(&observer_ptr)); |
| 96 Shell::Get()->shelf_controller()->AddObserver(observer_ptr.PassInterface()); |
| 97 |
| 98 // Simulated login should initialize the primary shelf and notify |observer|. |
| 99 EXPECT_EQ(display::kInvalidDisplayId, observer.display_id()); |
| 100 EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM_LOCKED, observer.alignment()); |
| 101 EXPECT_EQ(SHELF_AUTO_HIDE_ALWAYS_HIDDEN, observer.auto_hide()); |
| 102 SetUserLoggedIn(true); |
| 103 SetSessionStarted(true); |
| 104 base::RunLoop().RunUntilIdle(); |
| 105 EXPECT_EQ(GetPrimaryDisplay().id(), observer.display_id()); |
| 106 EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM, observer.alignment()); |
| 107 EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_NEVER, observer.auto_hide()); |
| 108 |
| 109 // Changing shelf properties should notify |observer|. |
| 110 GetPrimaryShelf()->SetAlignment(SHELF_ALIGNMENT_LEFT); |
| 111 base::RunLoop().RunUntilIdle(); |
| 112 EXPECT_EQ(SHELF_ALIGNMENT_LEFT, observer.alignment()); |
| 113 GetPrimaryShelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); |
| 114 base::RunLoop().RunUntilIdle(); |
| 115 EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS, observer.auto_hide()); |
| 116 } |
| 117 |
| 118 // A helper used as a callback for ShelfController::LinkShelfModels. |
| 119 void LinkCallback(mojom::ShelfModelObserverPtr* ash_observer, |
| 120 base::RunLoop* run_loop, |
| 121 mojom::ShelfModelObserverPtr observer, |
| 122 const std::vector<ShelfItem>& items) { |
| 123 EXPECT_EQ(1u, items.size()); |
| 124 EXPECT_EQ(kAppListId, items[0].id.app_id); |
| 125 *ash_observer = std::move(observer); |
| 126 run_loop->Quit(); |
| 127 } |
| 128 |
| 129 TEST_F(ShelfControllerTest, LinkShelfModels) { |
| 130 // TODO(msw): Enable this in other configs when they use two shelf models. |
| 131 if (Shell::GetAshConfig() != Config::MASH) |
| 132 return; |
| 133 |
| 134 // A test observer that simulates Chrome's observation of Ash. |
| 135 TestShelfModelObserver test_observer; |
| 136 |
| 137 // An observer pointer for Ash's observation of this simulated user (Chrome). |
| 138 mojom::ShelfModelObserverPtr ash_observer; |
| 139 |
| 140 // Connect |test_observer| and wait for the callback to quit the run loop. |
| 141 base::RunLoop run_loop; |
| 142 mojo::Binding<mojom::ShelfModelObserver> binding(&test_observer); |
| 143 ShelfController* controller = Shell::Get()->shelf_controller(); |
| 144 controller->LinkShelfModels( |
| 145 binding.CreateInterfacePtrAndBind(), |
| 146 base::Bind(&LinkCallback, &ash_observer, &run_loop)); |
| 147 run_loop.Run(); |
| 148 |
| 149 // Add an item in Ash's ShelfModel; the test observer should be notified. |
| 150 EXPECT_EQ(1, controller->model()->item_count()); |
| 151 EXPECT_EQ(0u, test_observer.added_count()); |
| 152 EXPECT_EQ(0u, test_observer.removed_count()); |
| 153 ShelfItem item; |
| 154 item.type = TYPE_PINNED_APP; |
| 155 item.id = ShelfID("foo"); |
| 156 int index = controller->model()->Add(item); |
| 157 base::RunLoop().RunUntilIdle(); |
| 158 EXPECT_EQ(2, controller->model()->item_count()); |
| 159 EXPECT_EQ(1u, test_observer.added_count()); |
| 160 EXPECT_EQ(0u, test_observer.removed_count()); |
| 161 |
| 162 // Remove an item from Ash's ShelfModel; the test observer should be notified. |
| 163 controller->model()->RemoveItemAt(index); |
| 164 base::RunLoop().RunUntilIdle(); |
| 165 EXPECT_EQ(1, controller->model()->item_count()); |
| 166 EXPECT_EQ(1u, test_observer.added_count()); |
| 167 EXPECT_EQ(1u, test_observer.removed_count()); |
| 168 |
| 169 // Simulate adding a remote ShelfModel item; Ash should sync the change. |
| 170 ash_observer->OnShelfItemAdded(index, item); |
| 171 base::RunLoop().RunUntilIdle(); |
| 172 EXPECT_EQ(2, controller->model()->item_count()); |
| 173 EXPECT_EQ(1u, test_observer.added_count()); |
| 174 EXPECT_EQ(1u, test_observer.removed_count()); |
| 175 |
| 176 // Simulate removing the remote ShelfModel item; Ash should sync the change. |
| 177 ash_observer->OnShelfItemRemoved(index, item); |
| 178 base::RunLoop().RunUntilIdle(); |
| 179 EXPECT_EQ(1, controller->model()->item_count()); |
| 180 EXPECT_EQ(1u, test_observer.added_count()); |
| 181 EXPECT_EQ(1u, test_observer.removed_count()); |
| 182 } |
| 183 |
| 184 } // namespace |
| 185 } // namespace ash |
OLD | NEW |