1// Copyright 2013 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 "chrome/browser/chromeos/file_manager/desktop_notifications.h"
6
7#include <string>
8
9#include "base/memory/scoped_ptr.h"
10#include "base/strings/utf_string_conversions.h"
11#include "grit/generated_resources.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "ui/base/l10n/l10n_util.h"
14
15namespace file_manager {
16
17namespace {
18
19// This class records parameters for functions to show and hide
20// notifications.
21class RecordedDesktopNotifications : public DesktopNotifications {
22 public:
23  explicit RecordedDesktopNotifications(Profile* profile)
24      : DesktopNotifications(profile) {
25  }
26
27  virtual ~RecordedDesktopNotifications() {}
28
29  virtual void ShowNotificationWithMessage(
30      NotificationType type,
31      const std::string& path,
32      const base::string16& message)  OVERRIDE {
33    ShowAndHideParams params;
34    params.event = SHOW;
35    params.type = type;
36    params.path = path;
37    params.message = message;
38    params_.push_back(params);
39  }
40
41  virtual void HideNotification(NotificationType type,
42                                const std::string& path) OVERRIDE {
43    ShowAndHideParams params;
44    params.event = HIDE;
45    params.type = type;
46    params.path = path;
47    params_.push_back(params);
48  }
49
50  enum Event {
51    SHOW,
52    HIDE,
53  };
54
55  // Used to record parameters passed to ShowNotificationWithMessage() and
56  // HideNotification().
57  struct ShowAndHideParams {
58    Event event;
59    NotificationType type;
60    std::string path;
61    base::string16 message;  // Empty for HideNotification().
62  };
63
64  // Returns parameters passed to ShowNotificationWithMessage() and
65  // HideNotificationParams().
66  const std::vector<ShowAndHideParams>& params() const {
67    return params_;
68  }
69
70 private:
71  std::vector<ShowAndHideParams> params_;
72};
73
74}  // namespace
75
76TEST(FileManagerMountNotificationsTest, GoodDevice) {
77  RecordedDesktopNotifications notifications(NULL);
78
79  std::string notification_path("system_path_prefix");
80  std::string device_label("label");
81
82  notifications.RegisterDevice(notification_path);
83
84  notifications.ManageNotificationsOnMountCompleted(
85      notification_path,
86      device_label,
87      true  /* is_parent */,
88      true  /* success */,
89      false  /* is_unsupported */);
90  // Should hide a DEVICE notification.
91  ASSERT_EQ(1U, notifications.params().size());
92  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
93            notifications.params()[0].event);
94  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
95  EXPECT_EQ(notification_path, notifications.params()[0].path);
96};
97
98TEST(FileManagerMountNotificationsTest, GoodDeviceWithBadParent) {
99  RecordedDesktopNotifications notifications(NULL);
100
101  std::string notification_path("system_path_prefix");
102  std::string device_label("label");
103
104  notifications.RegisterDevice(notification_path);
105
106  notifications.ManageNotificationsOnMountCompleted(
107      notification_path,
108      device_label,
109      true  /* is_parent */,
110      false  /* success */,
111      false  /* is_unsupported */);
112  ASSERT_EQ(2U, notifications.params().size());
113  // Should hide DEVICE notification.
114  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
115            notifications.params()[0].event);
116  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
117  EXPECT_EQ(notification_path, notifications.params()[0].path);
118  // Should show a DEVICE_FAIL notification.
119  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
120            notifications.params()[1].event);
121  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
122            notifications.params()[1].type);
123  EXPECT_EQ(notification_path, notifications.params()[0].path);
124
125  notifications.ManageNotificationsOnMountCompleted(
126      notification_path,
127      device_label,
128      false  /* is_parent */,
129      true  /* success */,
130      false  /* is_unsupported */);
131  ASSERT_EQ(3U, notifications.params().size());
132  // Should hide a DEVICE_FAIL notification.
133  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
134            notifications.params()[2].event);
135  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
136            notifications.params()[2].type);
137  EXPECT_EQ(notification_path, notifications.params()[2].path);
138
139  notifications.ManageNotificationsOnMountCompleted(
140      notification_path,
141      device_label,
142      false  /* is_parent */,
143      true  /* success */,
144      false  /* is_unsupported */);
145  // Should do nothing this time.
146  ASSERT_EQ(3U, notifications.params().size());
147}
148
149TEST(FileManagerMountNotificationsTest, UnsupportedDevice) {
150  RecordedDesktopNotifications notifications(NULL);
151
152  std::string notification_path("system_path_prefix");
153  std::string device_label("label");
154
155  notifications.RegisterDevice(notification_path);
156
157  notifications.ManageNotificationsOnMountCompleted(
158      notification_path,
159      device_label,
160      false  /* is_parent */,
161      false  /* success */,
162      true  /* is_unsupported */);
163  ASSERT_EQ(2U, notifications.params().size());
164  // Should hide DEVICE notification.
165  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
166            notifications.params()[0].event);
167  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
168  EXPECT_EQ(notification_path, notifications.params()[0].path);
169  // And should show a DEVICE_FAIL notification.
170  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
171            notifications.params()[1].event);
172  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
173            notifications.params()[1].type);
174  EXPECT_EQ(notification_path, notifications.params()[1].path);
175  EXPECT_EQ(
176      l10n_util::GetStringFUTF16(IDS_DEVICE_UNSUPPORTED_MESSAGE,
177                                 UTF8ToUTF16(device_label)),
178      notifications.params()[1].message);
179}
180
181TEST(FileManagerMountNotificationsTest, UnsupportedWithUnknownParent) {
182  RecordedDesktopNotifications notifications(NULL);
183
184  std::string notification_path("system_path_prefix");
185  std::string device_label("label");
186
187  notifications.RegisterDevice(notification_path);
188
189  notifications.ManageNotificationsOnMountCompleted(
190      notification_path,
191      device_label,
192      true  /* is_parent */,
193      false  /* success */,
194      false  /* is_unsupported */);
195  ASSERT_EQ(2U, notifications.params().size());
196  // Should hide DEVICE notification.
197  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
198            notifications.params()[0].event);
199  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
200  EXPECT_EQ(notification_path, notifications.params()[0].path);
201  // And should show a DEVICE_FAIL notification.
202  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
203            notifications.params()[1].event);
204  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
205            notifications.params()[1].type);
206  EXPECT_EQ(notification_path, notifications.params()[1].path);
207
208  notifications.ManageNotificationsOnMountCompleted(
209      notification_path,
210      device_label,
211      false  /* is_parent */,
212      false  /* success */,
213      true  /* is_unsupported */);
214  ASSERT_EQ(4U, notifications.params().size());
215  // Should hide DEVICE_FAIL notification.
216  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
217            notifications.params()[2].event);
218  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
219            notifications.params()[2].type);
220  EXPECT_EQ(notification_path, notifications.params()[2].path);
221  // Should show DEVICE_FAIL notification.
222  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
223            notifications.params()[3].event);
224  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
225            notifications.params()[3].type);
226  EXPECT_EQ(notification_path, notifications.params()[3].path);
227  EXPECT_EQ(
228      l10n_util::GetStringFUTF16(IDS_DEVICE_UNSUPPORTED_MESSAGE,
229                                 UTF8ToUTF16(device_label)),
230      notifications.params()[3].message);
231}
232
233TEST(FileManagerMountNotificationsTest, MountPartialSuccess) {
234  RecordedDesktopNotifications notifications(NULL);
235
236  std::string notification_path("system_path_prefix");
237  std::string device_label("label");
238
239  notifications.RegisterDevice(notification_path);
240
241  notifications.ManageNotificationsOnMountCompleted(
242      notification_path,
243      device_label,
244      false  /* is_parent */,
245      true  /* success */,
246      false  /* is_unsupported */);
247  ASSERT_EQ(1U, notifications.params().size());
248  // Should hide DEVICE notification.
249  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
250            notifications.params()[0].event);
251  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
252  EXPECT_EQ(notification_path, notifications.params()[0].path);
253
254  notifications.ManageNotificationsOnMountCompleted(
255      notification_path,
256      device_label,
257      false  /* is_parent */,
258      false  /* success */,
259      true  /* is_unsupported */);
260  ASSERT_EQ(2U, notifications.params().size());
261  // Should show a DEVICE_FAIL notification.
262  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
263            notifications.params()[1].event);
264  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
265            notifications.params()[1].type);
266  EXPECT_EQ(notification_path, notifications.params()[1].path);
267  EXPECT_EQ(
268      l10n_util::GetStringFUTF16(IDS_MULTIPART_DEVICE_UNSUPPORTED_MESSAGE,
269                                 UTF8ToUTF16(device_label)),
270      notifications.params()[1].message);
271}
272
273TEST(FileManagerMountNotificationsTest, Unknown) {
274  RecordedDesktopNotifications notifications(NULL);
275
276  std::string notification_path("system_path_prefix");
277  std::string device_label("label");
278
279  notifications.RegisterDevice(notification_path);
280
281  notifications.ManageNotificationsOnMountCompleted(
282      notification_path,
283      device_label,
284      false  /* is_parent */,
285      false  /* success */,
286      false  /* is_unsupported */);
287  ASSERT_EQ(2U, notifications.params().size());
288  // Should hide DEVICE notification.
289  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
290            notifications.params()[0].event);
291  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
292  EXPECT_EQ(notification_path, notifications.params()[0].path);
293  // Should show a DEVICE_FAIL notification.
294  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
295            notifications.params()[1].event);
296  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
297            notifications.params()[1].type);
298  EXPECT_EQ(notification_path, notifications.params()[1].path);
299  EXPECT_EQ(
300      l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_MESSAGE,
301                                 UTF8ToUTF16(device_label)),
302      notifications.params()[1].message);
303}
304
305TEST(FileManagerMountNotificationsTest, NonASCIILabel) {
306  RecordedDesktopNotifications notifications(NULL);
307
308  std::string notification_path("system_path_prefix");
309  // "RA (U+30E9) BE (U+30D9) RU (U+30EB)" in Katakana letters.
310  std::string device_label("\xE3\x83\xA9\xE3\x83\x99\xE3\x83\xAB");
311
312  notifications.RegisterDevice(notification_path);
313
314  notifications.ManageNotificationsOnMountCompleted(
315      notification_path,
316      device_label,
317      false  /* is_parent */,
318      false  /* success */,
319      false  /* is_unsupported */);
320  ASSERT_EQ(2U, notifications.params().size());
321  // Should hide DEVICE notification.
322  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
323            notifications.params()[0].event);
324  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
325  EXPECT_EQ(notification_path, notifications.params()[0].path);
326  // Should show a DEVICE_FAIL notification.
327  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
328            notifications.params()[1].event);
329  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
330            notifications.params()[1].type);
331  EXPECT_EQ(notification_path, notifications.params()[1].path);
332  EXPECT_EQ(
333      l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_MESSAGE,
334                                 UTF8ToUTF16(device_label)),
335      notifications.params()[1].message);
336}
337
338TEST(FileManagerMountNotificationsTest, MulitpleFail) {
339  RecordedDesktopNotifications notifications(NULL);
340
341  std::string notification_path("system_path_prefix");
342  std::string device_label("label");
343
344  notifications.RegisterDevice(notification_path);
345
346  notifications.ManageNotificationsOnMountCompleted(
347      notification_path,
348      device_label,
349      true  /* is_parent */,
350      false  /* success */,
351      false  /* is_unsupported */);
352  EXPECT_EQ(2U, notifications.params().size());
353  // Should hide DEVICE notification.
354  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
355            notifications.params()[0].event);
356  EXPECT_EQ(DesktopNotifications::DEVICE, notifications.params()[0].type);
357  EXPECT_EQ(notification_path, notifications.params()[0].path);
358  // Should show a DEVICE_FAIL notification.
359  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
360            notifications.params()[1].event);
361  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
362            notifications.params()[1].type);
363  EXPECT_EQ(notification_path, notifications.params()[1].path);
364  EXPECT_EQ(
365      l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_MESSAGE,
366                                 UTF8ToUTF16(device_label)),
367      notifications.params()[1].message);
368
369  notifications.ManageNotificationsOnMountCompleted(
370      notification_path,
371      device_label,
372      false  /* is_parent */,
373      false  /* success */,
374      false  /* is_unsupported */);
375  EXPECT_EQ(4U, notifications.params().size());
376  // Should hide DEVICE_FAIL notification.
377  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
378            notifications.params()[2].event);
379  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL, notifications.params()[2].type);
380  EXPECT_EQ(notification_path, notifications.params()[2].path);
381  // Should show a DEVICE_FAIL notification.
382  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
383            notifications.params()[3].event);
384  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
385            notifications.params()[3].type);
386  EXPECT_EQ(notification_path, notifications.params()[3].path);
387  EXPECT_EQ(
388      l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_MESSAGE,
389                                 UTF8ToUTF16(device_label)),
390      notifications.params()[3].message);
391
392  notifications.ManageNotificationsOnMountCompleted(
393      notification_path,
394      device_label,
395      false  /* is_parent */,
396      false  /* success */,
397      false  /* is_unsupported */);
398  EXPECT_EQ(6U, notifications.params().size());
399  // Should hide DEVICE_FAIL notification.
400  EXPECT_EQ(RecordedDesktopNotifications::HIDE,
401            notifications.params()[4].event);
402  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL, notifications.params()[4].type);
403  EXPECT_EQ(notification_path, notifications.params()[4].path);
404  // Should show a DEVICE_FAIL notification.
405  EXPECT_EQ(RecordedDesktopNotifications::SHOW,
406            notifications.params()[5].event);
407  EXPECT_EQ(DesktopNotifications::DEVICE_FAIL,
408            notifications.params()[5].type);
409  EXPECT_EQ(notification_path, notifications.params()[5].path);
410  EXPECT_EQ(
411      l10n_util::GetStringFUTF16(IDS_MULTIPART_DEVICE_UNSUPPORTED_MESSAGE,
412                                 UTF8ToUTF16(device_label)),
413      notifications.params()[5].message);
414
415  notifications.ManageNotificationsOnMountCompleted(
416      notification_path,
417      device_label,
418      false  /* is_parent */,
419      false  /* success */,
420      false  /* is_unsupported */);
421  EXPECT_EQ(6U, notifications.params().size());
422  // Should do nothing this time.
423}
424
425}  // namespace file_manager.
426