blob: bf76261a403040c7a80f777396621202cd1b05c9 [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
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -080017#include <memory>
Luigi Semenzato6c320062014-03-14 14:17:52 -070018
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>
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -080023#include <gtest/gtest.h>
Luigi Semenzato6c320062014-03-14 14:17:52 -070024
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070025#include "persistent_integer.h"
Luigi Semenzato6c320062014-03-14 14:17:52 -070026
27const char kBackingFileName[] = "1.pibakf";
Luigi Semenzato6c320062014-03-14 14:17:52 -070028
29using chromeos_metrics::PersistentInteger;
30
31class PersistentIntegerTest : public testing::Test {
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070032 void SetUp() override {
Luigi Semenzato6c320062014-03-14 14:17:52 -070033 // Set testing mode.
Bertrand SIMONNET12531862015-08-31 11:11:57 -070034 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
Luigi Semenzato6c320062014-03-14 14:17:52 -070035 }
36
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080037 protected:
Bertrand SIMONNET12531862015-08-31 11:11:57 -070038 base::ScopedTempDir temp_dir_;
Luigi Semenzato6c320062014-03-14 14:17:52 -070039};
40
41TEST_F(PersistentIntegerTest, BasicChecks) {
Bertrand SIMONNET6c9fbb92015-12-21 14:56:40 -080042 std::unique_ptr<PersistentInteger> pi(
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080043 new PersistentInteger(kBackingFileName, temp_dir_.path()));
Luigi Semenzato6c320062014-03-14 14:17:52 -070044
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 SIMONNETa7bc1c12015-11-25 13:29:48 -080055 pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path()));
Luigi Semenzato6c320062014-03-14 14:17:52 -070056 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 SIMONNETa7bc1c12015-11-25 13:29:48 -080063 pi.reset(new PersistentInteger(kBackingFileName, temp_dir_.path()));
Luigi Semenzato6c320062014-03-14 14:17:52 -070064 EXPECT_EQ(0, pi->Get());
65}