Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / sync_file_system / sync_file_system_apitest.cc
blob4177d52f2ee895b919cdee43c5c3f5e91ae6913d
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.
5 #include "base/bind.h"
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/test/base/test_switches.h"
18 #include "storage/browser/fileapi/file_system_url.h"
19 #include "storage/browser/quota/quota_manager.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using ::testing::_;
24 using ::testing::Eq;
25 using ::testing::Ne;
26 using ::testing::Property;
27 using ::testing::Return;
28 using storage::FileSystemURL;
29 using sync_file_system::MockRemoteFileSyncService;
30 using sync_file_system::RemoteFileSyncService;
31 using sync_file_system::SyncFileSystemServiceFactory;
33 namespace {
35 class SyncFileSystemApiTest : public ExtensionApiTest {
36 public:
37 SyncFileSystemApiTest()
38 : mock_remote_service_(NULL),
39 real_minimum_preserved_space_(0),
40 real_default_quota_(0) {}
42 void SetUpInProcessBrowserTestFixture() override {
43 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
45 real_minimum_preserved_space_ =
46 storage::QuotaManager::kMinimumPreserveForSystem;
47 storage::QuotaManager::kMinimumPreserveForSystem = 0;
49 // TODO(calvinlo): Update test code after default quota is made const
50 // (http://crbug.com/155488).
51 real_default_quota_ =
52 storage::QuotaManager::kSyncableStorageDefaultHostQuota;
53 storage::QuotaManager::kSyncableStorageDefaultHostQuota = 123456;
56 void TearDownInProcessBrowserTestFixture() override {
57 storage::QuotaManager::kMinimumPreserveForSystem =
58 real_minimum_preserved_space_;
59 storage::QuotaManager::kSyncableStorageDefaultHostQuota =
60 real_default_quota_;
61 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
64 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();
74 ::testing::NiceMock<MockRemoteFileSyncService>* mock_remote_service() {
75 return mock_remote_service_;
78 private:
79 ::testing::NiceMock<MockRemoteFileSyncService>* mock_remote_service_;
80 int64 real_minimum_preserved_space_;
81 int64 real_default_quota_;
84 ACTION_P(NotifyOkStateAndCallback, mock_remote_service) {
85 mock_remote_service->NotifyRemoteServiceStateUpdated(
86 sync_file_system::REMOTE_SERVICE_OK, "Test event description.");
87 base::ThreadTaskRunnerHandle::Get()->PostTask(
88 FROM_HERE, base::Bind(arg1, sync_file_system::SYNC_STATUS_OK));
91 ACTION_P2(UpdateRemoteChangeQueue, origin, mock_remote_service) {
92 *origin = arg0;
93 mock_remote_service->NotifyRemoteChangeQueueUpdated(1);
96 ACTION_P6(ReturnWithFakeFileAddedStatus,
97 origin,
98 mock_remote_service,
99 file_type,
100 sync_file_status,
101 sync_action_taken,
102 sync_direction) {
103 FileSystemURL mock_url = sync_file_system::CreateSyncableFileSystemURL(
104 *origin,
105 base::FilePath(FILE_PATH_LITERAL("foo.txt")));
106 mock_remote_service->NotifyRemoteChangeQueueUpdated(0);
107 base::ThreadTaskRunnerHandle::Get()->PostTask(
108 FROM_HERE, base::Bind(arg0,
109 sync_file_system::SYNC_STATUS_OK,
110 mock_url));
111 mock_remote_service->NotifyFileStatusChanged(
112 mock_url,
113 file_type,
114 sync_file_status,
115 sync_action_taken,
116 sync_direction);
119 } // namespace
121 // Flaky on Win, OS X, and Linux: http://crbug.com/417330.
122 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, DISABLED_GetFileStatus) {
123 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_status"))
124 << message_;
127 // http://crbug.com/417330
128 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, DISABLED_GetFileStatuses) {
129 // Mocking to return IsConflicting() == true only for the path "Conflicting".
130 base::FilePath conflicting = base::FilePath::FromUTF8Unsafe("Conflicting");
131 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_statuses"))
132 << message_;
135 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, GetUsageAndQuota) {
136 ASSERT_TRUE(RunExtensionTest("sync_file_system/get_usage_and_quota"))
137 << message_;
140 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnFileStatusChanged) {
141 // Mock a pending remote change to be synced.
142 GURL origin;
143 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
144 .WillOnce(UpdateRemoteChangeQueue(&origin, mock_remote_service()));
145 EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_))
146 .WillOnce(ReturnWithFakeFileAddedStatus(
147 &origin,
148 mock_remote_service(),
149 sync_file_system::SYNC_FILE_TYPE_FILE,
150 sync_file_system::SYNC_FILE_STATUS_SYNCED,
151 sync_file_system::SYNC_ACTION_ADDED,
152 sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL));
153 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_file_status_changed"))
154 << message_;
157 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnFileStatusChangedDeleted) {
158 // Mock a pending remote change to be synced.
159 GURL origin;
160 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
161 .WillOnce(UpdateRemoteChangeQueue(&origin, mock_remote_service()));
162 EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_))
163 .WillOnce(ReturnWithFakeFileAddedStatus(
164 &origin,
165 mock_remote_service(),
166 sync_file_system::SYNC_FILE_TYPE_FILE,
167 sync_file_system::SYNC_FILE_STATUS_SYNCED,
168 sync_file_system::SYNC_ACTION_DELETED,
169 sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL));
170 ASSERT_TRUE(RunPlatformAppTest(
171 "sync_file_system/on_file_status_changed_deleted"))
172 << message_;
175 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnServiceStatusChanged) {
176 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
177 .WillOnce(NotifyOkStateAndCallback(mock_remote_service()));
178 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_service_status_changed"))
179 << message_;
182 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, RequestFileSystem) {
183 EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _)).Times(1);
184 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/request_file_system"))
185 << message_;
188 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, WriteFileThenGetUsage) {
189 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/write_file_then_get_usage"))
190 << message_;
193 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, ConflictResolutionPolicy) {
194 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/conflict_resolution_policy"))
195 << message_;
198 IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, GetServiceStatus) {
199 mock_remote_service()->SetServiceState(
200 sync_file_system::REMOTE_SERVICE_AUTHENTICATION_REQUIRED);
201 ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_service_status"))
202 << message_;