Home | History | Annotate | Download | only in file_manager
      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/extensions/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/gmock/include/gmock/gmock.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "ui/base/l10n/l10n_util.h"
     15 
     16 using ::testing::_;
     17 using ::testing::InSequence;
     18 using ::testing::StrEq;
     19 
     20 namespace file_manager {
     21 
     22 namespace {
     23 
     24 class MockFileManagerNotificationsOnMount : public DesktopNotifications {
     25  public:
     26   explicit MockFileManagerNotificationsOnMount(Profile* profile)
     27       : DesktopNotifications(profile) {
     28   }
     29 
     30   virtual ~MockFileManagerNotificationsOnMount() {}
     31 
     32   MOCK_METHOD3(ShowNotificationWithMessage,
     33                void(NotificationType, const std::string&, const string16&));
     34   MOCK_METHOD2(HideNotification, void(NotificationType, const std::string&));
     35 };
     36 
     37 MATCHER_P2(String16Equals, id, label, "") {
     38   return arg == l10n_util::GetStringFUTF16(id, UTF8ToUTF16(label));
     39 }
     40 
     41 }  // namespace
     42 
     43 TEST(FileManagerMountNotificationsTest, GoodDevice) {
     44   MockFileManagerNotificationsOnMount* mocked_notifications =
     45       new MockFileManagerNotificationsOnMount(NULL);
     46   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
     47 
     48   std::string notification_path("system_path_prefix");
     49   std::string device_label("label");
     50 
     51   notifications->RegisterDevice(notification_path);
     52 
     53   EXPECT_CALL(*mocked_notifications, HideNotification(
     54               DesktopNotifications::DEVICE, StrEq(notification_path)));
     55 
     56   notifications->ManageNotificationsOnMountCompleted(notification_path,
     57       device_label, true, true, false);
     58 };
     59 
     60 TEST(FileManagerMountNotificationsTest, GoodDeviceWithBadParent) {
     61   MockFileManagerNotificationsOnMount* mocked_notifications =
     62       new MockFileManagerNotificationsOnMount(NULL);
     63   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
     64 
     65   std::string notification_path("system_path_prefix");
     66   std::string device_label("label");
     67 
     68   notifications->RegisterDevice(notification_path);
     69 
     70   EXPECT_CALL(*mocked_notifications, HideNotification(
     71               DesktopNotifications::DEVICE, StrEq(notification_path)));
     72 
     73   {
     74     InSequence s;
     75 
     76     EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
     77         DesktopNotifications::DEVICE_FAIL, StrEq(notification_path), _));
     78     EXPECT_CALL(*mocked_notifications, HideNotification(
     79         DesktopNotifications::DEVICE_FAIL,
     80         StrEq(notification_path)));
     81   }
     82 
     83   notifications->ManageNotificationsOnMountCompleted(notification_path,
     84       device_label, true, false, false);
     85   notifications->ManageNotificationsOnMountCompleted(notification_path,
     86       device_label, false, true, false);
     87   notifications->ManageNotificationsOnMountCompleted(notification_path,
     88       device_label, false, true, false);
     89 }
     90 
     91 TEST(FileManagerMountNotificationsTest, UnsupportedDevice) {
     92   MockFileManagerNotificationsOnMount* mocked_notifications =
     93       new MockFileManagerNotificationsOnMount(NULL);
     94   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
     95 
     96   std::string notification_path("system_path_prefix");
     97   std::string device_label("label");
     98 
     99   notifications->RegisterDevice(notification_path);
    100 
    101   EXPECT_CALL(*mocked_notifications, HideNotification(
    102               DesktopNotifications::DEVICE, StrEq(notification_path)));
    103   EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    104       DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    105       String16Equals(IDS_DEVICE_UNSUPPORTED_MESSAGE, device_label)));
    106 
    107   notifications->ManageNotificationsOnMountCompleted(notification_path,
    108       device_label, false, false, true);
    109 }
    110 
    111 TEST(FileManagerMountNotificationsTest, UnsupportedWithUnknownParent) {
    112   MockFileManagerNotificationsOnMount* mocked_notifications =
    113       new MockFileManagerNotificationsOnMount(NULL);
    114   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
    115 
    116   std::string notification_path("system_path_prefix");
    117   std::string device_label("label");
    118 
    119   notifications->RegisterDevice(notification_path);
    120 
    121   EXPECT_CALL(*mocked_notifications, HideNotification(
    122               DesktopNotifications::DEVICE, StrEq(notification_path)));
    123 
    124   {
    125     InSequence s;
    126 
    127     EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    128         DesktopNotifications::DEVICE_FAIL, StrEq(notification_path), _));
    129     EXPECT_CALL(*mocked_notifications, HideNotification(
    130         DesktopNotifications::DEVICE_FAIL, StrEq(notification_path)));
    131     EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    132         DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    133         String16Equals(IDS_DEVICE_UNSUPPORTED_MESSAGE,
    134         device_label)));
    135   }
    136 
    137   notifications->ManageNotificationsOnMountCompleted(notification_path,
    138       device_label, true, false, false);
    139   notifications->ManageNotificationsOnMountCompleted(notification_path,
    140       device_label, false, false, true);
    141 }
    142 
    143 TEST(FileManagerMountNotificationsTest, MountPartialSuccess) {
    144   MockFileManagerNotificationsOnMount* mocked_notifications =
    145       new MockFileManagerNotificationsOnMount(NULL);
    146   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
    147 
    148   std::string notification_path("system_path_prefix");
    149   std::string device_label("label");
    150 
    151   notifications->RegisterDevice(notification_path);
    152   EXPECT_CALL(*mocked_notifications, HideNotification(
    153               DesktopNotifications::DEVICE, StrEq(notification_path)));
    154   EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    155       DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    156           String16Equals(IDS_MULTIPART_DEVICE_UNSUPPORTED_MESSAGE,
    157           device_label)));
    158 
    159   notifications->ManageNotificationsOnMountCompleted(notification_path,
    160       device_label, false, true, false);
    161   notifications->ManageNotificationsOnMountCompleted(notification_path,
    162       device_label, false, false, true);
    163 }
    164 
    165 TEST(FileManagerMountNotificationsTest, Unknown) {
    166   MockFileManagerNotificationsOnMount* mocked_notifications =
    167       new MockFileManagerNotificationsOnMount(NULL);
    168   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
    169 
    170   std::string notification_path("system_path_prefix");
    171   std::string device_label("label");
    172 
    173   notifications->RegisterDevice(notification_path);
    174   EXPECT_CALL(*mocked_notifications, HideNotification(
    175               DesktopNotifications::DEVICE, StrEq(notification_path)));
    176   EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    177       DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    178       String16Equals(IDS_DEVICE_UNKNOWN_MESSAGE, device_label)));
    179 
    180   notifications->ManageNotificationsOnMountCompleted(notification_path,
    181       device_label, false, false, false);
    182 }
    183 
    184 TEST(FileManagerMountNotificationsTest, NonASCIILabel) {
    185   MockFileManagerNotificationsOnMount* mocked_notifications =
    186       new MockFileManagerNotificationsOnMount(NULL);
    187   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
    188 
    189   std::string notification_path("system_path_prefix");
    190   // "RA (U+30E9) BE (U+30D9) RU (U+30EB)" in Katakana letters.
    191   std::string device_label("\xE3\x83\xA9\xE3\x83\x99\xE3\x83\xAB");
    192 
    193   notifications->RegisterDevice(notification_path);
    194   EXPECT_CALL(*mocked_notifications, HideNotification(
    195               DesktopNotifications::DEVICE, StrEq(notification_path)));
    196   EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    197       DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    198       String16Equals(IDS_DEVICE_UNKNOWN_MESSAGE, device_label)));
    199 
    200   notifications->ManageNotificationsOnMountCompleted(notification_path,
    201       device_label, false, false, false);
    202 }
    203 
    204 TEST(FileManagerMountNotificationsTest, MulitpleFail) {
    205   MockFileManagerNotificationsOnMount* mocked_notifications =
    206       new MockFileManagerNotificationsOnMount(NULL);
    207   scoped_ptr<DesktopNotifications> notifications(mocked_notifications);
    208 
    209   std::string notification_path("system_path_prefix");
    210   std::string device_label("label");
    211 
    212   notifications->RegisterDevice(notification_path);
    213   EXPECT_CALL(*mocked_notifications, HideNotification(
    214               DesktopNotifications::DEVICE, StrEq(notification_path)));
    215   {
    216     InSequence s;
    217     EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    218         DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    219         String16Equals(IDS_DEVICE_UNKNOWN_MESSAGE, device_label)))
    220         .RetiresOnSaturation();
    221     EXPECT_CALL(*mocked_notifications, HideNotification(
    222         DesktopNotifications::DEVICE_FAIL, notification_path));
    223     EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    224         DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    225         String16Equals(IDS_DEVICE_UNKNOWN_MESSAGE, device_label)));
    226     EXPECT_CALL(*mocked_notifications, HideNotification(
    227         DesktopNotifications::DEVICE_FAIL, notification_path));
    228     EXPECT_CALL(*mocked_notifications, ShowNotificationWithMessage(
    229         DesktopNotifications::DEVICE_FAIL, StrEq(notification_path),
    230         String16Equals(IDS_MULTIPART_DEVICE_UNSUPPORTED_MESSAGE,
    231                        device_label)));
    232   }
    233 
    234   notifications->ManageNotificationsOnMountCompleted(notification_path,
    235       device_label, true, false, false);
    236   notifications->ManageNotificationsOnMountCompleted(notification_path,
    237       device_label, false, false, false);
    238   notifications->ManageNotificationsOnMountCompleted(notification_path,
    239       device_label, false, false, false);
    240   notifications->ManageNotificationsOnMountCompleted(notification_path,
    241       device_label, false, false, false);
    242 }
    243 
    244 }  // namespace file_manager.
    245