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