blob: 55d6cbc24dee7e4b4e0f6e30b6ba998122e71bbf [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
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 Semenzato6c320062014-03-14 14:17:52 -070016
17#include <gtest/gtest.h>
18
19#include <base/compiler_specific.h>
Luigi Semenzato6c320062014-03-14 14:17:52 -070020#include <base/files/file_enumerator.h>
Ben Chan51bf92a2014-09-05 08:21:06 -070021#include <base/files/file_util.h>
Bertrand SIMONNET12531862015-08-31 11:11:57 -070022#include <base/files/scoped_temp_dir.h>
Luigi Semenzato6c320062014-03-14 14:17:52 -070023
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070024#include "persistent_integer.h"
Luigi Semenzato6c320062014-03-14 14:17:52 -070025
26const char kBackingFileName[] = "1.pibakf";
Luigi Semenzato6c320062014-03-14 14:17:52 -070027
28using chromeos_metrics::PersistentInteger;
29
30class PersistentIntegerTest : public testing::Test {
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070031 void SetUp() override {
Luigi Semenzato6c320062014-03-14 14:17:52 -070032 // Set testing mode.
Bertrand SIMONNET12531862015-08-31 11:11:57 -070033 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
Luigi Semenzato6c320062014-03-14 14:17:52 -070034 }
35
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080036 protected:
Bertrand SIMONNET12531862015-08-31 11:11:57 -070037 base::ScopedTempDir temp_dir_;
Luigi Semenzato6c320062014-03-14 14:17:52 -070038};
39
40TEST_F(PersistentIntegerTest, BasicChecks) {
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080041 scoped_ptr<PersistentInteger> pi(
42 new PersistentInteger(kBackingFileName, temp_dir_.path()));
Luigi Semenzato6c320062014-03-14 14:17:52 -070043
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 SIMONNETa7bc1c12015-11-25 13:29:48 -080054 pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path()));
Luigi Semenzato6c320062014-03-14 14:17:52 -070055 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 SIMONNETa7bc1c12015-11-25 13:29:48 -080062 pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path()));
Luigi Semenzato6c320062014-03-14 14:17:52 -070063 EXPECT_EQ(0, pi->Get());
64}