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 <gtest/gtest.h>
      8 #include <string>
      9 
     10 #include "chrome/browser/browser_process.h"
     11 #include "chrome/browser/notifications/notification.h"
     12 #include "chrome/browser/notifications/notification_ui_manager.h"
     13 #include "chrome/browser/ui/browser.h"
     14 #include "chrome/test/base/in_process_browser_test.h"
     15 #include "chrome/test/base/ui_test_utils.h"
     16 #include "grit/generated_resources.h"
     17 #include "ui/base/l10n/l10n_util.h"
     18 
     19 namespace file_manager {
     20 
     21 // Add "FileManager" prefix to the class to avoid name conflicts.
     22 class FileManagerDesktopNotificationsBrowserTest : public InProcessBrowserTest {
     23  public:
     24   FileManagerDesktopNotificationsBrowserTest() {}
     25 
     26   virtual void CleanUpOnMainThread() OVERRIDE {
     27     notifications_.reset();
     28   }
     29 
     30  protected:
     31   // This must be initialized late in test startup.
     32   void InitNotifications() {
     33     Profile* profile = browser()->profile();
     34     notifications_.reset(new DesktopNotifications(profile));
     35   }
     36 
     37   bool FindNotification(const std::string& id) {
     38     return notifications_->HasNotificationForTest(id);
     39   }
     40 
     41   scoped_ptr<DesktopNotifications> notifications_;
     42 };
     43 
     44 IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest, TestBasic) {
     45   InitNotifications();
     46   // Showing a notification adds a new notification.
     47   notifications_->ShowNotification(DesktopNotifications::DEVICE, "path");
     48   EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
     49   EXPECT_TRUE(FindNotification("Device_path"));
     50 
     51   // Updating the same notification maintains the same count.
     52   notifications_->ShowNotification(DesktopNotifications::DEVICE, "path");
     53   EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
     54   EXPECT_TRUE(FindNotification("Device_path"));
     55 
     56   // A new notification increases the count.
     57   notifications_->ShowNotification(DesktopNotifications::DEVICE_FAIL,
     58                                    "path");
     59   EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
     60   EXPECT_TRUE(FindNotification("DeviceFail_path"));
     61   EXPECT_TRUE(FindNotification("Device_path"));
     62 
     63   // Hiding a notification removes it from our data.
     64   notifications_->HideNotification(DesktopNotifications::DEVICE_FAIL,
     65                                    "path");
     66   EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
     67   EXPECT_FALSE(FindNotification("DeviceFail_path"));
     68   EXPECT_TRUE(FindNotification("Device_path"));
     69 };
     70 
     71 // Note: Delayed tests use a delay time of 0 so that tasks wille execute
     72 // when RunAllPendingInMessageLoop() is called.
     73 IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest,
     74                        ShowDelayedTest) {
     75   InitNotifications();
     76   // Adding a delayed notification does not create a notification.
     77   notifications_->ShowNotificationDelayed(DesktopNotifications::DEVICE,
     78                                           "path",
     79                                           base::TimeDelta::FromSeconds(0));
     80   EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
     81   EXPECT_FALSE(FindNotification("Device_path"));
     82 
     83   // Running the message loop should create the notification.
     84   content::RunAllPendingInMessageLoop();
     85   EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
     86   EXPECT_TRUE(FindNotification("Device_path"));
     87 
     88   // Showing a notification both immediately and delayed results in one
     89   // additional notification.
     90   notifications_->ShowNotificationDelayed(DesktopNotifications::DEVICE_FAIL,
     91                                           "path",
     92                                           base::TimeDelta::FromSeconds(0));
     93   notifications_->ShowNotification(DesktopNotifications::DEVICE_FAIL,
     94                                    "path");
     95   EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
     96   EXPECT_TRUE(FindNotification("DeviceFail_path"));
     97 
     98   // When the delayed notification is processed, it's an update, so we still
     99   // only have two notifications.
    100   content::RunAllPendingInMessageLoop();
    101   EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
    102   EXPECT_TRUE(FindNotification("DeviceFail_path"));
    103 
    104   // If we schedule a show for later, then hide before it becomes visible,
    105   // the notification should not be added.
    106   notifications_->ShowNotificationDelayed(DesktopNotifications::FORMAT_FAIL,
    107                                           "path",
    108                                           base::TimeDelta::FromSeconds(0));
    109   notifications_->HideNotification(DesktopNotifications::FORMAT_FAIL,
    110                                    "path");
    111   EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
    112   EXPECT_TRUE(FindNotification("Device_path"));
    113   EXPECT_TRUE(FindNotification("DeviceFail_path"));
    114   EXPECT_FALSE(FindNotification("Format_path"));
    115 
    116   // Even after processing messages, no new notification should be added.
    117   content::RunAllPendingInMessageLoop();
    118   EXPECT_EQ(2u, notifications_->GetNotificationCountForTest());
    119   EXPECT_TRUE(FindNotification("Device_path"));
    120   EXPECT_TRUE(FindNotification("DeviceFail_path"));
    121   EXPECT_FALSE(FindNotification("Format_path"));
    122 }
    123 
    124 IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest,
    125                        HideDelayedTest) {
    126   InitNotifications();
    127   // Showing now, and scheduling a hide for later, results in one notification.
    128   notifications_->ShowNotification(DesktopNotifications::DEVICE, "path");
    129   notifications_->HideNotificationDelayed(DesktopNotifications::DEVICE,
    130                                           "path",
    131                                           base::TimeDelta::FromSeconds(0));
    132   EXPECT_EQ(1u, notifications_->GetNotificationCountForTest());
    133   EXPECT_TRUE(FindNotification("Device_path"));
    134 
    135   // Running pending messges should remove the notification.
    136   content::RunAllPendingInMessageLoop();
    137   EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
    138 
    139   // Immediate show then hide results in no notification.
    140   notifications_->ShowNotification(DesktopNotifications::DEVICE_FAIL,
    141                                    "path");
    142   notifications_->HideNotification(DesktopNotifications::DEVICE_FAIL,
    143                                    "path");
    144   content::RunAllPendingInMessageLoop();
    145   EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
    146 
    147   // Delayed hide for a notification that doesn't exist does nothing.
    148   notifications_->HideNotificationDelayed(DesktopNotifications::DEVICE_FAIL,
    149                                           "path",
    150                                           base::TimeDelta::FromSeconds(0));
    151   content::RunAllPendingInMessageLoop();
    152   EXPECT_EQ(0u, notifications_->GetNotificationCountForTest());
    153 }
    154 
    155 // Tests that showing two notifications with the same notification ids and
    156 // different messages results in showing the second notification's message.
    157 // This situation can be encountered while showing notifications for
    158 // MountCompletedEvent.
    159 IN_PROC_BROWSER_TEST_F(FileManagerDesktopNotificationsBrowserTest,
    160                        IdenticalNotificationIds) {
    161   InitNotifications();
    162   notifications_->RegisterDevice("path");
    163 
    164   notifications_->ManageNotificationsOnMountCompleted("path", "", false, false,
    165       false);
    166   content::RunAllPendingInMessageLoop();
    167 
    168   EXPECT_EQ(
    169     l10n_util::GetStringUTF16(IDS_DEVICE_UNKNOWN_DEFAULT_MESSAGE),
    170     notifications_->GetNotificationMessageForTest("DeviceFail_path"));
    171 
    172   notifications_->ManageNotificationsOnMountCompleted("path", "", false, false,
    173       false);
    174   content::RunAllPendingInMessageLoop();
    175 
    176   EXPECT_EQ(
    177     l10n_util::GetStringUTF16(IDS_MULTIPART_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE),
    178     notifications_->GetNotificationMessageForTest("DeviceFail_path"));
    179 
    180   notifications_->UnregisterDevice("path");
    181 }
    182 
    183 }  // namespace file_manager.
    184