Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / mounted_disk_monitor_unittest.cc
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/mounted_disk_monitor.h"
6
7 #include "base/basictypes.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "chromeos/dbus/fake_power_manager_client.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace file_manager {
14 namespace {
15
16 // Creates a fake disk with |device_path| and |fs_uuid|.
17 scoped_ptr<chromeos::disks::DiskMountManager::Disk> CreateDisk(
18     const std::string& device_path,
19     const std::string& fs_uuid) {
20   return make_scoped_ptr(
21       new chromeos::disks::DiskMountManager::Disk(
22           device_path, "", "", "", "", "", "", "", "", "", fs_uuid, "",
23           chromeos::DEVICE_TYPE_USB, 0, false, false, false, false, false,
24           false));
25 }
26
27 }  // namespace
28
29 class MountedDiskMonitorTest : public testing::Test {
30  protected:
31   virtual void SetUp() OVERRIDE {
32     power_manager_client_.reset(new chromeos::FakePowerManagerClient);
33     mounted_disk_monitor_.reset(new MountedDiskMonitor(
34         power_manager_client_.get()));
35     mounted_disk_monitor_->set_resuming_time_span_for_testing(
36         base::TimeDelta::FromSeconds(0));
37   }
38
39   base::MessageLoop message_loop_;
40   scoped_ptr<chromeos::FakePowerManagerClient> power_manager_client_;
41   scoped_ptr<MountedDiskMonitor> mounted_disk_monitor_;
42 };
43
44 // Makes sure that just mounting and unmounting repeatedly doesn't affect to
45 // "remounting" state.
46 TEST_F(MountedDiskMonitorTest, WithoutSuspend) {
47   scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk(
48       CreateDisk("removable_device1", "uuid1"));
49
50   const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
51       "removable_device1", "/tmp/removable_device1",
52       chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
53
54   // First, the disk is not remounting.
55   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
56
57   // Simple mounting and unmounting doesn't affect remounting state.
58   mounted_disk_monitor_->OnMountEvent(
59       chromeos::disks::DiskMountManager::MOUNTING,
60       chromeos::MOUNT_ERROR_NONE,
61       kMountPoint,
62       disk.get());
63   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
64
65   mounted_disk_monitor_->OnMountEvent(
66       chromeos::disks::DiskMountManager::UNMOUNTING,
67       chromeos::MOUNT_ERROR_NONE,
68       kMountPoint,
69       disk.get());
70   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
71
72   // Mounting again also should not affect remounting state.
73   mounted_disk_monitor_->OnMountEvent(
74       chromeos::disks::DiskMountManager::MOUNTING,
75       chromeos::MOUNT_ERROR_NONE,
76       kMountPoint,
77       disk.get());
78   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk));
79 }
80
81 // Makes sure that the unmounting after system resuming triggers the
82 // "remounting" state, then after some period, the state is reset.
83 TEST_F(MountedDiskMonitorTest, SuspendAndResume) {
84   scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk1(
85       CreateDisk("removable_device1", "uuid1"));
86   scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk2(
87       CreateDisk("removable_device2", "uuid2"));
88
89   const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint1(
90       "removable_device1", "/tmp/removable_device1",
91       chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
92   const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint2(
93       "removable_device2", "/tmp/removable_device2",
94       chromeos::MOUNT_TYPE_DEVICE, chromeos::disks::MOUNT_CONDITION_NONE);
95
96   // Mount |disk1|.
97   mounted_disk_monitor_->OnMountEvent(
98       chromeos::disks::DiskMountManager::MOUNTING,
99       chromeos::MOUNT_ERROR_NONE,
100       kMountPoint1,
101       disk1.get());
102   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
103
104   // Pseudo system suspend and resume.
105   mounted_disk_monitor_->SuspendImminent();
106   mounted_disk_monitor_->SuspendDone(base::TimeDelta::FromSeconds(0));
107
108   // On system resume, we expect unmount and then mount immediately.
109   // During the phase, we expect the disk is remounting.
110   mounted_disk_monitor_->OnMountEvent(
111       chromeos::disks::DiskMountManager::UNMOUNTING,
112       chromeos::MOUNT_ERROR_NONE,
113       kMountPoint1,
114       disk1.get());
115   EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
116
117   mounted_disk_monitor_->OnMountEvent(
118       chromeos::disks::DiskMountManager::MOUNTING,
119       chromeos::MOUNT_ERROR_NONE,
120       kMountPoint1,
121       disk1.get());
122   EXPECT_TRUE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
123
124   // New disk should not be "remounting."
125   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2));
126   mounted_disk_monitor_->OnMountEvent(
127       chromeos::disks::DiskMountManager::MOUNTING,
128       chromeos::MOUNT_ERROR_NONE,
129       kMountPoint2,
130       disk2.get());
131   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2));
132
133   // After certain period, remounting state should be cleared.
134   base::RunLoop().RunUntilIdle();  // Emulate time passage.
135   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk1));
136   EXPECT_FALSE(mounted_disk_monitor_->DiskIsRemounting(*disk2));
137 }
138
139 }  // namespace file_manager