<commit_before>// Copyright (c) 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/search_metadata.h"

#include "base/message_loop.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/drive/drive_file_system.h"
#include "chrome/browser/chromeos/drive/drive_test_util.h"
#include "chrome/browser/google_apis/fake_drive_service.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {

namespace {
const int kDefaultAtMostNumMatches = 10;
}

class SearchMetadataTest : public testing::Test {
 protected:
  SearchMetadataTest()
      : ui_thread_(content::BrowserThread::UI, &message_loop_) {
  }

  virtual void SetUp() OVERRIDE {
    profile_.reset(new TestingProfile);

    scoped_refptr<base::SequencedWorkerPool> pool =
        content::BrowserThread::GetBlockingPool();
    blocking_task_runner_ =
        pool->GetSequencedTaskRunner(pool->GetSequenceToken());

    file_system_.reset(new DriveFileSystem(profile_.get(),
                                           NULL,  // cache
                                           new google_apis::FakeDriveService,
                                           NULL,  // uploader
                                           NULL,  // webapps registry
                                           blocking_task_runner_));
    file_system_->Initialize();
    ASSERT_TRUE(test_util::LoadChangeFeed("gdata/root_feed.json",
                                          file_system_->feed_loader(),
                                          false,
                                          1));
  }

 protected:
  MessageLoopForUI message_loop_;
  content::TestBrowserThread ui_thread_;
  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  scoped_ptr<TestingProfile> profile_;
  scoped_ptr<DriveFileSystem> file_system_;
};

TEST_F(SearchMetadataTest, SearchMetadata_ZeroMatches) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  SearchMetadata(file_system_.get(),
                 "NonExistent",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(0U, result->size());
}

TEST_F(SearchMetadataTest, SearchMetadata_RegularFile) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  SearchMetadata(file_system_.get(),
                 "SubDirectory File 1.txt",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(1U, result->size());
  EXPECT_EQ(
      FilePath::FromUTF8Unsafe("drive/Directory 1/SubDirectory File 1.txt"),
      result->at(0).path);
}

// This test checks if |FindAndHighlight| does case-insensitive search.
// Tricker test cases for |FindAndHighlight| can be found below.
TEST_F(SearchMetadataTest, SearchMetadata_CaseInsensitiveSearch) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  // The query is all in lower case.
  SearchMetadata(file_system_.get(),
                 "subdirectory file 1.txt",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(1U, result->size());
  EXPECT_EQ(
      FilePath::FromUTF8Unsafe("drive/Directory 1/SubDirectory File 1.txt"),
      result->at(0).path);
}

TEST_F(SearchMetadataTest, SearchMetadata_RegularFiles) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  SearchMetadata(file_system_.get(),
                 "SubDir",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(2U, result->size());

  // The results should be sorted by the last accessed time.
  EXPECT_EQ(12970368000000000,
            result->at(0).entry_proto.file_info().last_accessed());
  EXPECT_EQ(12969849600000000,
            result->at(1).entry_proto.file_info().last_accessed());

  // All base names should contain "File".
  EXPECT_EQ(
      FilePath::FromUTF8Unsafe(
          "drive/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt"),
      result->at(0).path);<commit_msg>drive: Fix a build failure due to 64-bit constants<commit_after>// Copyright (c) 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/search_metadata.h"

#include "base/message_loop.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/drive/drive_file_system.h"
#include "chrome/browser/chromeos/drive/drive_test_util.h"
#include "chrome/browser/google_apis/fake_drive_service.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {

namespace {
const int kDefaultAtMostNumMatches = 10;
}

class SearchMetadataTest : public testing::Test {
 protected:
  SearchMetadataTest()
      : ui_thread_(content::BrowserThread::UI, &message_loop_) {
  }

  virtual void SetUp() OVERRIDE {
    profile_.reset(new TestingProfile);

    scoped_refptr<base::SequencedWorkerPool> pool =
        content::BrowserThread::GetBlockingPool();
    blocking_task_runner_ =
        pool->GetSequencedTaskRunner(pool->GetSequenceToken());

    file_system_.reset(new DriveFileSystem(profile_.get(),
                                           NULL,  // cache
                                           new google_apis::FakeDriveService,
                                           NULL,  // uploader
                                           NULL,  // webapps registry
                                           blocking_task_runner_));
    file_system_->Initialize();
    ASSERT_TRUE(test_util::LoadChangeFeed("gdata/root_feed.json",
                                          file_system_->feed_loader(),
                                          false,
                                          1));
  }

 protected:
  MessageLoopForUI message_loop_;
  content::TestBrowserThread ui_thread_;
  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  scoped_ptr<TestingProfile> profile_;
  scoped_ptr<DriveFileSystem> file_system_;
};

TEST_F(SearchMetadataTest, SearchMetadata_ZeroMatches) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  SearchMetadata(file_system_.get(),
                 "NonExistent",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(0U, result->size());
}

TEST_F(SearchMetadataTest, SearchMetadata_RegularFile) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  SearchMetadata(file_system_.get(),
                 "SubDirectory File 1.txt",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(1U, result->size());
  EXPECT_EQ(
      FilePath::FromUTF8Unsafe("drive/Directory 1/SubDirectory File 1.txt"),
      result->at(0).path);
}

// This test checks if |FindAndHighlight| does case-insensitive search.
// Tricker test cases for |FindAndHighlight| can be found below.
TEST_F(SearchMetadataTest, SearchMetadata_CaseInsensitiveSearch) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  // The query is all in lower case.
  SearchMetadata(file_system_.get(),
                 "subdirectory file 1.txt",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(1U, result->size());
  EXPECT_EQ(
      FilePath::FromUTF8Unsafe("drive/Directory 1/SubDirectory File 1.txt"),
      result->at(0).path);
}

TEST_F(SearchMetadataTest, SearchMetadata_RegularFiles) {
  DriveFileError error = DRIVE_FILE_ERROR_FAILED;
  scoped_ptr<MetadataSearchResultVector> result;

  SearchMetadata(file_system_.get(),
                 "SubDir",
                 kDefaultAtMostNumMatches,
                 base::Bind(&test_util::CopyResultsFromSearchMetadataCallback,
                            &error,
                            &result));

  google_apis::test_util::RunBlockingPoolTask();
  EXPECT_EQ(DRIVE_FILE_OK, error);
  ASSERT_EQ(2U, result->size());

  // The results should be sorted by the last accessed time.
  EXPECT_EQ(12970368000000000LL,
            result->at(0).entry_proto.file_info().last_accessed());
  EXPECT_EQ(12969849600000000LL,
            result->at(1).entry_proto.file_info().last_accessed());

  // All base names should contain "File".
  EXPECT_EQ(
      FilePath::FromUTF8Unsafe(
          "drive/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt"),
      result->at(0).path);
{'fraction_non_alphanumeric': 0.10023640661938535, 'fraction_numerical': 0.012293144208037825, 'mean_word_length': 2.7583511016346836, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3136479', 'n_tokens_mistral': 3058, 'n_tokens_neox': 2953, 'n_words': 503}