Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 16 | |
Bertrand SIMONNET | 6c9fbb9 | 2015-12-21 14:56:40 -0800 | [diff] [blame] | 17 | #include <memory> |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 18 | |
| 19 | #include <base/compiler_specific.h> |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 20 | #include <base/files/file_enumerator.h> |
Ben Chan | 51bf92a | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 22 | #include <base/files/scoped_temp_dir.h> |
Bertrand SIMONNET | 6c9fbb9 | 2015-12-21 14:56:40 -0800 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 24 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 25 | #include "persistent_integer.h" |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 26 | |
| 27 | const char kBackingFileName[] = "1.pibakf"; |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 28 | |
| 29 | using chromeos_metrics::PersistentInteger; |
| 30 | |
| 31 | class PersistentIntegerTest : public testing::Test { |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 32 | void SetUp() override { |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 33 | // Set testing mode. |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 34 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 37 | protected: |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 38 | base::ScopedTempDir temp_dir_; |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | TEST_F(PersistentIntegerTest, BasicChecks) { |
Bertrand SIMONNET | 6c9fbb9 | 2015-12-21 14:56:40 -0800 | [diff] [blame] | 42 | std::unique_ptr<PersistentInteger> pi( |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 43 | new PersistentInteger(kBackingFileName, temp_dir_.path())); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 44 | |
| 45 | // Test initialization. |
| 46 | EXPECT_EQ(0, pi->Get()); |
| 47 | EXPECT_EQ(kBackingFileName, pi->Name()); // boring |
| 48 | |
| 49 | // Test set and add. |
| 50 | pi->Set(2); |
| 51 | pi->Add(3); |
| 52 | EXPECT_EQ(5, pi->Get()); |
| 53 | |
| 54 | // Test persistence. |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 55 | pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path())); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 56 | EXPECT_EQ(5, pi->Get()); |
| 57 | |
| 58 | // Test GetAndClear. |
| 59 | EXPECT_EQ(5, pi->GetAndClear()); |
| 60 | EXPECT_EQ(pi->Get(), 0); |
| 61 | |
| 62 | // Another persistence test. |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 63 | pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path())); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 64 | EXPECT_EQ(0, pi->Get()); |
| 65 | } |