File: persistent_host_scan_cache_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (169 lines) | stat: -rw-r--r-- 7,152 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/tether/persistent_host_scan_cache_impl.h"

#include <memory>
#include <unordered_map>

#include "chromeos/ash/components/tether/fake_host_scan_cache.h"
#include "chromeos/ash/components/tether/host_scan_test_util.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace tether {

class PersistentHostScanCacheImplTest : public testing::Test {
 public:
  PersistentHostScanCacheImplTest(const PersistentHostScanCacheImplTest&) =
      delete;
  PersistentHostScanCacheImplTest& operator=(
      const PersistentHostScanCacheImplTest&) = delete;

 protected:
  PersistentHostScanCacheImplTest()
      : test_entries_(host_scan_test_util::CreateTestEntries()) {}

  void SetUp() override {
    test_pref_service_ =
        std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
    PersistentHostScanCacheImpl::RegisterPrefs(test_pref_service_->registry());

    host_scan_cache_ =
        std::make_unique<PersistentHostScanCacheImpl>(test_pref_service_.get());
    expected_cache_ = std::make_unique<FakeHostScanCache>();
  }

  void SetHostScanResult(const HostScanCacheEntry& entry) {
    host_scan_cache_->SetHostScanResult(entry);
    expected_cache_->SetHostScanResult(entry);
    EXPECT_TRUE(host_scan_cache_->ExistsInCache(entry.tether_network_guid));
    EXPECT_TRUE(expected_cache_->ExistsInCache(entry.tether_network_guid));
  }

  void RemoveHostScanResult(const std::string& tether_network_guid) {
    host_scan_cache_->RemoveHostScanResult(tether_network_guid);
    expected_cache_->RemoveHostScanResult(tether_network_guid);
    EXPECT_FALSE(host_scan_cache_->ExistsInCache(tether_network_guid));
    EXPECT_FALSE(expected_cache_->ExistsInCache(tether_network_guid));
  }

  void VerifyPersistentCacheMatchesInMemoryCache(size_t expected_size) {
    std::unordered_map<std::string, HostScanCacheEntry> entries =
        host_scan_cache_->GetStoredCacheEntries();
    EXPECT_EQ(expected_size, entries.size());
    EXPECT_EQ(expected_size, expected_cache_->size());
    EXPECT_EQ(expected_cache_->cache(), entries);
    EXPECT_EQ(expected_cache_->GetTetherGuidsInCache(),
              host_scan_cache_->GetTetherGuidsInCache());
  }

  const std::unordered_map<std::string, HostScanCacheEntry> test_entries_;

  std::unique_ptr<sync_preferences::TestingPrefServiceSyncable>
      test_pref_service_;
  std::unique_ptr<FakeHostScanCache> expected_cache_;

  std::unique_ptr<PersistentHostScanCacheImpl> host_scan_cache_;
};

TEST_F(PersistentHostScanCacheImplTest, TestSetAndRemove) {
  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid0));
  VerifyPersistentCacheMatchesInMemoryCache(1u /* expected_size */);

  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid1));
  VerifyPersistentCacheMatchesInMemoryCache(2u /* expected_size */);

  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid2));
  VerifyPersistentCacheMatchesInMemoryCache(3u /* expected_size */);

  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid3));
  VerifyPersistentCacheMatchesInMemoryCache(4u /* expected_size */);

  RemoveHostScanResult(host_scan_test_util::kTetherGuid0);
  VerifyPersistentCacheMatchesInMemoryCache(3u /* expected_size */);

  RemoveHostScanResult(host_scan_test_util::kTetherGuid1);
  VerifyPersistentCacheMatchesInMemoryCache(2u /* expected_size */);

  RemoveHostScanResult(host_scan_test_util::kTetherGuid2);
  VerifyPersistentCacheMatchesInMemoryCache(1u /* expected_size */);

  RemoveHostScanResult(host_scan_test_util::kTetherGuid3);
  VerifyPersistentCacheMatchesInMemoryCache(0u /* expected_size */);
}

TEST_F(PersistentHostScanCacheImplTest, TestUpdate) {
  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid0));
  VerifyPersistentCacheMatchesInMemoryCache(1u /* expected_size */);
  EXPECT_EQ(host_scan_test_util::kTetherSetupRequired0,
            host_scan_cache_->DoesHostRequireSetup(
                host_scan_test_util::kTetherGuid0));

  // Update existing entry, including changing the "setup required" field.
  SetHostScanResult(
      *HostScanCacheEntry::Builder()
           .SetTetherNetworkGuid(host_scan_test_util::kTetherGuid0)
           .SetDeviceName(host_scan_test_util::kTetherDeviceName0)
           .SetCarrier(host_scan_test_util::kTetherCarrier1)
           .SetBatteryPercentage(host_scan_test_util::kTetherBatteryPercentage1)
           .SetSignalStrength(host_scan_test_util::kTetherSignalStrength1)
           .SetSetupRequired(host_scan_test_util::kTetherSetupRequired1)
           .Build());
  VerifyPersistentCacheMatchesInMemoryCache(1u /* expected_size */);
  EXPECT_EQ(host_scan_test_util::kTetherSetupRequired1,
            host_scan_cache_->DoesHostRequireSetup(
                host_scan_test_util::kTetherGuid0));
}

TEST_F(PersistentHostScanCacheImplTest, TestStoredPersistently) {
  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid0));
  VerifyPersistentCacheMatchesInMemoryCache(1u /* expected_size */);

  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid1));
  VerifyPersistentCacheMatchesInMemoryCache(2u /* expected_size */);

  // Now, delete the existing PersistentHostScanCacheImpl object. All of its
  // in-memory state will be cleaned up, but it should have stored the scanned
  // data persistently.
  host_scan_cache_.reset();

  // Create a new object.
  host_scan_cache_ =
      std::make_unique<PersistentHostScanCacheImpl>(test_pref_service_.get());

  // The new object should still access the stored scanned data.
  VerifyPersistentCacheMatchesInMemoryCache(2u /* expected_size */);

  // Make some changes - update one existing result, add a new one, and remove
  // an old one.
  SetHostScanResult(
      *HostScanCacheEntry::Builder()
           .SetTetherNetworkGuid(host_scan_test_util::kTetherGuid0)
           .SetDeviceName(host_scan_test_util::kTetherDeviceName0)
           .SetCarrier(host_scan_test_util::kTetherCarrier1)
           .SetBatteryPercentage(host_scan_test_util::kTetherBatteryPercentage1)
           .SetSignalStrength(host_scan_test_util::kTetherSignalStrength1)
           .SetSetupRequired(host_scan_test_util::kTetherSetupRequired1)
           .Build());
  VerifyPersistentCacheMatchesInMemoryCache(2u /* expected_size */);

  SetHostScanResult(test_entries_.at(host_scan_test_util::kTetherGuid2));
  VerifyPersistentCacheMatchesInMemoryCache(3u /* expected_size */);

  RemoveHostScanResult(host_scan_test_util::kTetherGuid1);
  VerifyPersistentCacheMatchesInMemoryCache(2u /* expected_size */);

  // Delete the current PersistentHostScanCacheImpl and create another new one.
  // It should still contain the same data.
  host_scan_cache_ =
      std::make_unique<PersistentHostScanCacheImpl>(test_pref_service_.get());
  VerifyPersistentCacheMatchesInMemoryCache(2u /* expected_size */);
}

}  // namespace tether

}  // namespace ash