<commit_before>// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/drive/file_system/move_operation.h"

#include "chrome/browser/chromeos/drive/file_system/copy_operation.h"
#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
#include "chrome/browser/drive/fake_drive_service.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {
namespace file_system {

class MoveOperationTest : public OperationTestBase {
 protected:
  virtual void SetUp() OVERRIDE {
   OperationTestBase::SetUp();
   operation_.reset(new MoveOperation(blocking_task_runner(),
                                      observer(),
                                      metadata()));
   copy_operation_.reset(new CopyOperation(blocking_task_runner(),
                                           observer(),
                                           scheduler(),
                                           metadata(),
                                           cache(),
                                           fake_service(),
                                           temp_dir()));
  }

  scoped_ptr<MoveOperation> operation_;
  scoped_ptr<CopyOperation> copy_operation_;
};

TEST_F(MoveOperationTest, MoveFileInSameDirectory) {
  const base::FilePath src_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
  const base::FilePath dest_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));

  ResourceEntry src_entry, dest_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(dest_path, &dest_entry));

  FileError error = FILE_ERROR_FAILED;
  operation_->Move(src_path,
                   dest_path,
                   false,
                   google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);

  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));

  EXPECT_EQ(1U, observer()->get_changed_paths().size());
  EXPECT_TRUE(observer()->get_changed_paths().count(src_path.DirName()));
}

TEST_F(MoveOperationTest, MoveFileFromRootToSubDirectory) {
  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
  base::FilePath dest_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));

  ResourceEntry src_entry, dest_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(dest_path, &dest_entry));

  FileError error = FILE_ERROR_FAILED;
  operation_->Move(src_path,
                   dest_path,
                   false,
                   google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);

  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));

  EXPECT_EQ(2U, observer()->get_changed_paths().size());
  EXPECT_TRUE(observer()->get_changed_paths().count(src_path.DirName()));
  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));
}

TEST_F(MoveOperationTest, MoveFileBetweenSubDirectoriesRenameWithTitle) {
  base::FilePath src_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
  base::FilePath dest_path(FILE_PATH_LITERAL(
      "drive/root/Directory 1/Sub Directory Folder/"
      "SubDirectory File 1 (1).txt"));

  ResourceEntry src_entry, dest_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(dest_path, &dest_entry));

  FileError error = FILE_ERROR_FAILED;
  // Copy the src file into the same directory. This will make inconsistency
  // between title and path of the copied file.
  copy_operation_->Copy(
      src_path,
      src_path,
      false,
      google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);
  base::FilePath copied_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1 (1).txt"));
  ResourceEntry copied_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(copied_path, &copied_entry));
  ASSERT_EQ("SubDirectory File 1.txt", copied_entry.title());

  // Move the copied file.
  operation_->Move(copied_path,
                   dest_path,
                   false,
                   google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);

  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
  EXPECT_EQ("SubDirectory File 1 (1).txt", dest_entry.title());
  EXPECT_EQ(copied_entry.local_id(), dest_entry.local_id());
  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(copied_path, &copied_entry));

  EXPECT_EQ(2U, observer()->get_changed_paths().size());
  EXPECT_TRUE(observer()->get_changed_paths().count(copied_path.DirName()));
  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));
}

TEST_F(MoveOperationTest, MoveNotExistingFile) {
  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
  base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Test.log"));

  FileError error = FILE_ERROR_OK;
  operation_->Move(src_path,
                   dest_path,
                   false,<commit_msg>drive: Check OperationObserver::OnEntryUpdatedByOperation is called by MoveOperation in tests<commit_after>// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/drive/file_system/move_operation.h"

#include "chrome/browser/chromeos/drive/file_system/copy_operation.h"
#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
#include "chrome/browser/drive/fake_drive_service.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {
namespace file_system {

class MoveOperationTest : public OperationTestBase {
 protected:
  virtual void SetUp() OVERRIDE {
   OperationTestBase::SetUp();
   operation_.reset(new MoveOperation(blocking_task_runner(),
                                      observer(),
                                      metadata()));
   copy_operation_.reset(new CopyOperation(blocking_task_runner(),
                                           observer(),
                                           scheduler(),
                                           metadata(),
                                           cache(),
                                           fake_service(),
                                           temp_dir()));
  }

  scoped_ptr<MoveOperation> operation_;
  scoped_ptr<CopyOperation> copy_operation_;
};

TEST_F(MoveOperationTest, MoveFileInSameDirectory) {
  const base::FilePath src_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
  const base::FilePath dest_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));

  ResourceEntry src_entry, dest_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(dest_path, &dest_entry));

  FileError error = FILE_ERROR_FAILED;
  operation_->Move(src_path,
                   dest_path,
                   false,
                   google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);

  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));

  EXPECT_EQ(1U, observer()->get_changed_paths().size());
  EXPECT_TRUE(observer()->get_changed_paths().count(src_path.DirName()));

  EXPECT_EQ(1U, observer()->updated_local_ids().size());
  EXPECT_TRUE(observer()->updated_local_ids().count(src_entry.local_id()));
}

TEST_F(MoveOperationTest, MoveFileFromRootToSubDirectory) {
  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
  base::FilePath dest_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));

  ResourceEntry src_entry, dest_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(dest_path, &dest_entry));

  FileError error = FILE_ERROR_FAILED;
  operation_->Move(src_path,
                   dest_path,
                   false,
                   google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);

  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));

  EXPECT_EQ(2U, observer()->get_changed_paths().size());
  EXPECT_TRUE(observer()->get_changed_paths().count(src_path.DirName()));
  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));

  EXPECT_EQ(1U, observer()->updated_local_ids().size());
  EXPECT_TRUE(observer()->updated_local_ids().count(src_entry.local_id()));
}

TEST_F(MoveOperationTest, MoveFileBetweenSubDirectoriesRenameWithTitle) {
  base::FilePath src_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
  base::FilePath dest_path(FILE_PATH_LITERAL(
      "drive/root/Directory 1/Sub Directory Folder/"
      "SubDirectory File 1 (1).txt"));

  ResourceEntry src_entry, dest_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(dest_path, &dest_entry));

  FileError error = FILE_ERROR_FAILED;
  // Copy the src file into the same directory. This will make inconsistency
  // between title and path of the copied file.
  copy_operation_->Copy(
      src_path,
      src_path,
      false,
      google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);
  base::FilePath copied_path(
      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1 (1).txt"));
  ResourceEntry copied_entry;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(copied_path, &copied_entry));
  ASSERT_EQ("SubDirectory File 1.txt", copied_entry.title());

  // Move the copied file.
  operation_->Move(copied_path,
                   dest_path,
                   false,
                   google_apis::test_util::CreateCopyResultCallback(&error));
  test_util::RunBlockingPoolTask();
  EXPECT_EQ(FILE_ERROR_OK, error);

  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
  EXPECT_EQ("SubDirectory File 1 (1).txt", dest_entry.title());
  EXPECT_EQ(copied_entry.local_id(), dest_entry.local_id());
  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(copied_path, &copied_entry));

  EXPECT_EQ(2U, observer()->get_changed_paths().size());
  EXPECT_TRUE(observer()->get_changed_paths().count(copied_path.DirName()));
  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));

  EXPECT_EQ(1U, observer()->updated_local_ids().size());
  EXPECT_TRUE(observer()->updated_local_ids().count(copied_entry.local_id()));
}

TEST_F(MoveOperationTest, MoveNotExistingFile) {
  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
  base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Test.log"));

  FileError error = FILE_ERROR_OK;
  operation_->Move(src_path,
                   dest_path,
                   false,
{'fraction_non_alphanumeric': 0.11561930422378952, 'fraction_numerical': 0.0038830335208812107, 'mean_word_length': 3.980268350434096, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5107897', 'n_tokens_mistral': 3872, 'n_tokens_neox': 3811, 'n_words': 566}