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

Side by Side Diff: ash/tray_action/tray_action_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 "ash/tray_action/tray_action.h"
6
7 #include <memory>
8 #include <vector>
9
10 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/tray_action/tray_action_observer.h"
13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/run_loop.h"
16
17 using ash::mojom::TrayActionState;
18
19 namespace ash {
20
21 namespace {
22
23 class ScopedTestStateObserver : public TrayActionObserver {
24 public:
25 explicit ScopedTestStateObserver(TrayAction* tray_action)
26 : tray_action_(tray_action) {
27 tray_action_->AddObserver(this);
28 }
29
30 ~ScopedTestStateObserver() override { tray_action_->RemoveObserver(this); }
31
32 void OnLockScreenNoteStateChanged(TrayActionState state) override {
33 observed_states_.push_back(state);
34 }
35
36 const std::vector<TrayActionState>& observed_states() const {
37 return observed_states_;
38 }
39
40 void ClearObservedStates() { observed_states_.clear(); }
41
42 private:
43 TrayAction* tray_action_;
44
45 std::vector<TrayActionState> observed_states_;
46
47 DISALLOW_COPY_AND_ASSIGN(ScopedTestStateObserver);
48 };
49
50 class TestTrayActionClient : public mojom::TrayActionClient {
51 public:
52 TestTrayActionClient() : binding_(this) {}
53
54 ~TestTrayActionClient() override = default;
55
56 mojom::TrayActionClientPtr CreateInterfacePtrAndBind() {
57 return binding_.CreateInterfacePtrAndBind();
58 }
59
60 void RequestNewLockScreenNote() override { action_requests_count_++; }
James Cook 2017/05/05 02:55:44 nit: "// mojom::TrayActionClient:" above
tbarzic 2017/05/05 04:46:27 Done.
61
62 int action_requests_count() const { return action_requests_count_; }
63
64 void reset_action_requests_count() { action_requests_count_ = 0; }
James Cook 2017/05/05 02:55:45 nit: ResetActionRequestsCount().
tbarzic 2017/05/05 04:46:26 Done.
65
66 private:
67 mojo::Binding<ash::mojom::TrayActionClient> binding_;
68
69 int action_requests_count_ = 0;
70
71 DISALLOW_COPY_AND_ASSIGN(TestTrayActionClient);
72 };
73
74 using TrayActionTest = test::AshTestBase;
75
76 } // namespace
77
78 TEST_F(TrayActionTest, NoTrayAction) {
79 TrayAction* tray_action = Shell::Get()->tray_action();
80 ScopedTestStateObserver observer(tray_action);
81
82 EXPECT_EQ(TrayActionState::kNotSupported,
83 tray_action->GetLockScreenNoteState());
84
85 tray_action->UpdateLockScreenNoteState(TrayActionState::kAvailable);
86
87 // The effective state should be |kNotSupported| as long as an action handler
88 // is not set.
89 EXPECT_EQ(TrayActionState::kNotSupported,
90 tray_action->GetLockScreenNoteState());
91 EXPECT_EQ(0u, observer.observed_states().size());
92
93 // Adding action handler should update the tray_action state.
94 std::unique_ptr<TestTrayActionClient> action_client =
95 base::MakeUnique<TestTrayActionClient>();
96 tray_action->SetClient(action_client->CreateInterfacePtrAndBind(),
97 TrayActionState::kNotSupported);
98
99 EXPECT_EQ(TrayActionState::kNotSupported,
100 tray_action->GetLockScreenNoteState());
101 // Effective state hasn't changed, so observer should be notified of it.
James Cook 2017/05/05 02:55:45 nit: "should be" -> "should not be", or just remov
tbarzic 2017/05/05 04:46:26 Done.
102 ASSERT_EQ(0u, observer.observed_states().size());
James Cook 2017/05/05 02:55:44 EXPECT_EQ
tbarzic 2017/05/05 04:46:26 Done.
103
104 tray_action->SetClient(nullptr, TrayActionState::kNotSupported);
105 action_client.reset();
106 base::RunLoop().RunUntilIdle();
107
108 EXPECT_EQ(TrayActionState::kNotSupported,
109 tray_action->GetLockScreenNoteState());
110 ASSERT_EQ(0u, observer.observed_states().size());
James Cook 2017/05/05 02:55:45 EXPECT_EQ
tbarzic 2017/05/05 04:46:26 Done.
111 }
112
113 TEST_F(TrayActionTest, SettingInitialState) {
114 TrayAction* tray_action = Shell::Get()->tray_action();
115
116 ScopedTestStateObserver observer(tray_action);
117 TestTrayActionClient action_client;
118 tray_action->SetClient(action_client.CreateInterfacePtrAndBind(),
119 TrayActionState::kAvailable);
120
121 EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
122 ASSERT_EQ(1u, observer.observed_states().size());
123 EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
124 observer.ClearObservedStates();
James Cook 2017/05/05 02:55:45 do you have to do this?
tbarzic 2017/05/05 04:46:26 Done.
125
126 tray_action->SetClient(nullptr, TrayActionState::kNotSupported);
James Cook 2017/05/05 02:55:45 do you have to do this?
tbarzic 2017/05/05 04:46:27 Done.
127 }
128
129 TEST_F(TrayActionTest, StateChangeNotificationOnConnectionLoss) {
130 TrayAction* tray_action = Shell::Get()->tray_action();
131
132 ScopedTestStateObserver observer(tray_action);
133 std::unique_ptr<TestTrayActionClient> action_client(
134 new TestTrayActionClient());
135 tray_action->SetClient(action_client->CreateInterfacePtrAndBind(),
136 TrayActionState::kAvailable);
137
138 EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
139 ASSERT_EQ(1u, observer.observed_states().size());
140 EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
141 observer.ClearObservedStates();
142
143 action_client.reset();
144 base::RunLoop().RunUntilIdle();
145
146 EXPECT_EQ(TrayActionState::kNotSupported,
147 tray_action->GetLockScreenNoteState());
148 ASSERT_EQ(1u, observer.observed_states().size());
149 EXPECT_EQ(TrayActionState::kNotSupported, observer.observed_states()[0]);
150 observer.ClearObservedStates();
James Cook 2017/05/05 02:55:45 ditto
tbarzic 2017/05/05 04:46:26 Done.
151
152 tray_action->SetClient(nullptr, TrayActionState::kNotSupported);
James Cook 2017/05/05 02:55:44 ditto, and below
tbarzic 2017/05/05 04:46:26 Done.
153 }
154
155 TEST_F(TrayActionTest, StateChangesWithHandlerSet) {
James Cook 2017/05/05 02:55:44 This test is kinda duplicated by the NormalStatePr
tbarzic 2017/05/05 04:46:27 Done.
156 TrayAction* tray_action = Shell::Get()->tray_action();
157
158 ScopedTestStateObserver observer(tray_action);
159 TestTrayActionClient action_client;
160 tray_action->SetClient(action_client.CreateInterfacePtrAndBind(),
161 TrayActionState::kNotSupported);
162
163 tray_action->UpdateLockScreenNoteState(TrayActionState::kAvailable);
164 EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
165 ASSERT_EQ(1u, observer.observed_states().size());
166 EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
167 observer.ClearObservedStates();
168
169 tray_action->UpdateLockScreenNoteState(TrayActionState::kAvailable);
170 EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
171 // No real state change, so the observer should not be notified.
172 ASSERT_EQ(0u, observer.observed_states().size());
173
174 tray_action->UpdateLockScreenNoteState(TrayActionState::kActive);
175 EXPECT_EQ(TrayActionState::kActive, tray_action->GetLockScreenNoteState());
176 ASSERT_EQ(1u, observer.observed_states().size());
177 EXPECT_EQ(TrayActionState::kActive, observer.observed_states()[0]);
178 observer.ClearObservedStates();
179
180 tray_action->SetClient(nullptr, TrayActionState::kNotSupported);
181 }
182
183 TEST_F(TrayActionTest, NormalStateProgression) {
184 TrayAction* tray_action = Shell::Get()->tray_action();
185
186 ScopedTestStateObserver observer(tray_action);
187 TestTrayActionClient action_client;
188 tray_action->SetClient(action_client.CreateInterfacePtrAndBind(),
189 TrayActionState::kNotSupported);
190
191 tray_action->UpdateLockScreenNoteState(TrayActionState::kAvailable);
192 EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
193 ASSERT_EQ(1u, observer.observed_states().size());
194 EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
195 observer.ClearObservedStates();
196
197 tray_action->UpdateLockScreenNoteState(TrayActionState::kLaunching);
198 EXPECT_EQ(TrayActionState::kLaunching, tray_action->GetLockScreenNoteState());
199 ASSERT_EQ(1u, observer.observed_states().size());
200 EXPECT_EQ(TrayActionState::kLaunching, observer.observed_states()[0]);
201 observer.ClearObservedStates();
202
203 tray_action->UpdateLockScreenNoteState(TrayActionState::kBackground);
204 EXPECT_EQ(TrayActionState::kBackground,
205 tray_action->GetLockScreenNoteState());
206 ASSERT_EQ(1u, observer.observed_states().size());
207 EXPECT_EQ(TrayActionState::kBackground, observer.observed_states()[0]);
208 observer.ClearObservedStates();
209
210 tray_action->UpdateLockScreenNoteState(TrayActionState::kNotSupported);
211 EXPECT_EQ(TrayActionState::kNotSupported,
212 tray_action->GetLockScreenNoteState());
213 ASSERT_EQ(1u, observer.observed_states().size());
214 EXPECT_EQ(TrayActionState::kNotSupported, observer.observed_states()[0]);
215 observer.ClearObservedStates();
216
217 tray_action->SetClient(nullptr, TrayActionState::kNotSupported);
218 }
219
220 TEST_F(TrayActionTest, ObserversNotNotifiedOnDuplicateState) {
221 TrayAction* tray_action = Shell::Get()->tray_action();
222
223 ScopedTestStateObserver observer(tray_action);
224 TestTrayActionClient action_client;
225 tray_action->SetClient(action_client.CreateInterfacePtrAndBind(),
226 TrayActionState::kNotSupported);
227
228 tray_action->UpdateLockScreenNoteState(TrayActionState::kAvailable);
229 EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
230 ASSERT_EQ(1u, observer.observed_states().size());
231 EXPECT_EQ(TrayActionState::kAvailable, observer.observed_states()[0]);
232 observer.ClearObservedStates();
233
234 tray_action->UpdateLockScreenNoteState(TrayActionState::kAvailable);
235 EXPECT_EQ(TrayActionState::kAvailable, tray_action->GetLockScreenNoteState());
236 ASSERT_EQ(0u, observer.observed_states().size());
237
238 tray_action->SetClient(nullptr, TrayActionState::kNotSupported);
239 }
240
241 TEST_F(TrayActionTest, RequestAction) {
242 TrayAction* tray_action = Shell::Get()->tray_action();
243
244 TestTrayActionClient action_client;
245 tray_action->SetClient(action_client.CreateInterfacePtrAndBind(),
246 TrayActionState::kNotSupported);
247
248 EXPECT_EQ(0, action_client.action_requests_count());
249 tray_action->RequestNewLockScreenNote();
250 RunAllPendingInMessageLoop();
251 EXPECT_EQ(1, action_client.action_requests_count());
James Cook 2017/05/05 02:55:45 Why does it get a request if it set the state as "
tbarzic 2017/05/05 04:46:26 Yeah, updated the logic to check that the action c
James Cook 2017/05/05 17:22:46 After more consideration, I think you should elimi
tbarzic 2017/05/05 17:51:17 OK, there are subtle differences, but probably not
252
253 tray_action->SetClient(nullptr, TrayActionState::kNotSupported);
254 }
255
256 // Tests that there is no crash if handler is not set.
257 TEST_F(TrayActionTest, RequestActionWithNoHandler) {
258 TrayAction* tray_action = Shell::Get()->tray_action();
259 tray_action->RequestNewLockScreenNote();
260 }
261
262 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698