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 */ 16 17 #include <memory> 18 19 #include <base/compiler_specific.h> 20 #include <base/files/file_enumerator.h> 21 #include <base/files/file_util.h> 22 #include <base/files/scoped_temp_dir.h> 23 #include <gtest/gtest.h> 24 25 #include "persistent_integer.h" 26 27 const char kBackingFileName[] = "1.pibakf"; 28 29 using chromeos_metrics::PersistentInteger; 30 31 class PersistentIntegerTest : public testing::Test { 32 void SetUp() override { 33 // Set testing mode. 34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 35 } 36 37 protected: 38 base::ScopedTempDir temp_dir_; 39 }; 40 41 TEST_F(PersistentIntegerTest, BasicChecks) { 42 std::unique_ptr<PersistentInteger> pi( 43 new PersistentInteger(kBackingFileName, temp_dir_.path())); 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. 55 pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path())); 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. 63 pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path())); 64 EXPECT_EQ(0, pi->Get()); 65 } 66