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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/offline_pages/core/offline_page_client_policy.h"
#include <algorithm>
#include <memory>
#include <set>
#include "base/containers/contains.h"
#include "components/offline_pages/core/client_namespace_constants.h"
#include "components/offline_pages/core/offline_page_feature.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace offline_pages {
namespace {
const char kUndefinedNamespace[] = "undefined";
bool isTemporary(const OfflinePageClientPolicy& policy) {
return policy.lifetime_type == LifetimeType::TEMPORARY;
}
} // namespace
class ClientPolicyControllerTest : public testing::Test {
public:
// testing::Test
void SetUp() override {}
void TearDown() override {}
protected:
void ExpectTemporary(std::string name_space);
void ExpectDownloadSupport(std::string name_space, bool expectation);
void ExpectPersistent(std::string name_space);
void ExpectRestrictedToTabFromClientId(std::string name_space,
bool expectation);
void ExpectRequiresSpecificUserSettings(std::string name_space,
bool expectation);
};
void ClientPolicyControllerTest::ExpectTemporary(std::string name_space) {
EXPECT_TRUE(base::Contains(GetTemporaryPolicyNamespaces(), name_space))
<< "Namespace " << name_space
<< " had incorrect lifetime type when getting temporary namespaces.";
EXPECT_EQ(GetPolicy(name_space).lifetime_type, LifetimeType::TEMPORARY)
<< "Namespace " << name_space
<< " had incorrect lifetime type setting when directly checking"
" if it is temporary.";
EXPECT_FALSE(base::Contains(GetPersistentPolicyNamespaces(), name_space))
<< "Namespace " << name_space
<< " had incorrect lifetime type when getting persistent namespaces.";
}
void ClientPolicyControllerTest::ExpectDownloadSupport(std::string name_space,
bool expectation) {
EXPECT_EQ(expectation, GetPolicy(name_space).is_supported_by_download)
<< "Namespace " << name_space
<< " had incorrect download support when directly checking if supported"
" by download.";
}
void ClientPolicyControllerTest::ExpectPersistent(std::string name_space) {
EXPECT_FALSE(base::Contains(GetTemporaryPolicyNamespaces(), name_space))
<< "Namespace " << name_space
<< " had incorrect lifetime type when getting temporary namespaces.";
EXPECT_EQ(GetPolicy(name_space).lifetime_type, LifetimeType::PERSISTENT)
<< "Namespace " << name_space
<< " had incorrect lifetime type setting when directly checking"
" if it is temporary.";
EXPECT_TRUE(base::Contains(GetPersistentPolicyNamespaces(), name_space))
<< "Namespace " << name_space
<< " had incorrect lifetime type when getting persistent namespaces.";
}
void ClientPolicyControllerTest::ExpectRestrictedToTabFromClientId(
std::string name_space,
bool expectation) {
EXPECT_EQ(expectation,
GetPolicy(name_space).is_restricted_to_tab_from_client_id)
<< "Namespace " << name_space
<< " had incorrect restriction when directly checking if the namespace"
" is restricted to the tab from the client id field";
}
void ClientPolicyControllerTest::ExpectRequiresSpecificUserSettings(
std::string name_space,
bool expectation) {
EXPECT_EQ(expectation, GetPolicy(name_space).requires_specific_user_settings)
<< "Namespace " << name_space
<< " had incorrect download support when directly checking if disabled"
" when prefetch settings are disabled.";
}
TEST_F(ClientPolicyControllerTest, FallbackTest) {
const OfflinePageClientPolicy& policy = GetPolicy(kUndefinedNamespace);
EXPECT_EQ(policy.name_space, kDefaultNamespace);
EXPECT_TRUE(isTemporary(policy));
ExpectTemporary(kDefaultNamespace);
EXPECT_FALSE(
base::Contains(GetTemporaryPolicyNamespaces(), kUndefinedNamespace));
EXPECT_EQ(GetPolicy(kUndefinedNamespace).lifetime_type,
LifetimeType::TEMPORARY);
ExpectDownloadSupport(kUndefinedNamespace, false);
ExpectDownloadSupport(kDefaultNamespace, false);
ExpectRestrictedToTabFromClientId(kUndefinedNamespace, false);
ExpectRestrictedToTabFromClientId(kDefaultNamespace, false);
ExpectRequiresSpecificUserSettings(kUndefinedNamespace, false);
ExpectRequiresSpecificUserSettings(kDefaultNamespace, false);
}
TEST_F(ClientPolicyControllerTest, CheckBookmarkDefined) {
OfflinePageClientPolicy policy = GetPolicy(kBookmarkNamespace);
EXPECT_EQ(policy.name_space, kBookmarkNamespace);
EXPECT_TRUE(isTemporary(policy));
ExpectTemporary(kBookmarkNamespace);
ExpectDownloadSupport(kBookmarkNamespace, false);
ExpectRestrictedToTabFromClientId(kBookmarkNamespace, false);
ExpectRequiresSpecificUserSettings(kBookmarkNamespace, false);
}
TEST_F(ClientPolicyControllerTest, CheckLastNDefined) {
OfflinePageClientPolicy policy = GetPolicy(kLastNNamespace);
EXPECT_EQ(policy.name_space, kLastNNamespace);
EXPECT_TRUE(isTemporary(policy));
ExpectTemporary(kLastNNamespace);
ExpectDownloadSupport(kLastNNamespace, false);
ExpectRestrictedToTabFromClientId(kLastNNamespace, true);
ExpectRequiresSpecificUserSettings(kLastNNamespace, false);
}
TEST_F(ClientPolicyControllerTest, CheckAsyncDefined) {
OfflinePageClientPolicy policy = GetPolicy(kAsyncNamespace);
EXPECT_EQ(policy.name_space, kAsyncNamespace);
EXPECT_FALSE(isTemporary(policy));
ExpectDownloadSupport(kAsyncNamespace, true);
ExpectPersistent(kAsyncNamespace);
ExpectRestrictedToTabFromClientId(kAsyncNamespace, false);
ExpectRequiresSpecificUserSettings(kAsyncNamespace, false);
}
TEST_F(ClientPolicyControllerTest, CheckCCTDefined) {
OfflinePageClientPolicy policy = GetPolicy(kCCTNamespace);
EXPECT_EQ(policy.name_space, kCCTNamespace);
EXPECT_TRUE(isTemporary(policy));
ExpectTemporary(kCCTNamespace);
ExpectDownloadSupport(kCCTNamespace, false);
ExpectRestrictedToTabFromClientId(kCCTNamespace, false);
ExpectRequiresSpecificUserSettings(kCCTNamespace, true);
}
TEST_F(ClientPolicyControllerTest, CheckDownloadDefined) {
OfflinePageClientPolicy policy = GetPolicy(kDownloadNamespace);
EXPECT_EQ(policy.name_space, kDownloadNamespace);
EXPECT_FALSE(isTemporary(policy));
ExpectDownloadSupport(kDownloadNamespace, true);
ExpectPersistent(kDownloadNamespace);
ExpectRestrictedToTabFromClientId(kDownloadNamespace, false);
ExpectRequiresSpecificUserSettings(kDownloadNamespace, false);
}
TEST_F(ClientPolicyControllerTest, CheckNTPSuggestionsDefined) {
OfflinePageClientPolicy policy = GetPolicy(kNTPSuggestionsNamespace);
EXPECT_EQ(policy.name_space, kNTPSuggestionsNamespace);
EXPECT_FALSE(isTemporary(policy));
ExpectDownloadSupport(kNTPSuggestionsNamespace, true);
ExpectPersistent(kNTPSuggestionsNamespace);
ExpectRestrictedToTabFromClientId(kNTPSuggestionsNamespace, false);
ExpectRequiresSpecificUserSettings(kNTPSuggestionsNamespace, false);
}
TEST_F(ClientPolicyControllerTest, CheckLivePageSharingDefined) {
OfflinePageClientPolicy policy = GetPolicy(kLivePageSharingNamespace);
EXPECT_EQ(policy.name_space, kLivePageSharingNamespace);
EXPECT_TRUE(isTemporary(policy));
ExpectTemporary(kLivePageSharingNamespace);
ExpectDownloadSupport(kLivePageSharingNamespace, false);
ExpectRestrictedToTabFromClientId(kLivePageSharingNamespace, true);
ExpectRequiresSpecificUserSettings(kLivePageSharingNamespace, false);
}
TEST_F(ClientPolicyControllerTest, AllTemporaryNamespaces) {
std::vector<std::string> all_namespaces = GetAllPolicyNamespaces();
const std::vector<std::string>& cache_reset_namespaces_list =
GetTemporaryPolicyNamespaces();
std::set<std::string> cache_reset_namespaces(
cache_reset_namespaces_list.begin(), cache_reset_namespaces_list.end());
for (auto name_space : cache_reset_namespaces) {
if (cache_reset_namespaces.count(name_space) > 0) {
ExpectTemporary(name_space);
} else {
ExpectPersistent(name_space);
}
}
}
} // namespace offline_pages
|