<commit_before>// Copyright (c) 2006-2008 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 "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/autocomplete/history_url_provider.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/history/history.h"
#include "chrome/test/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_MACOSX)
#include "base/mac_util.h"
#include "chrome/common/mac_app_names.h"
#endif

using base::Time;
using base::TimeDelta;

struct TestURLInfo {
  std::string url;
  std::wstring title;
  int visit_count;
  int typed_count;
  bool starred;
};

// Contents of the test database.
static TestURLInfo test_db[] = {
  {"http://www.google.com/", L"Google", 3, 3, false},

  // High-quality pages should get a host synthesized as a lower-quality match.
  {"http://slashdot.org/favorite_page.html", L"Favorite page", 200, 100,
   false},

  // Less popular pages should have hosts synthesized as higher-quality
  // matches.
  {"http://kerneltrap.org/not_very_popular.html", L"Less popular", 4, 0,
   false},

  // Unpopular pages should not appear in the results at all.
  {"http://freshmeat.net/unpopular.html", L"Unpopular", 1, 1, false},

  // If a host has a match, we should pick it up during host synthesis.
  {"http://news.google.com/?ned=us&topic=n", L"Google News - U.S.", 2, 2,
   false},
  {"http://news.google.com/", L"Google News", 1, 1, false},

  // Suggested short URLs must be "good enough" and must match user input.
  {"http://foo.com/", L"Dir", 5, 5, false},
  {"http://foo.com/dir/", L"Dir", 2, 2, false},
  {"http://foo.com/dir/another/", L"Dir", 5, 1, false},
  {"http://foo.com/dir/another/again/", L"Dir", 10, 0, false},
  {"http://foo.com/dir/another/again/myfile.html", L"File", 10, 2, false},

  // Starred state is more important than visit count (but less important than
  // typed count) when sorting URLs.  The order in which the URLs were starred
  // shouldn't matter.
  // We throw in a lot of extra URLs here to make sure we're testing the
  // history database's query, not just the autocomplete provider.
  {"http://startest.com/y/a", L"A", 2, 2, true},
  {"http://startest.com/y/b", L"B", 5, 2, false},
  {"http://startest.com/x/c", L"C", 5, 2, true},
  {"http://startest.com/x/d", L"D", 5, 5, false},
  {"http://startest.com/y/e", L"E", 4, 2, false},
  {"http://startest.com/y/f", L"F", 3, 2, false},
  {"http://startest.com/y/g", L"G", 3, 2, false},
  {"http://startest.com/y/h", L"H", 3, 2, false},
  {"http://startest.com/y/i", L"I", 3, 2, false},
  {"http://startest.com/y/j", L"J", 3, 2, false},
  {"http://startest.com/y/k", L"K", 3, 2, false},
  {"http://startest.com/y/l", L"L", 3, 2, false},
  {"http://startest.com/y/m", L"M", 3, 2, false},

  // A file: URL is useful for testing that fixup does the right thing w.r.t.
  // the number of trailing slashes on the user's input.
  {"file:///C:/foo.txt", L"", 2, 2, false},

  // Results with absurdly high typed_counts so that very generic queries like
  // "http" will give consistent results even if more data is added above.
  {"http://bogussite.com/a", L"Bogus A", 10002, 10000, false},
  {"http://bogussite.com/b", L"Bogus B", 10001, 10000, false},
  {"http://bogussite.com/c", L"Bogus C", 10000, 10000, false},

  // Domain name with number.
  {"http://www.17173.com/", L"Domain with number", 3, 3, false},

  // URLs to test exact-matching behavior.
  {"http://go/", L"Intranet URL", 1, 1, false},
  {"http://gooey/", L"Intranet URL 2", 5, 5, false},

};

class HistoryURLProviderTest : public testing::Test,
                               public ACProviderListener {
  // ACProviderListener
  virtual void OnProviderUpdate(bool updated_matches);

 protected:
  // testing::Test
  virtual void SetUp();
  virtual void TearDown();

  // Fills test data into the history system
  void FillData();

  // Runs an autocomplete query on |text| and checks to see that the returned
  // results' destination URLs match those provided.
  void RunTest(const std::wstring text,
               const std::wstring& desired_tld,
               bool prevent_inline_autocomplete,
               const std::string* expected_urls,
               size_t num_results);

  MessageLoopForUI message_loop_;
  ACMatches matches_;
  scoped_ptr<TestingProfile> profile_;
  HistoryService* history_service_;

 private:
  scoped_refptr<HistoryURLProvider> autocomplete_;
};

void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) {
  if (autocomplete_->done())
    MessageLoop::current()->Quit();
}

void HistoryURLProviderTest::SetUp() {
#if defined(OS_MACOSX)
  FilePath path;
  PathService::Get(base::DIR_EXE, &path);
  path = path.AppendASCII(MAC_BROWSER_APP_NAME);
  mac_util::SetOverrideAppBundlePath(path);
#endif

  profile_.reset(new TestingProfile());
  profile_->CreateBookmarkModel(true);
  profile_->CreateHistoryService(true);
  history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);

  autocomplete_ = new HistoryURLProvider(this, profile_.get());

  FillData();
}

void HistoryURLProviderTest::TearDown() {
#if defined(OS_MACOSX)
  mac_util::SetOverrideAppBundle(NULL);
#endif
  autocomplete_ = NULL;
}

void HistoryURLProviderTest::FillData() {
  // All visits are a long time ago (some tests require this since we do some
  // special logic for things visited very recently). Note that this time must
  // be more recent than the "archived history" threshold for the data to go
  // into the main database.
  //
  // TODO(brettw) It would be nice if we could test this behavior, in which
  // case the time would be specifed in the test_db structure.
  Time visit_time = Time::Now() - TimeDelta::FromDays(80);

  for (size_t i = 0; i < arraysize(test_db); ++i) {
    const TestURLInfo& cur = test_db[i];
    const GURL current_url(cur.url);
    history_service_->AddPageWithDetails(current_url, cur.title,
                                         cur.visit_count, cur.typed_count,
                                         visit_time, false);
    if (cur.starred) {
      profile_->GetBookmarkModel()->SetURLStarred(
          current_url, std::wstring(), true);
    }
  }
}

void HistoryURLProviderTest::RunTest(const std::wstring text,
                                     const std::wstring& desired_tld,
                                     bool prevent_inline_autocomplete,<commit_msg>mac_util::SetOverrideAppBundle() are handled in class ChromeTestSuite. They should not be modified here (and especially should not be undone in TearDown since it breaks things for following unit tests).<commit_after>// Copyright (c) 2006-2008 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 "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/autocomplete/history_url_provider.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/history/history.h"
#include "chrome/test/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_MACOSX)
#include "base/mac_util.h"
#include "chrome/common/mac_app_names.h"
#endif

using base::Time;
using base::TimeDelta;

struct TestURLInfo {
  std::string url;
  std::wstring title;
  int visit_count;
  int typed_count;
  bool starred;
};

// Contents of the test database.
static TestURLInfo test_db[] = {
  {"http://www.google.com/", L"Google", 3, 3, false},

  // High-quality pages should get a host synthesized as a lower-quality match.
  {"http://slashdot.org/favorite_page.html", L"Favorite page", 200, 100,
   false},

  // Less popular pages should have hosts synthesized as higher-quality
  // matches.
  {"http://kerneltrap.org/not_very_popular.html", L"Less popular", 4, 0,
   false},

  // Unpopular pages should not appear in the results at all.
  {"http://freshmeat.net/unpopular.html", L"Unpopular", 1, 1, false},

  // If a host has a match, we should pick it up during host synthesis.
  {"http://news.google.com/?ned=us&topic=n", L"Google News - U.S.", 2, 2,
   false},
  {"http://news.google.com/", L"Google News", 1, 1, false},

  // Suggested short URLs must be "good enough" and must match user input.
  {"http://foo.com/", L"Dir", 5, 5, false},
  {"http://foo.com/dir/", L"Dir", 2, 2, false},
  {"http://foo.com/dir/another/", L"Dir", 5, 1, false},
  {"http://foo.com/dir/another/again/", L"Dir", 10, 0, false},
  {"http://foo.com/dir/another/again/myfile.html", L"File", 10, 2, false},

  // Starred state is more important than visit count (but less important than
  // typed count) when sorting URLs.  The order in which the URLs were starred
  // shouldn't matter.
  // We throw in a lot of extra URLs here to make sure we're testing the
  // history database's query, not just the autocomplete provider.
  {"http://startest.com/y/a", L"A", 2, 2, true},
  {"http://startest.com/y/b", L"B", 5, 2, false},
  {"http://startest.com/x/c", L"C", 5, 2, true},
  {"http://startest.com/x/d", L"D", 5, 5, false},
  {"http://startest.com/y/e", L"E", 4, 2, false},
  {"http://startest.com/y/f", L"F", 3, 2, false},
  {"http://startest.com/y/g", L"G", 3, 2, false},
  {"http://startest.com/y/h", L"H", 3, 2, false},
  {"http://startest.com/y/i", L"I", 3, 2, false},
  {"http://startest.com/y/j", L"J", 3, 2, false},
  {"http://startest.com/y/k", L"K", 3, 2, false},
  {"http://startest.com/y/l", L"L", 3, 2, false},
  {"http://startest.com/y/m", L"M", 3, 2, false},

  // A file: URL is useful for testing that fixup does the right thing w.r.t.
  // the number of trailing slashes on the user's input.
  {"file:///C:/foo.txt", L"", 2, 2, false},

  // Results with absurdly high typed_counts so that very generic queries like
  // "http" will give consistent results even if more data is added above.
  {"http://bogussite.com/a", L"Bogus A", 10002, 10000, false},
  {"http://bogussite.com/b", L"Bogus B", 10001, 10000, false},
  {"http://bogussite.com/c", L"Bogus C", 10000, 10000, false},

  // Domain name with number.
  {"http://www.17173.com/", L"Domain with number", 3, 3, false},

  // URLs to test exact-matching behavior.
  {"http://go/", L"Intranet URL", 1, 1, false},
  {"http://gooey/", L"Intranet URL 2", 5, 5, false},

};

class HistoryURLProviderTest : public testing::Test,
                               public ACProviderListener {
  // ACProviderListener
  virtual void OnProviderUpdate(bool updated_matches);

 protected:
  // testing::Test
  virtual void SetUp();
  virtual void TearDown();

  // Fills test data into the history system
  void FillData();

  // Runs an autocomplete query on |text| and checks to see that the returned
  // results' destination URLs match those provided.
  void RunTest(const std::wstring text,
               const std::wstring& desired_tld,
               bool prevent_inline_autocomplete,
               const std::string* expected_urls,
               size_t num_results);

  MessageLoopForUI message_loop_;
  ACMatches matches_;
  scoped_ptr<TestingProfile> profile_;
  HistoryService* history_service_;

 private:
  scoped_refptr<HistoryURLProvider> autocomplete_;
};

void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) {
  if (autocomplete_->done())
    MessageLoop::current()->Quit();
}

void HistoryURLProviderTest::SetUp() {
  profile_.reset(new TestingProfile());
  profile_->CreateBookmarkModel(true);
  profile_->CreateHistoryService(true);
  history_service_ = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);

  autocomplete_ = new HistoryURLProvider(this, profile_.get());

  FillData();
}

void HistoryURLProviderTest::TearDown() {
  autocomplete_ = NULL;
}

void HistoryURLProviderTest::FillData() {
  // All visits are a long time ago (some tests require this since we do some
  // special logic for things visited very recently). Note that this time must
  // be more recent than the "archived history" threshold for the data to go
  // into the main database.
  //
  // TODO(brettw) It would be nice if we could test this behavior, in which
  // case the time would be specifed in the test_db structure.
  Time visit_time = Time::Now() - TimeDelta::FromDays(80);

  for (size_t i = 0; i < arraysize(test_db); ++i) {
    const TestURLInfo& cur = test_db[i];
    const GURL current_url(cur.url);
    history_service_->AddPageWithDetails(current_url, cur.title,
                                         cur.visit_count, cur.typed_count,
                                         visit_time, false);
    if (cur.starred) {
      profile_->GetBookmarkModel()->SetURLStarred(
          current_url, std::wstring(), true);
    }
  }
}

void HistoryURLProviderTest::RunTest(const std::wstring text,
                                     const std::wstring& desired_tld,
                                     bool prevent_inline_autocomplete,
{'fraction_non_alphanumeric': 0.14814532132498856, 'fraction_numerical': 0.01663868111738666, 'mean_word_length': 3.369123041013671, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2650936', 'n_tokens_mistral': 4564, 'n_tokens_neox': 4226, 'n_words': 1262}