Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS 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 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | #include <base/compiler_specific.h> |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 8 | #include <base/files/file_enumerator.h> |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame^] | 9 | #include <base/files/file_util.h> |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 10 | |
| 11 | #include "metrics/persistent_integer.h" |
| 12 | |
| 13 | const char kBackingFileName[] = "1.pibakf"; |
| 14 | const char kBackingFilePattern[] = "*.pibakf"; |
| 15 | |
| 16 | using chromeos_metrics::PersistentInteger; |
| 17 | |
| 18 | class PersistentIntegerTest : public testing::Test { |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 19 | void SetUp() override { |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 20 | // Set testing mode. |
| 21 | chromeos_metrics::PersistentInteger::SetTestingMode(true); |
| 22 | } |
| 23 | |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 24 | void TearDown() override { |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 25 | // Remove backing files. The convention is that they all end in ".pibakf". |
| 26 | base::FileEnumerator f_enum(base::FilePath("."), |
| 27 | false, |
| 28 | base::FileEnumerator::FILES, |
| 29 | FILE_PATH_LITERAL(kBackingFilePattern)); |
| 30 | for (base::FilePath name = f_enum.Next(); |
| 31 | !name.empty(); |
| 32 | name = f_enum.Next()) { |
| 33 | base::DeleteFile(name, false); |
| 34 | } |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | TEST_F(PersistentIntegerTest, BasicChecks) { |
| 39 | scoped_ptr<PersistentInteger> pi(new PersistentInteger(kBackingFileName)); |
| 40 | |
| 41 | // Test initialization. |
| 42 | EXPECT_EQ(0, pi->Get()); |
| 43 | EXPECT_EQ(kBackingFileName, pi->Name()); // boring |
| 44 | |
| 45 | // Test set and add. |
| 46 | pi->Set(2); |
| 47 | pi->Add(3); |
| 48 | EXPECT_EQ(5, pi->Get()); |
| 49 | |
| 50 | // Test persistence. |
| 51 | pi.reset(new PersistentInteger(kBackingFileName)); |
| 52 | EXPECT_EQ(5, pi->Get()); |
| 53 | |
| 54 | // Test GetAndClear. |
| 55 | EXPECT_EQ(5, pi->GetAndClear()); |
| 56 | EXPECT_EQ(pi->Get(), 0); |
| 57 | |
| 58 | // Another persistence test. |
| 59 | pi.reset(new PersistentInteger(kBackingFileName)); |
| 60 | EXPECT_EQ(0, pi->Get()); |
| 61 | } |
| 62 | |
| 63 | int main(int argc, char** argv) { |
| 64 | testing::InitGoogleTest(&argc, argv); |
| 65 | return RUN_ALL_TESTS(); |
| 66 | } |