#include "base/metrics/persistent_histogram_storage.h"
#include <memory>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
constexpr char kTestHistogramAllocatorName[] = "TestMetrics";
}
class PersistentHistogramStorageTest : public testing::Test {
public:
PersistentHistogramStorageTest(const PersistentHistogramStorageTest&) =
delete;
PersistentHistogramStorageTest& operator=(
const PersistentHistogramStorageTest&) = delete;
protected:
PersistentHistogramStorageTest() = default;
~PersistentHistogramStorageTest() override = default;
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
test_storage_dir_ =
temp_dir_path().AppendASCII(kTestHistogramAllocatorName);
}
const FilePath& temp_dir_path() { return temp_dir_.GetPath(); }
const FilePath& test_storage_dir() { return test_storage_dir_; }
private:
ScopedTempDir temp_dir_;
FilePath test_storage_dir_;
};
#if !BUILDFLAG(IS_NACL)
TEST_F(PersistentHistogramStorageTest, HistogramWriteTest) {
auto persistent_histogram_storage =
std::make_unique<PersistentHistogramStorage>(
kTestHistogramAllocatorName,
PersistentHistogramStorage::StorageDirManagement::kCreate);
persistent_histogram_storage->set_storage_base_dir(temp_dir_path());
UMA_HISTOGRAM_BOOLEAN("Some.Test.Metric", true);
persistent_histogram_storage.reset();
EXPECT_TRUE(DirectoryExists(test_storage_dir()));
EXPECT_FALSE(IsDirectoryEmpty(test_storage_dir()));
GlobalHistogramAllocator::ReleaseForTesting();
}
#endif
}