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

Side by Side Diff: ash/tray_action_handler/tray_action_handler_controller_unittest.cc

Issue 2848813002: Introduce ash mojo interface for lock screen action handlers (Closed)
Patch Set: . Created 3 years, 7 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 <memory>
6 #include <vector>
7
8 #include "ash/shell.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/tray_action_handler/tray_action_handler_controller.h"
James Cook 2017/05/03 23:31:51 nit: put this include at the top
tbarzic 2017/05/04 20:58:30 Done.
11 #include "ash/tray_action_handler/tray_action_handler_observer.h"
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14
15 namespace ash {
16
17 namespace {
18
19 class ScopedTestStateObserver : public TrayActionHandlerObserver {
20 public:
21 explicit ScopedTestStateObserver(TrayActionHandlerController* controller)
22 : state_controller_(controller) {
23 state_controller_->AddObserver(this);
24 }
25
26 ~ScopedTestStateObserver() override {
27 state_controller_->RemoveObserver(this);
28 }
29
30 void OnActionStateChanged(mojom::TrayActionHandlerAction action,
31 mojom::TrayActionHandlerState state) override {
32 ASSERT_EQ(mojom::TrayActionHandlerAction::kNewLockScreenNote, action);
33
34 observed_states_.push_back(state);
35 }
36
37 const std::vector<mojom::TrayActionHandlerState>& observed_states() const {
38 return observed_states_;
39 }
40
41 void ClearObservedStates() { observed_states_.clear(); }
42
43 private:
44 TrayActionHandlerController* state_controller_;
45
46 std::vector<mojom::TrayActionHandlerState> observed_states_;
47
48 DISALLOW_COPY_AND_ASSIGN(ScopedTestStateObserver);
49 };
50
51 class ScopedTestTrayActionHandler : public mojom::TrayActionHandlerClient {
James Cook 2017/05/03 23:31:51 nit: I think the tests would be easier to understa
tbarzic 2017/05/04 20:58:31 Done.
52 public:
53 explicit ScopedTestTrayActionHandler(TrayActionHandlerController* controller)
54 : state_controller_(controller), binding_(this) {
55 state_controller_->SetClient(binding_.CreateInterfacePtrAndBind());
56 }
57
58 ~ScopedTestTrayActionHandler() override {
59 state_controller_->SetClient(nullptr);
60 }
61
62 void RequestHandleAction(
63 ash::mojom::TrayActionHandlerAction action) override {
64 ASSERT_EQ(mojom::TrayActionHandlerAction::kNewLockScreenNote, action);
65
66 action_requests_count_++;
67 }
68
69 int action_requests_count() const { return action_requests_count_; }
70
71 void reset_action_requests_count() { action_requests_count_ = 0; }
72
73 private:
74 TrayActionHandlerController* state_controller_;
75 mojo::Binding<ash::mojom::TrayActionHandlerClient> binding_;
76
77 int action_requests_count_ = 0;
78
79 DISALLOW_COPY_AND_ASSIGN(ScopedTestTrayActionHandler);
80 };
81
82 class TrayActionHandlerControllerTest : public test::AshTestBase {
James Cook 2017/05/03 23:31:51 optional: "using TrayActionHandlerControllerTest =
tbarzic 2017/05/04 20:58:30 Done.
83 public:
84 TrayActionHandlerControllerTest() {}
James Cook 2017/05/03 23:31:51 optional nit: = default instead
tbarzic 2017/05/04 20:58:30 Done.
85 ~TrayActionHandlerControllerTest() override {}
86
87 TrayActionHandlerController* GetController() {
88 return Shell::Get()->tray_action_handler_controller();
89 }
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(TrayActionHandlerControllerTest);
93 };
94
95 } // namespace
James Cook 2017/05/03 23:31:51 nit: can this go at the bottom (put all the test c
tbarzic 2017/05/04 20:58:30 I vaguely remember a chromium-dev thread that sugg
96
97 TEST_F(TrayActionHandlerControllerTest, NoTrayActionHandler) {
98 TrayActionHandlerController* controller = GetController();
99 ScopedTestStateObserver observer(controller);
100
101 EXPECT_EQ(
102 mojom::TrayActionHandlerState::kNotSupported,
James Cook 2017/05/03 23:31:51 optional: I think you may be able to do "using moj
tbarzic 2017/05/04 20:58:30 Done.
103 controller->GetState(mojom::TrayActionHandlerAction::kNewLockScreenNote));
104
105 controller->UpdateActionHandlerState(
106 mojom::TrayActionHandlerAction::kNewLockScreenNote,
107 mojom::TrayActionHandlerState::kAvailable);
108
109 // The effective state should be |kNotSupported| as long as an action handler
110 // is not set.
111 EXPECT_EQ(
112 mojom::TrayActionHandlerState::kNotSupported,
113 controller->GetState(mojom::TrayActionHandlerAction::kNewLockScreenNote));
114 EXPECT_EQ(0u, observer.observed_states().size());
115
116 // Adding action handler should update the controller state.
117 std::unique_ptr<ScopedTestTrayActionHandler> scoped_action_handler =
118 base::MakeUnique<ScopedTestTrayActionHandler>(controller);
119
120 EXPECT_EQ(
121 mojom::TrayActionHandlerState::kAvailable,
122 controller->GetState(mojom::TrayActionHandlerAction::kNewLockScreenNote));
123 // Effective state changed, so observer should be notified of it.
124 ASSERT_EQ(1u, observer.observed_states().size());
125 EXPECT_EQ(mojom::TrayActionHandlerState::kAvailable,
126 observer.observed_states()[0]);
127 observer.ClearObservedStates();
128
129 // When action handler is reset, effective state should go back to
130 // |kNotSupported|.
131 scoped_action_handler.reset();
132
133 EXPECT_EQ(
134 mojom::TrayActionHandlerState::kNotSupported,
135 controller->GetState(mojom::TrayActionHandlerAction::kNewLockScreenNote));
136 // Effective state changed, so observer should be notified of it.
137 ASSERT_EQ(1u, observer.observed_states().size());
138 EXPECT_EQ(mojom::TrayActionHandlerState::kNotSupported,
139 observer.observed_states()[0]);
140 observer.ClearObservedStates();
141 }
142
143 TEST_F(TrayActionHandlerControllerTest, StateChangesWithHandlerSet) {
144 TrayActionHandlerController* controller = GetController();
145
146 ScopedTestStateObserver observer(controller);
147 ScopedTestTrayActionHandler action_handler(controller);
148
149 controller->UpdateActionHandlerState(
150 mojom::TrayActionHandlerAction::kNewLockScreenNote,
151 mojom::TrayActionHandlerState::kAvailable);
152 EXPECT_EQ(
153 mojom::TrayActionHandlerState::kAvailable,
154 controller->GetState(mojom::TrayActionHandlerAction::kNewLockScreenNote));
155 ASSERT_EQ(1u, observer.observed_states().size());
156 EXPECT_EQ(mojom::TrayActionHandlerState::kAvailable,
157 observer.observed_states()[0]);
158 observer.ClearObservedStates();
159
160 controller->UpdateActionHandlerState(
161 mojom::TrayActionHandlerAction::kNewLockScreenNote,
162 mojom::TrayActionHandlerState::kAvailable);
163 EXPECT_EQ(
164 mojom::TrayActionHandlerState::kAvailable,
165 controller->GetState(mojom::TrayActionHandlerAction::kNewLockScreenNote));
166 // No real state change, so the observer should not be notified.
167 ASSERT_EQ(0u, observer.observed_states().size());
168
169 controller->UpdateActionHandlerState(
170 mojom::TrayActionHandlerAction::kNewLockScreenNote,
171 mojom::TrayActionHandlerState::kActive);
172 EXPECT_EQ(
173 mojom::TrayActionHandlerState::kActive,
174 controller->GetState(mojom::TrayActionHandlerAction::kNewLockScreenNote));
175 ASSERT_EQ(1u, observer.observed_states().size());
176 EXPECT_EQ(mojom::TrayActionHandlerState::kActive,
177 observer.observed_states()[0]);
178 observer.ClearObservedStates();
179 }
James Cook 2017/05/03 23:31:51 I suggest one test that moves through the expected
tbarzic 2017/05/04 20:58:30 Done.
180
181 TEST_F(TrayActionHandlerControllerTest, RequestAction) {
182 TrayActionHandlerController* controller = GetController();
183
184 ScopedTestTrayActionHandler action_handler(controller);
185
186 EXPECT_EQ(0, action_handler.action_requests_count());
187 controller->RequestHandleAction(
188 mojom::TrayActionHandlerAction::kNewLockScreenNote);
189 RunAllPendingInMessageLoop();
190 EXPECT_EQ(1, action_handler.action_requests_count());
191 }
192
193 // Tests that there is no crash if handler is not set.
194 TEST_F(TrayActionHandlerControllerTest, RequestActionWithNoHandler) {
195 TrayActionHandlerController* controller = GetController();
196 controller->RequestHandleAction(
197 mojom::TrayActionHandlerAction::kNewLockScreenNote);
198 }
199
200 } // namespace ash
James Cook 2017/05/03 23:31:51 Thanks for writing a nice test suite!
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698