sync_file_system_apitest.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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
25using ::testing::_;
26using ::testing::Eq;
27using ::testing::Ne;
28using ::testing::Property;
29using ::testing::Return;
30using fileapi::FileSystemURL;
31using sync_file_system::MockRemoteFileSyncService;
32using sync_file_system::RemoteFileSyncService;
33using sync_file_system::SyncFileSystemServiceFactory;
34
35namespace {
36
37class SyncFileSystemApiTest : public ExtensionApiTest {
38 public:
39  SyncFileSystemApiTest()
40      : mock_remote_service_(NULL), real_default_quota_(0) {}
41
42  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
43    ExtensionApiTest::SetUpInProcessBrowserTestFixture();
44    // TODO(calvinlo): Update test code after default quota is made const
45    // (http://crbug.com/155488).
46    real_default_quota_ = quota::QuotaManager::kSyncableStorageDefaultHostQuota;
47    quota::QuotaManager::kSyncableStorageDefaultHostQuota = 123456;
48  }
49
50  virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
51    quota::QuotaManager::kSyncableStorageDefaultHostQuota = real_default_quota_;
52    ExtensionApiTest::TearDownInProcessBrowserTestFixture();
53  }
54
55  virtual void SetUpOnMainThread() OVERRIDE {
56    // Must happen after the browser process is created because instantiating
57    // the factory will instantiate ExtensionSystemFactory which depends on
58    // ExtensionsBrowserClient setup in BrowserProcessImpl.
59    mock_remote_service_ = new ::testing::NiceMock<MockRemoteFileSyncService>;
60    SyncFileSystemServiceFactory::GetInstance()->set_mock_remote_file_service(
61        scoped_ptr<RemoteFileSyncService>(mock_remote_service_));
62    ExtensionApiTest::SetUpOnMainThread();
63  }
64
65  ::testing::NiceMock<MockRemoteFileSyncService>* mock_remote_service() {
66    return mock_remote_service_;
67  }
68
69 private:
70  ::testing::NiceMock<MockRemoteFileSyncService>* mock_remote_service_;
71  int64 real_default_quota_;
72};
73
74ACTION_P(NotifyOkStateAndCallback, mock_remote_service) {
75  mock_remote_service->NotifyRemoteServiceStateUpdated(
76      sync_file_system::REMOTE_SERVICE_OK, "Test event description.");
77  base::MessageLoopProxy::current()->PostTask(
78      FROM_HERE, base::Bind(arg1, sync_file_system::SYNC_STATUS_OK));
79}
80
81ACTION_P2(UpdateRemoteChangeQueue, origin, mock_remote_service) {
82  *origin = arg0;
83  mock_remote_service->NotifyRemoteChangeQueueUpdated(1);
84}
85
86ACTION_P5(ReturnWithFakeFileAddedStatus,
87          origin,
88          mock_remote_service,
89          sync_direction,
90          sync_file_status,
91          sync_action_taken) {
92  FileSystemURL mock_url = sync_file_system::CreateSyncableFileSystemURL(
93      *origin,
94      base::FilePath(FILE_PATH_LITERAL("foo.txt")));
95  mock_remote_service->NotifyRemoteChangeQueueUpdated(0);
96  base::MessageLoopProxy::current()->PostTask(
97      FROM_HERE, base::Bind(arg0,
98                            sync_file_system::SYNC_STATUS_OK,
99                            mock_url));
100  mock_remote_service->NotifyFileStatusChanged(
101      mock_url, sync_direction, sync_file_status, sync_action_taken);
102}
103
104}  // namespace
105
106// Flaky on WinXP Tests(1): http://crbug.com/354425
107#if defined(OS_WIN) && defined(ARCH_CPU_X86)
108#define MAYBE_GetFileStatus DISABLED_GetFileStatus
109#else
110#define MAYBE_GetFileStatus GetFileStatus
111#endif
112IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, MAYBE_GetFileStatus) {
113  EXPECT_CALL(*mock_remote_service(), IsConflicting(_)).WillOnce(Return(true));
114  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_status"))
115      << message_;
116}
117
118#if defined(OS_WIN) && defined(ARCH_CPU_X86)
119// SyncFileSystemApiTest.GetFileStatuses fails under AddressSanitizer
120// on Precise. See http://crbug.com/230779.
121// Also fails on WinXP Tests(1). See crbug.com/354425 .
122#define MAYBE_GetFileStatuses DISABLED_GetFileStatuses
123#else
124#define MAYBE_GetFileStatuses GetFileStatuses
125#endif
126IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, MAYBE_GetFileStatuses) {
127#if defined(OS_WIN) && defined(USE_ASH)
128  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
129  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
130    return;
131#endif
132
133  // Mocking to return IsConflicting() == true only for the path "Conflicting".
134  base::FilePath conflicting = base::FilePath::FromUTF8Unsafe("Conflicting");
135  EXPECT_CALL(*mock_remote_service(),
136              IsConflicting(Property(&FileSystemURL::path, Eq(conflicting))))
137      .WillOnce(Return(true));
138  EXPECT_CALL(*mock_remote_service(),
139              IsConflicting(Property(&FileSystemURL::path, Ne(conflicting))))
140      .WillRepeatedly(Return(false));
141
142  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_file_statuses"))
143      << message_;
144}
145
146// Test is flaky, it fails only on a certain bot, namely WinXP Tests(1).
147// See crbug.com/354425 .
148#if defined(OS_WIN) && defined(ARCH_CPU_X86)
149#define MAYBE_GetUsageAndQuota DISABLED_GetUsageAndQuota
150#else
151#define MAYBE_GetUsageAndQuota GetUsageAndQuota
152#endif
153IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, MAYBE_GetUsageAndQuota) {
154  ASSERT_TRUE(RunExtensionTest("sync_file_system/get_usage_and_quota"))
155      << message_;
156}
157
158IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnFileStatusChanged) {
159  // Mock a pending remote change to be synced.
160  GURL origin;
161  EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
162      .WillOnce(UpdateRemoteChangeQueue(&origin, mock_remote_service()));
163  EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_))
164      .WillOnce(ReturnWithFakeFileAddedStatus(
165          &origin,
166          mock_remote_service(),
167          sync_file_system::SYNC_FILE_STATUS_SYNCED,
168          sync_file_system::SYNC_ACTION_ADDED,
169          sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL));
170  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_file_status_changed"))
171      << message_;
172}
173
174IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnFileStatusChangedDeleted) {
175  // Mock a pending remote change to be synced.
176  GURL origin;
177  EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
178      .WillOnce(UpdateRemoteChangeQueue(&origin, mock_remote_service()));
179  EXPECT_CALL(*mock_remote_service(), ProcessRemoteChange(_))
180      .WillOnce(ReturnWithFakeFileAddedStatus(
181          &origin,
182          mock_remote_service(),
183          sync_file_system::SYNC_FILE_STATUS_SYNCED,
184          sync_file_system::SYNC_ACTION_DELETED,
185          sync_file_system::SYNC_DIRECTION_REMOTE_TO_LOCAL));
186  ASSERT_TRUE(RunPlatformAppTest(
187      "sync_file_system/on_file_status_changed_deleted"))
188      << message_;
189}
190
191IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, OnServiceStatusChanged) {
192  EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _))
193      .WillOnce(NotifyOkStateAndCallback(mock_remote_service()));
194  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/on_service_status_changed"))
195      << message_;
196}
197
198IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, RequestFileSystem) {
199  EXPECT_CALL(*mock_remote_service(), RegisterOrigin(_, _)).Times(1);
200  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/request_file_system"))
201      << message_;
202}
203
204// Test is flaky, it fails only on a certain bot, namely WinXP Tests(1).
205// See crbug.com/354425 .
206#if defined(OS_WIN) && defined(ARCH_CPU_X86)
207#define MAYBE_WriteFileThenGetUsage DISABLED_WriteFileThenGetUsage
208#else
209#define MAYBE_WriteFileThenGetUsage WriteFileThenGetUsage
210#endif
211IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, MAYBE_WriteFileThenGetUsage) {
212  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/write_file_then_get_usage"))
213      << message_;
214}
215
216IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, ConflictResolutionPolicy) {
217  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/conflict_resolution_policy"))
218      << message_;
219}
220
221IN_PROC_BROWSER_TEST_F(SyncFileSystemApiTest, GetServiceStatus) {
222  mock_remote_service()->SetServiceState(
223      sync_file_system::REMOTE_SERVICE_AUTHENTICATION_REQUIRED);
224  ASSERT_TRUE(RunPlatformAppTest("sync_file_system/get_service_status"))
225      << message_;
226}
227