1 // Copyright (c) 2012 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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/path_service.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_service.h"
11 #include "chrome/browser/sync_file_system/file_status_observer.h"
12 #include "chrome/browser/sync_file_system/local_change_processor.h"
13 #include "chrome/browser/sync_file_system/mock_remote_file_sync_service.h"
14 #include "chrome/browser/sync_file_system/sync_file_system_service.h"
15 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h"
16 #include "chrome/browser/sync_file_system/sync_status_code.h"
17 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
18 #include "chrome/common/chrome_version_info.h"
19 #include "chrome/test/base/test_switches.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "webkit/browser/fileapi/file_system_url.h"
23 #include "webkit/browser/quota/quota_manager.h"
24
25 using ::testing::_;
26 using ::testing::Eq;
27 using ::testing::Ne;
28 using ::testing::Property;
29 using ::testing::Return;
30 using fileapi::FileSystemURL;
31 using sync_file_system::MockRemoteFileSyncService;
32 using sync_file_system::RemoteFileSyncService;
33 using sync_file_system::SyncFileSystemServiceFactory;
34
35 namespace {
36
37 class SyncFileSystemApiTest : public ExtensionApiTest {
38 public:
SyncFileSystemApiTest()39 SyncFileSystemApiTest()
40 : mock_remote_service_(NULL),
41 real_minimum_preserved_space_(0),
42 real_default_quota_(0) {}
43
SetUpInProcessBrowserTestFixture()44 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
45 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
46
47 real_minimum_preserved_space_ =
48 quota::QuotaManager::kMinimumPreserveForSystem;
49 quota::QuotaManager::kMinimumPreserveForSystem = 0;
50
51 // TODO(calvinlo): Update test code after default quota is made const
52 // (http://crbug.com/155488).
53 real_default_quota_ = quota::QuotaManager::kSyncableStorageDefaultHostQuota;
54 quota::QuotaManager::kSyncableStorageDefaultHostQuota = 123456;
55 }
56
TearDownInProcessBrowserTestFixture()57 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
58 quota::QuotaManager::kMinimumPreserveForSystem =
59 real_minimum_preserved_space_;
60 quota::QuotaManager::kSyncableStorageDefaultHostQuota = real_default_quota_;
61 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
62 }
63
SetUpOnMainThread()64 virtual void SetUpOnMainThread() OVERRIDE {
65 // Must happen after the browser process is created because instantiating
66 // the factory will instantiate ExtensionSystemFactory which depends on
67 // ExtensionsBrowserClient setup in BrowserProcessImpl.
68 mock_remote_service_ = new ::testing::NiceMock<MockRemoteFileSyncService>;
69 SyncFileSystemServiceFactory::GetInstance()->set_mock_remote_file_service(
70 scoped_ptr<RemoteFileSyncService>(mock_remote_service_));
71 ExtensionApiTest::SetUpOnMainThread();
72 }
73
mock_remote_service()74 ::testing::NiceMock<MockRemoteFileSyncService>* mock_remote_service() {
75 return mock_remote_service_;
76 }
77
78 private:
79 ::testing::NiceMock<MockRemoteFileSyncService>* mock_remote_service_;
80 int64 real_minimum_preserved_space_;
81 int64 real_default_quota_;
82 };
83
ACTION_P(NotifyOkStateAndCallback,mock_remote_service)84 ACTION_P(NotifyOkStateAndCallback, mock_remote_service) {
85 mock_remote_service->NotifyRemoteServiceStateUpdated(
86 sync_file_system::REMOTE_SERVICE_OK, "Test event description.");
87 base::MessageLoopProxy::current()->PostTask(
88 FROM_HERE, base::Bind(arg1, sync_file_system::SYNC_STATUS_OK));
89 }
90
ACTION_P2(UpdateRemoteChangeQueue,origin,mock_remote_service)91 ACTION_P2(UpdateRemoteChangeQueue, origin, mock_remote_service) {
92 *origin = arg0;
93 mock_remote_service->NotifyRemoteChangeQueueUpdated(1);
94 }
95
ACTION_P5(ReturnWithFakeFileAddedStatus,origin,mock_remote_service,sync_direction,sync_file_status,sync_action_taken)96 ACTION_P5(ReturnWithFakeFileAddedStatus,
97 origin,
98 mock_remote_service,
99 sync_direction,
100 sync_file_status,
101 sync_action_taken) {
102 FileSystemURL mock_url = sync_file_system::CreateSyncableFileSystemURL(
103 *origin,
104 base::FilePath(FILE_PATH_LITERAL("foo.txt")));
105 mock_remote_service->NotifyRemoteChangeQueueUpdated(0);
106 base::MessageLoopProxy::current()->PostTask(
107 FROM_HERE, base::Bind(arg0,
108 sync_file_system::SYNC_STATUS_OK,
109 mock_url));
110 mock_remote_service->NotifyFileStatusChanged(
111 mock_url, sync_direction, sync_file_status, sync_action_taken);
112 }
113
114 } // namespace
115
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,GetFileStatus)116 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, GetFileStatus) {
117 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_status"))
118 << message_;
119 }
120
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,GetFileStatuses)121 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, GetFileStatuses) {
122 // Mocking to return IsConflicting() == true only for the path "Conflicting".
123 base::FilePath conflicting = base::FilePath::FromUTF8Unsafe("Conflicting");
124 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_statuses"))
125 << message_;
126 }
127
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,GetUsageAndQuota)128 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, GetUsageAndQuota) {
129 ASSERT_TRUE(RunExtensionTest("sync_file_system/get_usage_and_quota"))
130 << message_;
131 }
132
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,OnFileStatusChanged)133 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnFileStatusChanged) {
134 // Mock a pending remote change to be synced.
135 GURL origin;
136 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
137 .WillOnce(UpdateRemoteChangeQueue(&origin, mock_remote_service()));
138 EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_))
139 .WillOnce(ReturnWithFakeFileAddedStatus(
140 &origin,
141 mock_remote_service(),
142 sync_file_system::SYNC_FILE_STATUS_SYNCED,
143 sync_file_system::SYNC_ACTION_ADDED,
144 sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL));
145 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_file_status_changed"))
146 << message_;
147 }
148
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,OnFileStatusChangedDeleted)149 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnFileStatusChangedDeleted) {
150 // Mock a pending remote change to be synced.
151 GURL origin;
152 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
153 .WillOnce(UpdateRemoteChangeQueue(&origin, mock_remote_service()));
154 EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_))
155 .WillOnce(ReturnWithFakeFileAddedStatus(
156 &origin,
157 mock_remote_service(),
158 sync_file_system::SYNC_FILE_STATUS_SYNCED,
159 sync_file_system::SYNC_ACTION_DELETED,
160 sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL));
161 ASSERT_TRUE(RunPlatformAppTest(
162 "sync_file_system/on_file_status_changed_deleted"))
163 << message_;
164 }
165
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,OnServiceStatusChanged)166 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnServiceStatusChanged) {
167 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
168 .WillOnce(NotifyOkStateAndCallback(mock_remote_service()));
169 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_service_status_changed"))
170 << message_;
171 }
172
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,RequestFileSystem)173 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, RequestFileSystem) {
174 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _)).Times(1);
175 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/request_file_system"))
176 << message_;
177 }
178
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,WriteFileThenGetUsage)179 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, WriteFileThenGetUsage) {
180 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/write_file_then_get_usage"))
181 << message_;
182 }
183
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,ConflictResolutionPolicy)184 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, ConflictResolutionPolicy) {
185 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/conflict_resolution_policy"))
186 << message_;
187 }
188
IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest,GetServiceStatus)189 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, GetServiceStatus) {
190 mock_remote_service()->SetServiceState(
191 sync_file_system::REMOTE_SERVICE_AUTHENTICATION_REQUIRED);
192 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_service_status"))
193 << message_;
194 }
195