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

Side by Side Diff: ash/shelf/shelf_controller_unittest.cc

Issue 2833173002: mash: Support ShelfModel access in Chrome. (Closed)
Patch Set: Refine init pattern; add AppList item in ShelfModel ctor. Created 3 years, 6 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 unified diff | Download patch
OLDNEW
(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(int32_t, const ShelfItem&) override {
45 removed_count_++;
46 }
47 void OnShelfItemMoved(int32_t, int32_t) override {}
48 void OnShelfItemChanged(int32_t, const ShelfItem&) override {}
49 void OnShelfItemDelegateChanged(const ShelfID&,
50 mojom::ShelfItemDelegatePtr) override {}
51
52 int64_t display_id() const { return display_id_; }
53 ShelfAlignment alignment() const { return alignment_; }
54 ShelfAutoHideBehavior auto_hide() const { return auto_hide_; }
55 size_t added_count() const { return added_count_; }
56 size_t removed_count() const { return removed_count_; }
57
58 private:
59 int64_t display_id_ = display::kInvalidDisplayId;
60 ShelfAlignment alignment_ = SHELF_ALIGNMENT_BOTTOM_LOCKED;
61 ShelfAutoHideBehavior auto_hide_ = SHELF_AUTO_HIDE_ALWAYS_HIDDEN;
62 size_t added_count_ = 0;
63 size_t removed_count_ = 0;
64
65 DISALLOW_COPY_AND_ASSIGN(TestShelfObserver);
66 };
67
68 using ShelfControllerTest = test::AshTestBase;
69 using NoSessionShelfControllerTest = test::NoSessionAshTestBase;
70
71 TEST_F(ShelfControllerTest, IntializesAppListItemDelegate) {
72 ShelfModel* model = Shell::Get()->shelf_controller()->model();
73 EXPECT_EQ(1, model->item_count());
74 EXPECT_EQ(kAppListId, model->items()[0].id.app_id);
75 EXPECT_TRUE(model->GetShelfItemDelegate(ShelfID(kAppListId)));
76 }
77
78 TEST_F(NoSessionShelfControllerTest, NotifiesShelfObserver) {
79 ShelfController* controller = Shell::Get()->shelf_controller();
80 TestShelfObserver observer;
81 mojom::ShelfObserverAssociatedPtr observer_ptr;
82 mojo::AssociatedBinding<mojom::ShelfObserver> binding(
83 &observer, mojo::MakeIsolatedRequest(&observer_ptr));
84 controller->AddObserver(observer_ptr.PassInterface());
85
86 // Simulated login should initialize the primary shelf and notify |observer|.
87 EXPECT_EQ(display::kInvalidDisplayId, observer.display_id());
88 EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM_LOCKED, observer.alignment());
89 EXPECT_EQ(SHELF_AUTO_HIDE_ALWAYS_HIDDEN, observer.auto_hide());
90 SetUserLoggedIn(true);
91 SetSessionStarted(true);
92 base::RunLoop().RunUntilIdle();
93 EXPECT_EQ(GetPrimaryDisplay().id(), observer.display_id());
94 EXPECT_EQ(SHELF_ALIGNMENT_BOTTOM, observer.alignment());
95 EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_NEVER, observer.auto_hide());
96
97 // Changing shelf properties should notify |observer|.
98 GetPrimaryShelf()->SetAlignment(SHELF_ALIGNMENT_LEFT);
99 base::RunLoop().RunUntilIdle();
100 EXPECT_EQ(SHELF_ALIGNMENT_LEFT, observer.alignment());
101 GetPrimaryShelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
102 base::RunLoop().RunUntilIdle();
103 EXPECT_EQ(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS, observer.auto_hide());
104
105 // The ShelfModel should be initialized with a single item for the AppList.
James Cook 2017/05/30 22:47:47 I think this test would be clearer if you took the
msw 2017/05/31 16:55:21 Done.
106 // In mash, the observer is immediately notified of existing shelf items.
107 const bool is_mash = Shell::GetAshConfig() == Config::MASH;
108 EXPECT_EQ(1, controller->model()->item_count());
109 EXPECT_EQ(is_mash ? 1u : 0u, observer.added_count());
110 EXPECT_EQ(0u, observer.removed_count());
111
112 // Add a ShelfModel item; |observer| should be notified in mash.
113 ShelfItem item;
114 item.type = TYPE_PINNED_APP;
115 item.id = ShelfID("foo");
116 int index = controller->model()->Add(item);
117 base::RunLoop().RunUntilIdle();
118 EXPECT_EQ(2, controller->model()->item_count());
119 EXPECT_EQ(is_mash ? 2u : 0u, observer.added_count());
120 EXPECT_EQ(0u, observer.removed_count());
121
122 // Remove a ShelfModel item; |observer| should be notified in mash.
123 controller->model()->RemoveItemAt(index);
124 base::RunLoop().RunUntilIdle();
125 EXPECT_EQ(1, controller->model()->item_count());
126 EXPECT_EQ(is_mash ? 2u : 0u, observer.added_count());
127 EXPECT_EQ(is_mash ? 1u : 0u, observer.removed_count());
128
129 // The following code is only applicable to the Mash config.
130 if (!is_mash)
131 return;
132
133 // Simulate adding an item remotely; Ash should apply the change.
134 // |observer| is not notified; see mojom::ShelfController for rationale.
135 controller->AddShelfItem(index, item);
136 base::RunLoop().RunUntilIdle();
137 EXPECT_EQ(2, controller->model()->item_count());
138 EXPECT_EQ(is_mash ? 2u : 0u, observer.added_count());
139 EXPECT_EQ(is_mash ? 1u : 0u, observer.removed_count());
140
141 // Simulate removing an item remotely; Ash should apply the change.
142 // |observer| is not notified; see mojom::ShelfController for rationale.
143 controller->RemoveShelfItem(item.id);
144 base::RunLoop().RunUntilIdle();
145 EXPECT_EQ(1, controller->model()->item_count());
146 EXPECT_EQ(is_mash ? 2u : 0u, observer.added_count());
147 EXPECT_EQ(is_mash ? 1u : 0u, observer.removed_count());
148 }
James Cook 2017/05/30 22:47:47 Thanks for adding tests.
msw 2017/05/31 16:55:21 Acknowledged.
149
150 } // namespace
151 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698