<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/path_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/test/data/resource.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class PrefServiceTest : public testing::Test {
protected:
  virtual void SetUp() {
    // Name a subdirectory of the temp directory.
    ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
    file_util::AppendToPath(&test_dir_, L"PrefServiceTest");

    // Create a fresh, empty copy of this directory.
    file_util::Delete(test_dir_, true);
    file_util::CreateDirectory(test_dir_);

    ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
    file_util::AppendToPath(&data_dir_, L"pref_service");
    ASSERT_TRUE(file_util::PathExists(data_dir_));
  }
  virtual void TearDown() {
    // Clean up test directory
    ASSERT_TRUE(file_util::Delete(test_dir_, false));
    ASSERT_FALSE(file_util::PathExists(test_dir_));
  }

  // the path to temporary directory used to contain the test operations
  std::wstring test_dir_;
  // the path to the directory where the test data is stored
  std::wstring data_dir_;
};

class TestPrefObserver : public NotificationObserver {
 public:
  TestPrefObserver(const PrefService* prefs, const std::wstring& pref_name,
      const std::wstring& new_pref_value)
      : observer_fired_(false),
        prefs_(prefs),
        pref_name_(pref_name),
        new_pref_value_(new_pref_value) {
  }
  virtual ~TestPrefObserver() {}

  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    EXPECT_EQ(type.value, NotificationType::PREF_CHANGED);
    PrefService* prefs_in = Source<PrefService>(source).ptr();
    EXPECT_EQ(prefs_in, prefs_);
    std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
    EXPECT_EQ(*pref_name_in, pref_name_);
    EXPECT_EQ(new_pref_value_, prefs_in->GetString(L"homepage"));
    observer_fired_ = true;
  }

  bool observer_fired() { return observer_fired_; }

  void Reset(const std::wstring& new_pref_value) {
    observer_fired_ = false;
    new_pref_value_ = new_pref_value;
  }

 private:
  bool observer_fired_;
  const PrefService* prefs_;
  const std::wstring pref_name_;
  std::wstring new_pref_value_;
};

}  // anonymous namespace

// This test is disabled. See issue 8339.
TEST_F(PrefServiceTest, DISABLED_Basic) {
  PrefService prefs;

  // Test that it fails on nonexistent file.
  std::wstring bogus_input_file = data_dir_;
  file_util::AppendToPath(&bogus_input_file, L"read.txt");
  EXPECT_FALSE(prefs.LoadPersistentPrefs(bogus_input_file));

  // Test that the persistent value can be loaded.
  std::wstring input_file = data_dir_;
  file_util::AppendToPath(&input_file, L"read.json");
  ASSERT_TRUE(file_util::PathExists(input_file));
  ASSERT_TRUE(prefs.LoadPersistentPrefs(input_file));

  // Register test prefs.
  const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs";
  const wchar_t kMaxTabs[] = L"tabs.max_tabs";
  prefs.RegisterStringPref(prefs::kHomePage, L"");
  prefs.RegisterBooleanPref(kNewWindowsInTabs, false);
  prefs.RegisterIntegerPref(kMaxTabs, 0);

  std::wstring microsoft(L"http://www.microsoft.com");
  std::wstring cnn(L"http://www.cnn.com");
  std::wstring homepage(L"http://www.example.com");

  EXPECT_EQ(cnn, prefs.GetString(prefs::kHomePage));

  // Now test that the transient value overrides the persistent value,
  // without actually altering the persistent store.
  EXPECT_TRUE(prefs.transient()->SetString(prefs::kHomePage, microsoft));
  EXPECT_TRUE(prefs.transient()->GetString(prefs::kHomePage, &homepage));
  EXPECT_EQ(microsoft, homepage);

  EXPECT_EQ(microsoft, prefs.GetString(prefs::kHomePage));

  // Test reading some other data types from sub-dictionaries, and
  // writing to the persistent store.
  EXPECT_TRUE(prefs.GetBoolean(kNewWindowsInTabs));
  prefs.SetBoolean(kNewWindowsInTabs, false);
  EXPECT_FALSE(prefs.GetBoolean(kNewWindowsInTabs));

  EXPECT_EQ(20, prefs.GetInteger(kMaxTabs));
  prefs.SetInteger(kMaxTabs, 10);
  EXPECT_EQ(10, prefs.GetInteger(kMaxTabs));

  // Serialize and compare to expected output.
  std::wstring output_file = test_dir_;
  file_util::AppendToPath(&output_file, L"write.json");
  prefs.pref_filename_ = output_file;
  ASSERT_TRUE(prefs.SavePersistentPrefs(NULL));
  std::wstring golden_output_file = data_dir_;
  file_util::AppendToPath(&golden_output_file, L"write.golden.json");
  ASSERT_TRUE(file_util::PathExists(golden_output_file));
  ASSERT_TRUE(file_util::ContentsEqual(golden_output_file, output_file));
}

TEST_F(PrefServiceTest, Overlay) {
  const std::string transient =
    "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\","
    "\"dictionary\":{\"value\":\"transient\"},"
    "\"list\":[\"transient\"]}";

  std::wstring persistent_string(L"persistent");
  std::wstring transient_string(L"transient");

  PrefService prefs;

  std::wstring persistent_file = data_dir_;
  file_util::AppendToPath(&persistent_file, L"overlay.json");
  EXPECT_TRUE(prefs.LoadPersistentPrefs(persistent_file));

  Value* transient_value;
  {
    JSONStringValueSerializer serializer(transient);
    transient_value = serializer.Deserialize(NULL);
    ASSERT_TRUE(transient_value);
  }
  prefs.transient()->Set(transient_string, transient_value);

  Value* both_transient_value;
  {
    JSONStringValueSerializer serializer(transient);
    both_transient_value = serializer.Deserialize(NULL);
    ASSERT_TRUE(both_transient_value);<commit_msg>use #if 0 instead of DISABLED_.<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/path_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/test/data/resource.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class PrefServiceTest : public testing::Test {
protected:
  virtual void SetUp() {
    // Name a subdirectory of the temp directory.
    ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
    file_util::AppendToPath(&test_dir_, L"PrefServiceTest");

    // Create a fresh, empty copy of this directory.
    file_util::Delete(test_dir_, true);
    file_util::CreateDirectory(test_dir_);

    ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
    file_util::AppendToPath(&data_dir_, L"pref_service");
    ASSERT_TRUE(file_util::PathExists(data_dir_));
  }
  virtual void TearDown() {
    // Clean up test directory
    ASSERT_TRUE(file_util::Delete(test_dir_, false));
    ASSERT_FALSE(file_util::PathExists(test_dir_));
  }

  // the path to temporary directory used to contain the test operations
  std::wstring test_dir_;
  // the path to the directory where the test data is stored
  std::wstring data_dir_;
};

class TestPrefObserver : public NotificationObserver {
 public:
  TestPrefObserver(const PrefService* prefs, const std::wstring& pref_name,
      const std::wstring& new_pref_value)
      : observer_fired_(false),
        prefs_(prefs),
        pref_name_(pref_name),
        new_pref_value_(new_pref_value) {
  }
  virtual ~TestPrefObserver() {}

  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    EXPECT_EQ(type.value, NotificationType::PREF_CHANGED);
    PrefService* prefs_in = Source<PrefService>(source).ptr();
    EXPECT_EQ(prefs_in, prefs_);
    std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
    EXPECT_EQ(*pref_name_in, pref_name_);
    EXPECT_EQ(new_pref_value_, prefs_in->GetString(L"homepage"));
    observer_fired_ = true;
  }

  bool observer_fired() { return observer_fired_; }

  void Reset(const std::wstring& new_pref_value) {
    observer_fired_ = false;
    new_pref_value_ = new_pref_value;
  }

 private:
  bool observer_fired_;
  const PrefService* prefs_;
  const std::wstring pref_name_;
  std::wstring new_pref_value_;
};

}  // anonymous namespace

// This test is disabled. See issue 8339.
#if 0
TEST_F(PrefServiceTest, Basic) {
  PrefService prefs;

  // Test that it fails on nonexistent file.
  std::wstring bogus_input_file = data_dir_;
  file_util::AppendToPath(&bogus_input_file, L"read.txt");
  EXPECT_FALSE(prefs.LoadPersistentPrefs(bogus_input_file));

  // Test that the persistent value can be loaded.
  std::wstring input_file = data_dir_;
  file_util::AppendToPath(&input_file, L"read.json");
  ASSERT_TRUE(file_util::PathExists(input_file));
  ASSERT_TRUE(prefs.LoadPersistentPrefs(input_file));

  // Register test prefs.
  const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs";
  const wchar_t kMaxTabs[] = L"tabs.max_tabs";
  prefs.RegisterStringPref(prefs::kHomePage, L"");
  prefs.RegisterBooleanPref(kNewWindowsInTabs, false);
  prefs.RegisterIntegerPref(kMaxTabs, 0);

  std::wstring microsoft(L"http://www.microsoft.com");
  std::wstring cnn(L"http://www.cnn.com");
  std::wstring homepage(L"http://www.example.com");

  EXPECT_EQ(cnn, prefs.GetString(prefs::kHomePage));

  // Now test that the transient value overrides the persistent value,
  // without actually altering the persistent store.
  EXPECT_TRUE(prefs.transient()->SetString(prefs::kHomePage, microsoft));
  EXPECT_TRUE(prefs.transient()->GetString(prefs::kHomePage, &homepage));
  EXPECT_EQ(microsoft, homepage);

  EXPECT_EQ(microsoft, prefs.GetString(prefs::kHomePage));

  // Test reading some other data types from sub-dictionaries, and
  // writing to the persistent store.
  EXPECT_TRUE(prefs.GetBoolean(kNewWindowsInTabs));
  prefs.SetBoolean(kNewWindowsInTabs, false);
  EXPECT_FALSE(prefs.GetBoolean(kNewWindowsInTabs));

  EXPECT_EQ(20, prefs.GetInteger(kMaxTabs));
  prefs.SetInteger(kMaxTabs, 10);
  EXPECT_EQ(10, prefs.GetInteger(kMaxTabs));

  // Serialize and compare to expected output.
  std::wstring output_file = test_dir_;
  file_util::AppendToPath(&output_file, L"write.json");
  prefs.pref_filename_ = output_file;
  ASSERT_TRUE(prefs.SavePersistentPrefs(NULL));
  std::wstring golden_output_file = data_dir_;
  file_util::AppendToPath(&golden_output_file, L"write.golden.json");
  ASSERT_TRUE(file_util::PathExists(golden_output_file));
  ASSERT_TRUE(file_util::ContentsEqual(golden_output_file, output_file));
}
#endif

TEST_F(PrefServiceTest, Overlay) {
  const std::string transient =
    "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\","
    "\"dictionary\":{\"value\":\"transient\"},"
    "\"list\":[\"transient\"]}";

  std::wstring persistent_string(L"persistent");
  std::wstring transient_string(L"transient");

  PrefService prefs;

  std::wstring persistent_file = data_dir_;
  file_util::AppendToPath(&persistent_file, L"overlay.json");
  EXPECT_TRUE(prefs.LoadPersistentPrefs(persistent_file));

  Value* transient_value;
  {
    JSONStringValueSerializer serializer(transient);
    transient_value = serializer.Deserialize(NULL);
    ASSERT_TRUE(transient_value);
  }
  prefs.transient()->Set(transient_string, transient_value);

  Value* both_transient_value;
  {
    JSONStringValueSerializer serializer(transient);
    both_transient_value = serializer.Deserialize(NULL);
    ASSERT_TRUE(both_transient_value);
{'fraction_non_alphanumeric': 0.12435405900983497, 'fraction_numerical': 0.0038339723287214534, 'mean_word_length': 5.197830578512397, 'pattern_counts': {'":': 14, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6255759', 'n_tokens_mistral': 4041, 'n_tokens_neox': 3946, 'n_words': 737}