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.
6 #include "base/command_line.h"
7 #include "base/run_loop.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/sync_file_system/file_status_observer.h"
11 #include "chrome/browser/sync_file_system/local_change_processor.h"
12 #include "chrome/browser/sync_file_system/mock_remote_file_sync_service.h"
13 #include "chrome/browser/sync_file_system/sync_file_system_service.h"
14 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h"
15 #include "chrome/browser/sync_file_system/sync_status_code.h"
16 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/test/base/test_switches.h"
19 #include "storage/browser/fileapi/file_system_url.h"
20 #include "storage/browser/quota/quota_manager.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
27 using ::testing::Property
;
28 using ::testing::Return
;
29 using storage::FileSystemURL
;
30 using sync_file_system::MockRemoteFileSyncService
;
31 using sync_file_system::RemoteFileSyncService
;
32 using sync_file_system::SyncFileSystemServiceFactory
;
36 class SyncFileSystemApiTest
: public ExtensionApiTest
{
38 SyncFileSystemApiTest()
39 : mock_remote_service_(NULL
),
40 real_minimum_preserved_space_(0),
41 real_default_quota_(0) {}
43 void SetUpInProcessBrowserTestFixture() override
{
44 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
46 real_minimum_preserved_space_
=
47 storage::QuotaManager::kMinimumPreserveForSystem
;
48 storage::QuotaManager::kMinimumPreserveForSystem
= 0;
50 // TODO(calvinlo): Update test code after default quota is made const
51 // (http://crbug.com/155488).
53 storage::QuotaManager::kSyncableStorageDefaultHostQuota
;
54 storage::QuotaManager::kSyncableStorageDefaultHostQuota
= 123456;
57 void TearDownInProcessBrowserTestFixture() override
{
58 storage::QuotaManager::kMinimumPreserveForSystem
=
59 real_minimum_preserved_space_
;
60 storage::QuotaManager::kSyncableStorageDefaultHostQuota
=
62 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
65 void SetUpOnMainThread() override
{
66 // Must happen after the browser process is created because instantiating
67 // the factory will instantiate ExtensionSystemFactory which depends on
68 // ExtensionsBrowserClient setup in BrowserProcessImpl.
69 mock_remote_service_
= new ::testing::NiceMock
<MockRemoteFileSyncService
>;
70 SyncFileSystemServiceFactory::GetInstance()->set_mock_remote_file_service(
71 scoped_ptr
<RemoteFileSyncService
>(mock_remote_service_
));
72 ExtensionApiTest::SetUpOnMainThread();
75 ::testing::NiceMock
<MockRemoteFileSyncService
>* mock_remote_service() {
76 return mock_remote_service_
;
80 ::testing::NiceMock
<MockRemoteFileSyncService
>* mock_remote_service_
;
81 int64 real_minimum_preserved_space_
;
82 int64 real_default_quota_
;
85 ACTION_P(NotifyOkStateAndCallback
, mock_remote_service
) {
86 mock_remote_service
->NotifyRemoteServiceStateUpdated(
87 sync_file_system::REMOTE_SERVICE_OK
, "Test event description.");
88 base::ThreadTaskRunnerHandle::Get()->PostTask(
89 FROM_HERE
, base::Bind(arg1
, sync_file_system::SYNC_STATUS_OK
));
92 ACTION_P2(UpdateRemoteChangeQueue
, origin
, mock_remote_service
) {
94 mock_remote_service
->NotifyRemoteChangeQueueUpdated(1);
97 ACTION_P6(ReturnWithFakeFileAddedStatus
,
104 FileSystemURL mock_url
= sync_file_system::CreateSyncableFileSystemURL(
106 base::FilePath(FILE_PATH_LITERAL("foo.txt")));
107 mock_remote_service
->NotifyRemoteChangeQueueUpdated(0);
108 base::ThreadTaskRunnerHandle::Get()->PostTask(
109 FROM_HERE
, base::Bind(arg0
,
110 sync_file_system::SYNC_STATUS_OK
,
112 mock_remote_service
->NotifyFileStatusChanged(
122 // Flaky on Win, OS X, and Linux: http://crbug.com/417330.
123 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, DISABLED_GetFileStatus
) {
124 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_status"))
128 // http://crbug.com/417330
129 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, DISABLED_GetFileStatuses
) {
130 // Mocking to return IsConflicting() == true only for the path "Conflicting".
131 base::FilePath conflicting
= base::FilePath::FromUTF8Unsafe("Conflicting");
132 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_statuses"))
136 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, GetUsageAndQuota
) {
137 ASSERT_TRUE(RunExtensionTest("sync_file_system/get_usage_and_quota"))
141 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, OnFileStatusChanged
) {
142 // Mock a pending remote change to be synced.
144 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_
, _
))
145 .WillOnce(UpdateRemoteChangeQueue(&origin
, mock_remote_service()));
146 EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_
))
147 .WillOnce(ReturnWithFakeFileAddedStatus(
149 mock_remote_service(),
150 sync_file_system::SYNC_FILE_TYPE_FILE
,
151 sync_file_system::SYNC_FILE_STATUS_SYNCED
,
152 sync_file_system::SYNC_ACTION_ADDED
,
153 sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL
));
154 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_file_status_changed"))
158 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, OnFileStatusChangedDeleted
) {
159 // Mock a pending remote change to be synced.
161 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_
, _
))
162 .WillOnce(UpdateRemoteChangeQueue(&origin
, mock_remote_service()));
163 EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_
))
164 .WillOnce(ReturnWithFakeFileAddedStatus(
166 mock_remote_service(),
167 sync_file_system::SYNC_FILE_TYPE_FILE
,
168 sync_file_system::SYNC_FILE_STATUS_SYNCED
,
169 sync_file_system::SYNC_ACTION_DELETED
,
170 sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL
));
171 ASSERT_TRUE(RunPlatformAppTest(
172 "sync_file_system/on_file_status_changed_deleted"))
176 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, OnServiceStatusChanged
) {
177 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_
, _
))
178 .WillOnce(NotifyOkStateAndCallback(mock_remote_service()));
179 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_service_status_changed"))
183 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, RequestFileSystem
) {
184 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_
, _
)).Times(1);
185 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/request_file_system"))
189 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, WriteFileThenGetUsage
) {
190 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/write_file_then_get_usage"))
194 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, ConflictResolutionPolicy
) {
195 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/conflict_resolution_policy"))
199 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest
, GetServiceStatus
) {
200 mock_remote_service()->SetServiceState(
201 sync_file_system::REMOTE_SERVICE_AUTHENTICATION_REQUIRED
);
202 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_service_status"))