Index: ash/shelf/shelf_controller_unittest.cc |
diff --git a/ash/shelf/shelf_controller_unittest.cc b/ash/shelf/shelf_controller_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c12bb1954e5cf9ffae675d69d9bc2c8de8dc7b73 |
--- /dev/null |
+++ b/ash/shelf/shelf_controller_unittest.cc |
@@ -0,0 +1,185 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/shelf/shelf_controller.h" |
+ |
+#include <set> |
+#include <string> |
+ |
+#include "ash/public/cpp/config.h" |
+#include "ash/public/interfaces/shelf.mojom.h" |
+#include "ash/shelf/shelf.h" |
+#include "ash/shelf/shelf_model.h" |
+#include "ash/shelf/shelf_model_observer.h" |
+#include "ash/shell.h" |
+#include "ash/test/ash_test_base.h" |
+#include "mojo/public/cpp/bindings/associated_binding.h" |
+#include "ui/display/types/display_constants.h" |
+ |
+namespace ash { |
+namespace { |
+ |
+constexpr char kAppListId[] = "jlfapfmkapbjlfbpjedlinehodkccjee"; |
+ |
+// A test implementation of the ShelfObserver mojo interface. |
+class TestShelfObserver : public mojom::ShelfObserver { |
+ public: |
+ TestShelfObserver() = default; |
+ ~TestShelfObserver() override = default; |
+ |
+ // mojom::ShelfObserver: |
+ void OnShelfInitialized(int64_t display_id) override { |
+ display_id_ = display_id; |
+ } |
+ void OnAlignmentChanged(ShelfAlignment alignment, |
+ int64_t display_id) override { |
+ alignment_ = alignment; |
+ display_id_ = display_id; |
+ } |
+ void OnAutoHideBehaviorChanged(ShelfAutoHideBehavior auto_hide, |
+ int64_t display_id) override { |
+ auto_hide_ = auto_hide; |
+ display_id_ = display_id; |
+ } |
+ |
+ int64_t display_id() const { return display_id_; } |
+ ShelfAlignment alignment() const { return alignment_; } |
+ ShelfAutoHideBehavior auto_hide() const { return auto_hide_; } |
+ |
+ private: |
+ int64_t display_id_ = display::kInvalidDisplayId; |
+ ShelfAlignment alignment_ = SHELF_ALIGNMENT_BOTTOM_LOCKED; |
+ ShelfAutoHideBehavior auto_hide_ = SHELF_AUTO_HIDE_ALWAYS_HIDDEN; |
+ DISALLOW_COPY_AND_ASSIGN(TestShelfObserver); |
+}; |
+ |
+// A test implementation of the ShelfModelObserver mojo interface. |
+class TestShelfModelObserver : public mojom::ShelfModelObserver { |
+ public: |
+ TestShelfModelObserver() = default; |
+ ~TestShelfModelObserver() override = default; |
+ |
+ // mojom::ShelfModelObserver: |
+ void OnShelfItemAdded(int32_t, const ShelfItem&) override { added_count_++; } |
+ void OnShelfItemRemoved(int32_t, const ShelfItem&) override { |
+ removed_count_++; |
+ } |
+ void OnShelfItemMoved(int32_t, int32_t) override {} |
+ void OnShelfItemChanged(int32_t, const ShelfItem&) override {} |
+ void OnShelfItemDelegateChanged(const ShelfID&, |
+ mojom::ShelfItemDelegatePtr) override {} |
+ |
+ size_t added_count() const { return added_count_; } |
+ size_t removed_count() const { return removed_count_; } |
+ |
+ private: |
+ size_t added_count_ = 0; |
+ size_t removed_count_ = 0; |
+ DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver); |
+}; |
+ |
+using ShelfControllerTest = test::AshTestBase; |
+using NoSessionShelfControllerTest = test::NoSessionAshTestBase; |
+ |
+TEST_F(ShelfControllerTest, IntializesAppListItem) { |
+ ShelfController* controller = Shell::Get()->shelf_controller(); |
+ EXPECT_EQ(1, controller->model()->item_count()); |
+ EXPECT_EQ(kAppListId, controller->model()->items()[0].id.app_id); |
+} |
+ |
+TEST_F(NoSessionShelfControllerTest, NotifiesShelfObserver) { |
+ TestShelfObserver observer; |
+ mojom::ShelfObserverAssociatedPtr observer_ptr; |
+ mojo::AssociatedBinding<mojom::ShelfObserver> binding( |
+ &observer, mojo::MakeIsolatedRequest(&observer_ptr)); |
+ Shell::Get()->shelf_controller()->AddObserver(observer_ptr.PassInterface()); |
+ |
+ // Simulated login should initialize the primary shelf and notify |observer|. |
+ EXPECT_EQ(display::kInvalidDisplayId, observer.display_id()); |
+ EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM_LOCKED, observer.alignment()); |
+ EXPECT_EQ(SHELF_AUTO_HIDE_ALWAYS_HIDDEN, observer.auto_hide()); |
+ SetUserLoggedIn(true); |
+ SetSessionStarted(true); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(GetPrimaryDisplay().id(), observer.display_id()); |
+ EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM, observer.alignment()); |
+ EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_NEVER, observer.auto_hide()); |
+ |
+ // Changing shelf properties should notify |observer|. |
+ GetPrimaryShelf()->SetAlignment(SHELF_ALIGNMENT_LEFT); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(SHELF_ALIGNMENT_LEFT, observer.alignment()); |
+ GetPrimaryShelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS, observer.auto_hide()); |
+} |
+ |
+// A helper used as a callback for ShelfController::LinkShelfModels. |
+void LinkCallback(mojom::ShelfModelObserverPtr* ash_observer, |
+ base::RunLoop* run_loop, |
+ mojom::ShelfModelObserverPtr observer, |
+ const std::vector<ShelfItem>& items) { |
+ EXPECT_EQ(1u, items.size()); |
+ EXPECT_EQ(kAppListId, items[0].id.app_id); |
+ *ash_observer = std::move(observer); |
+ run_loop->Quit(); |
+} |
+ |
+TEST_F(ShelfControllerTest, LinkShelfModels) { |
+ // TODO(msw): Enable this in other configs when they use two shelf models. |
+ if (Shell::GetAshConfig() != Config::MASH) |
+ return; |
+ |
+ // A test observer that simulates Chrome's observation of Ash. |
+ TestShelfModelObserver test_observer; |
+ |
+ // An observer pointer for Ash's observation of this simulated user (Chrome). |
+ mojom::ShelfModelObserverPtr ash_observer; |
+ |
+ // Connect |test_observer| and wait for the callback to quit the run loop. |
+ base::RunLoop run_loop; |
+ mojo::Binding<mojom::ShelfModelObserver> binding(&test_observer); |
+ ShelfController* controller = Shell::Get()->shelf_controller(); |
+ controller->LinkShelfModels( |
+ binding.CreateInterfacePtrAndBind(), |
+ base::Bind(&LinkCallback, &ash_observer, &run_loop)); |
+ run_loop.Run(); |
+ |
+ // Add an item in Ash's ShelfModel; the test observer should be notified. |
+ EXPECT_EQ(1, controller->model()->item_count()); |
+ EXPECT_EQ(0u, test_observer.added_count()); |
+ EXPECT_EQ(0u, test_observer.removed_count()); |
+ ShelfItem item; |
+ item.type = TYPE_PINNED_APP; |
+ item.id = ShelfID("foo"); |
+ int index = controller->model()->Add(item); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(2, controller->model()->item_count()); |
+ EXPECT_EQ(1u, test_observer.added_count()); |
+ EXPECT_EQ(0u, test_observer.removed_count()); |
+ |
+ // Remove an item from Ash's ShelfModel; the test observer should be notified. |
+ controller->model()->RemoveItemAt(index); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(1, controller->model()->item_count()); |
+ EXPECT_EQ(1u, test_observer.added_count()); |
+ EXPECT_EQ(1u, test_observer.removed_count()); |
+ |
+ // Simulate adding a remote ShelfModel item; Ash should sync the change. |
+ ash_observer->OnShelfItemAdded(index, item); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(2, controller->model()->item_count()); |
+ EXPECT_EQ(1u, test_observer.added_count()); |
+ EXPECT_EQ(1u, test_observer.removed_count()); |
+ |
+ // Simulate removing the remote ShelfModel item; Ash should sync the change. |
+ ash_observer->OnShelfItemRemoved(index, item); |
+ base::RunLoop().RunUntilIdle(); |
+ EXPECT_EQ(1, controller->model()->item_count()); |
+ EXPECT_EQ(1u, test_observer.added_count()); |
+ EXPECT_EQ(1u, test_observer.removed_count()); |
+} |
+ |
+} // namespace |
+} // namespace ash |