blob: dfe242c2a61f37e466e4341d26cc54fa186d6e49 [file] [log] [blame]
wafflesd2d9a332016-04-09 01:59:571// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
7
8#include "components/prefs/testing_pref_service.h"
9#include "components/update_client/persisted_data.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace update_client {
13
14TEST(PersistedDataTest, Simple) {
15 std::unique_ptr<TestingPrefServiceSimple> pref(
16 new TestingPrefServiceSimple());
17 PersistedData::RegisterPrefs(pref->registry());
18 std::unique_ptr<PersistedData> metadata(new PersistedData(pref.get()));
19 EXPECT_EQ(-2, metadata->GetDateLastRollCall("someappid"));
20 std::vector<std::string> items;
21 items.push_back("someappid");
22 metadata->SetDateLastRollCall(items, 3383);
23 EXPECT_EQ(3383, metadata->GetDateLastRollCall("someappid"));
24 EXPECT_EQ(-2, metadata->GetDateLastRollCall("someotherappid"));
wafflese7dff732016-04-15 23:51:4925 const std::string pf1 = metadata->GetPingFreshness("someappid");
26 EXPECT_FALSE(pf1.empty());
wafflesd2d9a332016-04-09 01:59:5727 metadata->SetDateLastRollCall(items, 3386);
28 EXPECT_EQ(3386, metadata->GetDateLastRollCall("someappid"));
29 EXPECT_EQ(-2, metadata->GetDateLastRollCall("someotherappid"));
wafflese7dff732016-04-15 23:51:4930 const std::string pf2 = metadata->GetPingFreshness("someappid");
31 EXPECT_FALSE(pf2.empty());
32 // The following has a 1 / 2^128 chance of being flaky.
33 EXPECT_NE(pf1, pf2);
wafflesd2d9a332016-04-09 01:59:5734}
35
36TEST(PersistedDataTest, SharedPref) {
37 std::unique_ptr<TestingPrefServiceSimple> pref(
38 new TestingPrefServiceSimple());
39 PersistedData::RegisterPrefs(pref->registry());
40 std::unique_ptr<PersistedData> metadata(new PersistedData(pref.get()));
41 EXPECT_EQ(-2, metadata->GetDateLastRollCall("someappid"));
42 std::vector<std::string> items;
43 items.push_back("someappid");
44 metadata->SetDateLastRollCall(items, 3383);
45
46 // Now, create a new PersistedData reading from the same path, verify
47 // that it loads the value.
48 metadata.reset(new PersistedData(pref.get()));
49 EXPECT_EQ(3383, metadata->GetDateLastRollCall("someappid"));
50 EXPECT_EQ(-2, metadata->GetDateLastRollCall("someotherappid"));
51}
52
53} // namespace update_client