1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <memory>
#include <vector>
#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "chrome/browser/sync_file_system/file_change.h"
#include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
#include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
#include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
#include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
#include "chrome/browser/sync_file_system/local/local_file_sync_status.h"
#include "chrome/browser/sync_file_system/local/mock_sync_status_observer.h"
#include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
#include "chrome/browser/sync_file_system/mock_local_change_processor.h"
#include "chrome/browser/sync_file_system/sync_file_metadata.h"
#include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
#include "chrome/browser/sync_file_system/sync_status_code.h"
#include "chrome/browser/sync_file_system/syncable_file_system_util.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "storage/browser/file_system/file_system_context.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/leveldb_chrome.h"
using content::BrowserThread;
using storage::FileSystemURL;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::StrictMock;
using ::testing::WithArg;
using ::testing::WithArgs;
namespace sync_file_system {
namespace {
const char kOrigin[] = "http://example.com";
void DidPrepareForProcessRemoteChange(const base::Location& where,
base::OnceClosure oncompleted,
SyncStatusCode expected_status,
const SyncFileMetadata& expected_metadata,
SyncStatusCode status,
const SyncFileMetadata& metadata,
const FileChangeList& changes) {
SCOPED_TRACE(testing::Message() << where.ToString());
ASSERT_EQ(expected_status, status);
ASSERT_EQ(expected_metadata.file_type, metadata.file_type);
ASSERT_EQ(expected_metadata.size, metadata.size);
ASSERT_TRUE(changes.empty());
std::move(oncompleted).Run();
}
void OnSyncCompleted(const base::Location& where,
base::OnceClosure oncompleted,
SyncStatusCode expected_status,
const FileSystemURL& expected_url,
SyncStatusCode status,
const FileSystemURL& url) {
SCOPED_TRACE(testing::Message() << where.ToString());
ASSERT_EQ(expected_status, status);
ASSERT_EQ(expected_url, url);
std::move(oncompleted).Run();
}
void OnGetFileMetadata(const base::Location& where,
base::OnceClosure oncompleted,
SyncStatusCode* status_out,
SyncFileMetadata* metadata_out,
SyncStatusCode status,
const SyncFileMetadata& metadata) {
SCOPED_TRACE(testing::Message() << where.ToString());
*status_out = status;
*metadata_out = metadata;
std::move(oncompleted).Run();
}
struct PostStatusFunctor {
explicit PostStatusFunctor(SyncStatusCode status) : status_(status) {}
void operator()(SyncStatusCallback callback) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), status_));
}
private:
SyncStatusCode status_;
};
struct PostStatusAndRecordChangeFunctor {
PostStatusAndRecordChangeFunctor(SyncStatusCode status,
std::vector<FileChange>* changes)
: status_(status), changes_(changes) {}
void operator()(FileChange change, SyncStatusCallback callback) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), status_));
changes_->push_back(change);
}
private:
SyncStatusCode status_;
raw_ptr<std::vector<FileChange>> changes_;
};
} // namespace
class LocalFileSyncServiceTest
: public testing::Test,
public LocalFileSyncService::Observer {
protected:
LocalFileSyncServiceTest()
: task_environment_(content::BrowserTaskEnvironment::REAL_IO_THREAD),
num_changes_(0) {}
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
in_memory_env_ = leveldb_chrome::NewMemEnv("LocalFileSyncServiceTest");
file_system_ = std::make_unique<CannedSyncableFileSystem>(
GURL(kOrigin), in_memory_env_.get(), content::GetIOThreadTaskRunner({}),
base::ThreadPool::CreateSingleThreadTaskRunner({base::MayBlock()}));
local_service_ = LocalFileSyncService::CreateForTesting(
&profile_, in_memory_env_.get());
file_system_->SetUp();
base::RunLoop run_loop;
SyncStatusCode status = SYNC_STATUS_UNKNOWN;
local_service_->MaybeInitializeFileSystemContext(
GURL(kOrigin), file_system_->file_system_context(),
AssignAndQuitCallback(&run_loop, &status));
run_loop.Run();
local_service_->AddChangeObserver(this);
EXPECT_EQ(base::File::FILE_OK, file_system_->OpenFileSystem());
file_system_->backend()->sync_context()->
set_mock_notify_changes_duration_in_sec(0);
}
void TearDown() override {
local_service_->Shutdown();
file_system_->TearDown();
RevokeSyncableFileSystem();
base::ThreadPoolInstance::Get()->FlushForTesting();
content::RunAllPendingInMessageLoop(BrowserThread::IO);
}
// LocalChangeObserver overrides.
void OnLocalChangeAvailable(int64_t num_changes) override {
num_changes_ = num_changes;
}
void PrepareForProcessRemoteChange(
const FileSystemURL& url,
const base::Location& where,
SyncStatusCode expected_status,
const SyncFileMetadata& expected_metadata) {
base::RunLoop run_loop;
local_service_->PrepareForProcessRemoteChange(
url,
base::BindOnce(&DidPrepareForProcessRemoteChange,
where,
run_loop.QuitClosure(),
expected_status,
expected_metadata));
run_loop.Run();
}
SyncStatusCode ApplyRemoteChange(const FileChange& change,
const base::FilePath& local_path,
const FileSystemURL& url) {
SyncStatusCode sync_status = SYNC_STATUS_UNKNOWN;
{
base::RunLoop run_loop;
local_service_->ApplyRemoteChange(
change, local_path, url,
AssignAndQuitCallback(&run_loop, &sync_status));
run_loop.Run();
}
{
base::RunLoop run_loop;
local_service_->FinalizeRemoteSync(
url,
sync_status == SYNC_STATUS_OK,
run_loop.QuitClosure());
run_loop.Run();
}
return sync_status;
}
int64_t GetNumChangesInTracker() const {
return file_system_->backend()->change_tracker()->num_changes();
}
content::BrowserTaskEnvironment task_environment_;
base::ScopedTempDir temp_dir_;
std::unique_ptr<leveldb::Env> in_memory_env_;
TestingProfile profile_;
std::unique_ptr<CannedSyncableFileSystem> file_system_;
std::unique_ptr<LocalFileSyncService> local_service_;
int64_t num_changes_;
};
// More complete tests for PrepareForProcessRemoteChange and ApplyRemoteChange
// are also in content_unittest:LocalFileSyncContextTest.
TEST_F(LocalFileSyncServiceTest, RemoteSyncStepsSimple) {
const FileSystemURL kFile(file_system_->URL("file"));
const FileSystemURL kDir(file_system_->URL("dir"));
const std::string kTestFileData = "0123456789";
base::FilePath local_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &local_path));
ASSERT_TRUE(base::WriteFile(local_path, kTestFileData));
// Run PrepareForProcessRemoteChange for kFile.
SyncFileMetadata expected_metadata;
expected_metadata.file_type = SYNC_FILE_TYPE_UNKNOWN;
expected_metadata.size = 0;
PrepareForProcessRemoteChange(kFile, FROM_HERE,
SYNC_STATUS_OK,
expected_metadata);
// Run ApplyRemoteChange for kFile.
FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
SYNC_FILE_TYPE_FILE);
EXPECT_EQ(SYNC_STATUS_OK,
ApplyRemoteChange(change, local_path, kFile));
// Verify the file is synced.
EXPECT_EQ(base::File::FILE_OK,
file_system_->VerifyFile(kFile, kTestFileData));
// Run PrepareForProcessRemoteChange for kDir.
PrepareForProcessRemoteChange(kDir, FROM_HERE,
SYNC_STATUS_OK,
expected_metadata);
// Run ApplyRemoteChange for kDir.
change = FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
SYNC_FILE_TYPE_DIRECTORY);
EXPECT_EQ(SYNC_STATUS_OK,
ApplyRemoteChange(change, base::FilePath(), kDir));
// Verify the directory.
EXPECT_EQ(base::File::FILE_OK,
file_system_->DirectoryExists(kDir));
// Run PrepareForProcessRemoteChange and ApplyRemoteChange for
// kDir once again for deletion.
expected_metadata.file_type = SYNC_FILE_TYPE_DIRECTORY;
expected_metadata.size = 0;
PrepareForProcessRemoteChange(kDir, FROM_HERE,
SYNC_STATUS_OK,
expected_metadata);
change = FileChange(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_UNKNOWN);
EXPECT_EQ(SYNC_STATUS_OK, ApplyRemoteChange(change, base::FilePath(), kDir));
// Now the directory must have deleted.
EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
file_system_->DirectoryExists(kDir));
}
TEST_F(LocalFileSyncServiceTest, LocalChangeObserver) {
const FileSystemURL kFile(file_system_->URL("file"));
const FileSystemURL kDir(file_system_->URL("dir"));
const char kTestFileData[] = "0123456789";
const int kTestFileDataSize = static_cast<int>(std::size(kTestFileData) - 1);
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kFile));
EXPECT_EQ(1, num_changes_);
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateDirectory(kDir));
EXPECT_EQ(kTestFileDataSize,
file_system_->WriteString(kFile, kTestFileData));
EXPECT_EQ(2, num_changes_);
}
#if BUILDFLAG(IS_WIN)
// Flaky: http://crbug.com/171487
#define MAYBE_LocalChangeObserverMultipleContexts\
DISABLED_LocalChangeObserverMultipleContexts
#else
#define MAYBE_LocalChangeObserverMultipleContexts\
LocalChangeObserverMultipleContexts
#endif
TEST_F(LocalFileSyncServiceTest, MAYBE_LocalChangeObserverMultipleContexts) {
const char kOrigin2[] = "http://foo";
CannedSyncableFileSystem file_system2(
GURL(kOrigin2), in_memory_env_.get(), content::GetIOThreadTaskRunner({}),
base::ThreadPool::CreateSingleThreadTaskRunner({base::MayBlock()}));
file_system2.SetUp();
base::RunLoop run_loop;
SyncStatusCode status = SYNC_STATUS_UNKNOWN;
local_service_->MaybeInitializeFileSystemContext(
GURL(kOrigin2), file_system2.file_system_context(),
AssignAndQuitCallback(&run_loop, &status));
run_loop.Run();
EXPECT_EQ(base::File::FILE_OK, file_system2.OpenFileSystem());
file_system2.backend()->sync_context()->
set_mock_notify_changes_duration_in_sec(0);
const FileSystemURL kFile1(file_system_->URL("file1"));
const FileSystemURL kFile2(file_system_->URL("file2"));
const FileSystemURL kFile3(file_system2.URL("file3"));
const FileSystemURL kFile4(file_system2.URL("file4"));
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kFile1));
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kFile2));
EXPECT_EQ(base::File::FILE_OK, file_system2.CreateFile(kFile3));
EXPECT_EQ(base::File::FILE_OK, file_system2.CreateFile(kFile4));
EXPECT_EQ(4, num_changes_);
file_system2.TearDown();
}
TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateFile) {
const FileSystemURL kFile(file_system_->URL("foo"));
const char kTestFileData[] = "0123456789";
const int kTestFileDataSize = static_cast<int>(std::size(kTestFileData) - 1);
base::RunLoop run_loop;
// We should get called OnSyncEnabled and OnWriteEnabled on kFile.
// (OnWriteEnabled is called because we release lock before returning
// from ApplyLocalChange)
StrictMock<MockSyncStatusObserver> status_observer;
EXPECT_CALL(status_observer, OnSyncEnabled(kFile)).Times(AtLeast(1));
EXPECT_CALL(status_observer, OnWriteEnabled(kFile)).Times(AtLeast(0));
file_system_->AddSyncStatusObserver(&status_observer);
// Creates and writes into a file.
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kFile));
EXPECT_EQ(kTestFileDataSize,
file_system_->WriteString(kFile, std::string(kTestFileData)));
// Retrieve the expected file info.
base::File::Info info;
base::FilePath platform_path;
EXPECT_EQ(base::File::FILE_OK,
file_system_->GetMetadataAndPlatformPath(
kFile, &info, &platform_path));
ASSERT_FALSE(info.is_directory);
ASSERT_EQ(kTestFileDataSize, info.size);
SyncFileMetadata metadata;
metadata.file_type = SYNC_FILE_TYPE_FILE;
metadata.size = info.size;
metadata.last_modified = info.last_modified;
// The local_change_processor's ApplyLocalChange should be called once
// with ADD_OR_UPDATE change for TYPE_FILE.
StrictMock<MockLocalChangeProcessor> local_change_processor;
const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
SYNC_FILE_TYPE_FILE);
PostStatusFunctor post_ok_status(SYNC_STATUS_OK);
EXPECT_CALL(local_change_processor,
ApplyLocalChange(change, _, metadata, kFile, _))
.WillOnce(WithArg<4>(Invoke(post_ok_status)));
local_service_->SetLocalChangeProcessor(&local_change_processor);
local_service_->ProcessLocalChange(base::BindOnce(&OnSyncCompleted, FROM_HERE,
run_loop.QuitClosure(),
SYNC_STATUS_OK, kFile));
run_loop.Run();
file_system_->RemoveSyncStatusObserver(&status_observer);
EXPECT_EQ(0, GetNumChangesInTracker());
}
TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateAndRemoveFile) {
const FileSystemURL kFile(file_system_->URL("foo"));
base::RunLoop run_loop;
// We should get called OnSyncEnabled and possibly OnWriteEnabled (depends
// on timing) on kFile.
StrictMock<MockSyncStatusObserver> status_observer;
EXPECT_CALL(status_observer, OnSyncEnabled(kFile)).Times(AtLeast(1));
EXPECT_CALL(status_observer, OnWriteEnabled(kFile)).Times(AtLeast(0));
file_system_->AddSyncStatusObserver(&status_observer);
// Creates and then deletes a file.
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kFile));
EXPECT_EQ(base::File::FILE_OK, file_system_->Remove(kFile, false));
// The local_change_processor's ApplyLocalChange should be called once
// with DELETE change for TYPE_FILE.
// The file will NOT exist in the remote side and the processor might
// return SYNC_FILE_ERROR_NOT_FOUND (as mocked).
PostStatusFunctor post_not_found_status(SYNC_FILE_ERROR_NOT_FOUND);
StrictMock<MockLocalChangeProcessor> local_change_processor;
const FileChange change(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_FILE);
EXPECT_CALL(local_change_processor, ApplyLocalChange(change, _, _, kFile, _))
.WillOnce(WithArg<4>(Invoke(post_not_found_status)));
// The sync should succeed anyway.
local_service_->SetLocalChangeProcessor(&local_change_processor);
local_service_->ProcessLocalChange(base::BindOnce(&OnSyncCompleted, FROM_HERE,
run_loop.QuitClosure(),
SYNC_STATUS_OK, kFile));
run_loop.Run();
file_system_->RemoveSyncStatusObserver(&status_observer);
EXPECT_EQ(0, GetNumChangesInTracker());
}
TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateAndRemoveDirectory) {
const FileSystemURL kDir(file_system_->URL("foo"));
base::RunLoop run_loop;
// OnSyncEnabled is expected to be called at least or more than once.
StrictMock<MockSyncStatusObserver> status_observer;
EXPECT_CALL(status_observer, OnSyncEnabled(kDir)).Times(AtLeast(1));
file_system_->AddSyncStatusObserver(&status_observer);
// Creates and then deletes a directory.
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateDirectory(kDir));
EXPECT_EQ(base::File::FILE_OK, file_system_->Remove(kDir, false));
// The local_change_processor's ApplyLocalChange should never be called.
StrictMock<MockLocalChangeProcessor> local_change_processor;
local_service_->SetLocalChangeProcessor(&local_change_processor);
local_service_->ProcessLocalChange(
base::BindOnce(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(),
SYNC_STATUS_NO_CHANGE_TO_SYNC, FileSystemURL()));
run_loop.Run();
file_system_->RemoveSyncStatusObserver(&status_observer);
EXPECT_EQ(0, GetNumChangesInTracker());
}
TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_MultipleChanges) {
const FileSystemURL kPath(file_system_->URL("foo"));
const FileSystemURL kOther(file_system_->URL("bar"));
base::RunLoop run_loop;
// We should get called OnSyncEnabled and OnWriteEnabled on kPath and
// OnSyncEnabled on kOther.
StrictMock<MockSyncStatusObserver> status_observer;
EXPECT_CALL(status_observer, OnSyncEnabled(kPath)).Times(AtLeast(1));
EXPECT_CALL(status_observer, OnSyncEnabled(kOther)).Times(AtLeast(1));
file_system_->AddSyncStatusObserver(&status_observer);
// Creates a file, delete the file and creates a directory with the same
// name.
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kPath));
EXPECT_EQ(base::File::FILE_OK, file_system_->Remove(kPath, false));
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateDirectory(kPath));
// Creates one more file.
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kOther));
// The local_change_processor's ApplyLocalChange will be called
// twice for FILE_TYPE and FILE_DIRECTORY.
StrictMock<MockLocalChangeProcessor> local_change_processor;
std::vector<FileChange> changes;
PostStatusAndRecordChangeFunctor post_ok_and_record_change(SYNC_STATUS_OK,
&changes);
// auto post_ok_and_record_change =
// DoAll(WithArg<4>(Invoke(post_ok_status)), RecordChange(&changes));
EXPECT_CALL(local_change_processor, ApplyLocalChange(_, _, _, kPath, _))
.Times(2)
.WillOnce(WithArgs<0, 4>(Invoke(post_ok_and_record_change)))
.WillOnce(WithArgs<0, 4>(Invoke(post_ok_and_record_change)));
local_service_->SetLocalChangeProcessor(&local_change_processor);
// OnWriteEnabled will be notified on kPath (in multi-threaded this
// could be delayed, so AtLeast(0)).
EXPECT_CALL(status_observer, OnWriteEnabled(kPath)).Times(AtLeast(0));
local_service_->ProcessLocalChange(base::BindOnce(&OnSyncCompleted, FROM_HERE,
run_loop.QuitClosure(),
SYNC_STATUS_OK, kPath));
run_loop.Run();
EXPECT_EQ(2U, changes.size());
EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_FILE),
changes[0]);
EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
SYNC_FILE_TYPE_DIRECTORY),
changes[1]);
file_system_->RemoveSyncStatusObserver(&status_observer);
// We have one more change for kOther.
EXPECT_EQ(1, GetNumChangesInTracker());
}
TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_GetLocalMetadata) {
const FileSystemURL kURL(file_system_->URL("foo"));
const base::Time kTime = base::Time::FromSecondsSinceUnixEpoch(333);
const int kSize = 555;
base::RunLoop run_loop;
// Creates a file.
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kURL));
EXPECT_EQ(base::File::FILE_OK, file_system_->TruncateFile(kURL, kSize));
EXPECT_EQ(base::File::FILE_OK,
file_system_->TouchFile(kURL, base::Time(), kTime));
SyncStatusCode status = SYNC_STATUS_UNKNOWN;
SyncFileMetadata metadata;
local_service_->GetLocalFileMetadata(
kURL, base::BindOnce(&OnGetFileMetadata, FROM_HERE,
run_loop.QuitClosure(), &status, &metadata));
run_loop.Run();
EXPECT_EQ(SYNC_STATUS_OK, status);
EXPECT_EQ(kTime, metadata.last_modified);
EXPECT_EQ(kSize, metadata.size);
}
TEST_F(LocalFileSyncServiceTest, RecordFakeChange) {
const FileSystemURL kURL(file_system_->URL("foo"));
// Create a file and reset the changes (as preparation).
EXPECT_EQ(base::File::FILE_OK, file_system_->CreateFile(kURL));
file_system_->ClearChangeForURLInTracker(kURL);
EXPECT_EQ(0, GetNumChangesInTracker());
storage::FileSystemURLSet urlset;
file_system_->GetChangedURLsInTracker(&urlset);
EXPECT_TRUE(urlset.empty());
const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
SYNC_FILE_TYPE_FILE);
// Call RecordFakeLocalChange to add an ADD_OR_UPDATE change.
{
base::RunLoop run_loop;
SyncStatusCode status = SYNC_STATUS_UNKNOWN;
local_service_->RecordFakeLocalChange(
kURL, change, AssignAndQuitCallback(&run_loop, &status));
run_loop.Run();
EXPECT_EQ(SYNC_STATUS_OK, status);
}
EXPECT_EQ(1, GetNumChangesInTracker());
file_system_->GetChangedURLsInTracker(&urlset);
EXPECT_EQ(1U, urlset.size());
EXPECT_TRUE(urlset.find(kURL) != urlset.end());
// Next local sync should pick up the recorded change.
StrictMock<MockLocalChangeProcessor> local_change_processor;
std::vector<FileChange> changes;
PostStatusAndRecordChangeFunctor post_ok_and_record_change(SYNC_STATUS_OK,
&changes);
EXPECT_CALL(local_change_processor, ApplyLocalChange(_, _, _, kURL, _))
.WillOnce(WithArgs<0, 4>(Invoke(post_ok_and_record_change)));
{
base::RunLoop run_loop;
local_service_->SetLocalChangeProcessor(&local_change_processor);
local_service_->ProcessLocalChange(
base::BindOnce(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(),
SYNC_STATUS_OK, kURL));
run_loop.Run();
}
EXPECT_EQ(1U, changes.size());
EXPECT_EQ(change, changes[0]);
}
// TODO(kinuko): Add tests for multiple file changes and multiple
// FileSystemContexts.
// Unit test for OriginChangeMap ---------------------------------------------
class OriginChangeMapTest : public testing::Test {
protected:
OriginChangeMapTest() = default;
~OriginChangeMapTest() override = default;
bool NextOriginToProcess(GURL* origin) {
return map_.NextOriginToProcess(origin);
}
int64_t GetTotalChangeCount() const { return map_.GetTotalChangeCount(); }
void SetOriginChangeCount(const GURL& origin, int64_t changes) {
map_.SetOriginChangeCount(origin, changes);
}
void SetOriginEnabled(const GURL& origin, bool enabled) {
map_.SetOriginEnabled(origin, enabled);
}
LocalFileSyncService::OriginChangeMap map_;
};
TEST_F(OriginChangeMapTest, Basic) {
const GURL kOrigin1("chrome-extension://foo");
const GURL kOrigin2("chrome-extension://bar");
const GURL kOrigin3("chrome-extension://baz");
ASSERT_EQ(0, GetTotalChangeCount());
SetOriginChangeCount(kOrigin1, 1);
SetOriginChangeCount(kOrigin2, 2);
ASSERT_EQ(1 + 2, GetTotalChangeCount());
SetOriginChangeCount(kOrigin3, 4);
ASSERT_EQ(1 + 2 + 4, GetTotalChangeCount());
const GURL kOrigins[] = { kOrigin1, kOrigin2, kOrigin3 };
std::set<GURL> all_origins(std::begin(kOrigins), std::end(kOrigins));
GURL origin;
while (!all_origins.empty()) {
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_TRUE(base::Contains(all_origins, origin));
all_origins.erase(origin);
}
// Set kOrigin2's change count 0.
SetOriginChangeCount(kOrigin2, 0);
ASSERT_EQ(1 + 4, GetTotalChangeCount());
// kOrigin2 won't return this time.
all_origins.insert(kOrigin1);
all_origins.insert(kOrigin3);
while (!all_origins.empty()) {
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_TRUE(base::Contains(all_origins, origin));
all_origins.erase(origin);
}
// Calling NextOriginToProcess() again will just return
// the same set of origins (as far as we don't change the
// change count).
all_origins.insert(kOrigin1);
all_origins.insert(kOrigin3);
while (!all_origins.empty()) {
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_TRUE(base::Contains(all_origins, origin));
all_origins.erase(origin);
}
// Set kOrigin2's change count 8.
SetOriginChangeCount(kOrigin2, 8);
ASSERT_EQ(1 + 4 + 8, GetTotalChangeCount());
all_origins.insert(std::begin(kOrigins), std::end(kOrigins));
while (!all_origins.empty()) {
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_TRUE(base::Contains(all_origins, origin));
all_origins.erase(origin);
}
}
TEST_F(OriginChangeMapTest, WithDisabled) {
const GURL kOrigin1("chrome-extension://foo");
const GURL kOrigin2("chrome-extension://bar");
const GURL kOrigin3("chrome-extension://baz");
const GURL kOrigins[] = { kOrigin1, kOrigin2, kOrigin3 };
ASSERT_EQ(0, GetTotalChangeCount());
SetOriginChangeCount(kOrigin1, 1);
SetOriginChangeCount(kOrigin2, 2);
SetOriginChangeCount(kOrigin3, 4);
ASSERT_EQ(1 + 2 + 4, GetTotalChangeCount());
std::set<GURL> all_origins(std::begin(kOrigins), std::end(kOrigins));
GURL origin;
while (!all_origins.empty()) {
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_TRUE(base::Contains(all_origins, origin));
all_origins.erase(origin);
}
SetOriginEnabled(kOrigin2, false);
ASSERT_EQ(1 + 4, GetTotalChangeCount());
// kOrigin2 won't return this time.
all_origins.insert(kOrigin1);
all_origins.insert(kOrigin3);
while (!all_origins.empty()) {
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_TRUE(base::Contains(all_origins, origin));
all_origins.erase(origin);
}
// kOrigin1 and kOrigin2 are now disabled.
SetOriginEnabled(kOrigin1, false);
ASSERT_EQ(4, GetTotalChangeCount());
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_EQ(kOrigin3, origin);
// Re-enable kOrigin2.
SetOriginEnabled(kOrigin2, true);
ASSERT_EQ(2 + 4, GetTotalChangeCount());
// kOrigin1 won't return this time.
all_origins.insert(kOrigin2);
all_origins.insert(kOrigin3);
while (!all_origins.empty()) {
ASSERT_TRUE(NextOriginToProcess(&origin));
ASSERT_TRUE(base::Contains(all_origins, origin));
all_origins.erase(origin);
}
}
} // namespace sync_file_system
|