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 | |
| 17 | #include <gtest/gtest.h> |
| 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> |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 23 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 24 | #include "persistent_integer.h" |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 25 | |
| 26 | const char kBackingFileName[] = "1.pibakf"; |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 27 | |
| 28 | using chromeos_metrics::PersistentInteger; |
| 29 | |
| 30 | class PersistentIntegerTest : public testing::Test { |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 31 | void SetUp() override { |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 32 | // Set testing mode. |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 33 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame^] | 36 | protected: |
Bertrand SIMONNET | 1253186 | 2015-08-31 11:11:57 -0700 | [diff] [blame] | 37 | base::ScopedTempDir temp_dir_; |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | TEST_F(PersistentIntegerTest, BasicChecks) { |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame^] | 41 | scoped_ptr<PersistentInteger> pi( |
| 42 | new PersistentInteger(kBackingFileName, temp_dir_.path())); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 43 | |
| 44 | // Test initialization. |
| 45 | EXPECT_EQ(0, pi->Get()); |
| 46 | EXPECT_EQ(kBackingFileName, pi->Name()); // boring |
| 47 | |
| 48 | // Test set and add. |
| 49 | pi->Set(2); |
| 50 | pi->Add(3); |
| 51 | EXPECT_EQ(5, pi->Get()); |
| 52 | |
| 53 | // Test persistence. |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame^] | 54 | pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path())); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 55 | EXPECT_EQ(5, pi->Get()); |
| 56 | |
| 57 | // Test GetAndClear. |
| 58 | EXPECT_EQ(5, pi->GetAndClear()); |
| 59 | EXPECT_EQ(pi->Get(), 0); |
| 60 | |
| 61 | // Another persistence test. |
Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame^] | 62 | pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path())); |
Luigi Semenzato | 6c32006 | 2014-03-14 14:17:52 -0700 | [diff] [blame] | 63 | EXPECT_EQ(0, pi->Get()); |
| 64 | } |