persistent_histogram_storage_unittest.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/metrics/persistent_histogram_storage.h"
  5. #include <memory>
  6. #include "base/files/file_path.h"
  7. #include "base/files/file_util.h"
  8. #include "base/files/scoped_temp_dir.h"
  9. #include "base/metrics/histogram_macros.h"
  10. #include "base/metrics/persistent_histogram_allocator.h"
  11. #include "base/time/time.h"
  12. #include "build/build_config.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace base {
  15. namespace {
  16. // Name of the allocator for storing histograms.
  17. constexpr char kTestHistogramAllocatorName[] = "TestMetrics";
  18. } // namespace
  19. class PersistentHistogramStorageTest : public testing::Test {
  20. public:
  21. PersistentHistogramStorageTest(const PersistentHistogramStorageTest&) =
  22. delete;
  23. PersistentHistogramStorageTest& operator=(
  24. const PersistentHistogramStorageTest&) = delete;
  25. protected:
  26. PersistentHistogramStorageTest() = default;
  27. ~PersistentHistogramStorageTest() override = default;
  28. // Creates a unique temporary directory, and sets the test storage directory.
  29. void SetUp() override {
  30. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  31. test_storage_dir_ =
  32. temp_dir_path().AppendASCII(kTestHistogramAllocatorName);
  33. }
  34. // Gets the path to the temporary directory.
  35. const FilePath& temp_dir_path() { return temp_dir_.GetPath(); }
  36. const FilePath& test_storage_dir() { return test_storage_dir_; }
  37. private:
  38. // A temporary directory where all file IO operations take place.
  39. ScopedTempDir temp_dir_;
  40. // The directory into which metrics files are written.
  41. FilePath test_storage_dir_;
  42. };
  43. #if !BUILDFLAG(IS_NACL)
  44. TEST_F(PersistentHistogramStorageTest, HistogramWriteTest) {
  45. auto persistent_histogram_storage =
  46. std::make_unique<PersistentHistogramStorage>(
  47. kTestHistogramAllocatorName,
  48. PersistentHistogramStorage::StorageDirManagement::kCreate);
  49. persistent_histogram_storage->set_storage_base_dir(temp_dir_path());
  50. // Log some random data.
  51. UMA_HISTOGRAM_BOOLEAN("Some.Test.Metric", true);
  52. // Deleting the object causes the data to be written to the disk.
  53. persistent_histogram_storage.reset();
  54. // The storage directory and the histogram file are created during the
  55. // destruction of the PersistentHistogramStorage instance.
  56. EXPECT_TRUE(DirectoryExists(test_storage_dir()));
  57. EXPECT_FALSE(IsDirectoryEmpty(test_storage_dir()));
  58. // Clean up for subsequent tests.
  59. GlobalHistogramAllocator::ReleaseForTesting();
  60. }
  61. #endif // !BUILDFLAG(IS_NACL)
  62. } // namespace base