| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/browsing_data/hosted_apps_counter.h" | 5 #include "chrome/browser/browsing_data/hosted_apps_counter.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/browsing_data/browsing_data_utils.h" |
| 17 #include "components/crx_file/id_util.h" | 18 #include "components/crx_file/id_util.h" |
| 18 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
| 19 #include "extensions/browser/extension_registry.h" | 20 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/common/extension_builder.h" | 21 #include "extensions/common/extension_builder.h" |
| 21 #include "extensions/common/value_builder.h" | 22 #include "extensions/common/value_builder.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 using extensions::DictionaryBuilder; | 27 using extensions::DictionaryBuilder; |
| 27 using extensions::ListBuilder; | 28 using extensions::ListBuilder; |
| 28 | 29 |
| 29 class HostedAppsCounterTest : public testing::Test { | 30 class HostedAppsCounterTest : public testing::Test { |
| 30 public: | 31 public: |
| 31 void SetUp() override { | 32 void SetUp() override { |
| 32 profile_.reset(new TestingProfile()); | 33 profile_.reset(new TestingProfile()); |
| 33 extension_registry_ = extensions::ExtensionRegistry::Get(profile_.get()); | 34 extension_registry_ = extensions::ExtensionRegistry::Get(profile_.get()); |
| 34 | 35 |
| 35 SetHostedAppsDeletionPref(true); | 36 SetHostedAppsDeletionPref(true); |
| 36 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 37 SetDeletionPeriodPref(EVERYTHING); |
| 37 } | 38 } |
| 38 | 39 |
| 39 // Adding and removing apps and extensions. ---------------------------------- | 40 // Adding and removing apps and extensions. ---------------------------------- |
| 40 | 41 |
| 41 std::string AddExtension() { | 42 std::string AddExtension() { |
| 42 return AddItem( | 43 return AddItem( |
| 43 base::GenerateGUID(), | 44 base::GenerateGUID(), |
| 44 std::unique_ptr<base::DictionaryValue>()); | 45 std::unique_ptr<base::DictionaryValue>()); |
| 45 } | 46 } |
| 46 | 47 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 void RemoveItem(const std::string& id) { | 93 void RemoveItem(const std::string& id) { |
| 93 extension_registry_->RemoveEnabled(id); | 94 extension_registry_->RemoveEnabled(id); |
| 94 } | 95 } |
| 95 | 96 |
| 96 // Setting preferences. ------------------------------------------------------ | 97 // Setting preferences. ------------------------------------------------------ |
| 97 | 98 |
| 98 void SetHostedAppsDeletionPref(bool value) { | 99 void SetHostedAppsDeletionPref(bool value) { |
| 99 GetProfile()->GetPrefs()->SetBoolean(prefs::kDeleteHostedAppsData, value); | 100 GetProfile()->GetPrefs()->SetBoolean(prefs::kDeleteHostedAppsData, value); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { | 103 void SetDeletionPeriodPref(TimePeriod period) { |
| 103 GetProfile()->GetPrefs()->SetInteger( | 104 GetProfile()->GetPrefs()->SetInteger( |
| 104 prefs::kDeleteTimePeriod, static_cast<int>(period)); | 105 prefs::kDeleteTimePeriod, static_cast<int>(period)); |
| 105 } | 106 } |
| 106 | 107 |
| 107 // Retrieving counter results. ----------------------------------------------- | 108 // Retrieving counter results. ----------------------------------------------- |
| 108 | 109 |
| 109 BrowsingDataCounter::ResultInt GetNumHostedApps() { | 110 BrowsingDataCounter::ResultInt GetNumHostedApps() { |
| 110 DCHECK(finished_); | 111 DCHECK(finished_); |
| 111 return num_apps_; | 112 return num_apps_; |
| 112 } | 113 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 139 std::unique_ptr<TestingProfile> profile_; | 140 std::unique_ptr<TestingProfile> profile_; |
| 140 extensions::ExtensionRegistry* extension_registry_; | 141 extensions::ExtensionRegistry* extension_registry_; |
| 141 | 142 |
| 142 bool finished_; | 143 bool finished_; |
| 143 BrowsingDataCounter::ResultInt num_apps_; | 144 BrowsingDataCounter::ResultInt num_apps_; |
| 144 std::vector<std::string> examples_; | 145 std::vector<std::string> examples_; |
| 145 }; | 146 }; |
| 146 | 147 |
| 147 // Tests that we count the total number of hosted apps correctly. | 148 // Tests that we count the total number of hosted apps correctly. |
| 148 TEST_F(HostedAppsCounterTest, Count) { | 149 TEST_F(HostedAppsCounterTest, Count) { |
| 149 HostedAppsCounter counter; | 150 Profile* profile = GetProfile(); |
| 150 counter.Init(GetProfile(), | 151 HostedAppsCounter counter(profile); |
| 151 base::Bind(&HostedAppsCounterTest::Callback, | 152 counter.Init( |
| 152 base::Unretained(this))); | 153 profile->GetPrefs(), prefs::kDeleteTimePeriod, |
| 154 base::Bind(&HostedAppsCounterTest::Callback, base::Unretained(this))); |
| 153 counter.Restart(); | 155 counter.Restart(); |
| 154 EXPECT_EQ(0u, GetNumHostedApps()); | 156 EXPECT_EQ(0u, GetNumHostedApps()); |
| 155 | 157 |
| 156 std::string first_app = AddHostedApp(); | 158 std::string first_app = AddHostedApp(); |
| 157 AddHostedApp(); | 159 AddHostedApp(); |
| 158 std::string last_app = AddHostedApp(); | 160 std::string last_app = AddHostedApp(); |
| 159 | 161 |
| 160 counter.Restart(); | 162 counter.Restart(); |
| 161 EXPECT_EQ(3u, GetNumHostedApps()); | 163 EXPECT_EQ(3u, GetNumHostedApps()); |
| 162 | 164 |
| 163 RemoveItem(last_app); | 165 RemoveItem(last_app); |
| 164 RemoveItem(first_app); | 166 RemoveItem(first_app); |
| 165 counter.Restart(); | 167 counter.Restart(); |
| 166 EXPECT_EQ(1u, GetNumHostedApps()); | 168 EXPECT_EQ(1u, GetNumHostedApps()); |
| 167 | 169 |
| 168 AddHostedApp(); | 170 AddHostedApp(); |
| 169 counter.Restart(); | 171 counter.Restart(); |
| 170 EXPECT_EQ(2u, GetNumHostedApps()); | 172 EXPECT_EQ(2u, GetNumHostedApps()); |
| 171 } | 173 } |
| 172 | 174 |
| 173 // Tests that we only count hosted apps, not packaged apps or extensions. | 175 // Tests that we only count hosted apps, not packaged apps or extensions. |
| 174 TEST_F(HostedAppsCounterTest, OnlyHostedApps) { | 176 TEST_F(HostedAppsCounterTest, OnlyHostedApps) { |
| 175 HostedAppsCounter counter; | 177 Profile* profile = GetProfile(); |
| 176 counter.Init(GetProfile(), | 178 HostedAppsCounter counter(profile); |
| 177 base::Bind(&HostedAppsCounterTest::Callback, | 179 counter.Init( |
| 178 base::Unretained(this))); | 180 profile->GetPrefs(), prefs::kDeleteTimePeriod, |
| 181 base::Bind(&HostedAppsCounterTest::Callback, base::Unretained(this))); |
| 179 | 182 |
| 180 AddHostedApp(); // 1 | 183 AddHostedApp(); // 1 |
| 181 AddExtension(); | 184 AddExtension(); |
| 182 AddPackagedApp(); | 185 AddPackagedApp(); |
| 183 AddExtension(); | 186 AddExtension(); |
| 184 counter.Restart(); | 187 counter.Restart(); |
| 185 EXPECT_EQ(1u, GetNumHostedApps()); | 188 EXPECT_EQ(1u, GetNumHostedApps()); |
| 186 | 189 |
| 187 AddHostedApp(); // 2 | 190 AddHostedApp(); // 2 |
| 188 AddHostedApp(); // 3 | 191 AddHostedApp(); // 3 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 202 AddExtension(); | 205 AddExtension(); |
| 203 AddExtension(); | 206 AddExtension(); |
| 204 AddPackagedApp(); | 207 AddPackagedApp(); |
| 205 counter.Restart(); | 208 counter.Restart(); |
| 206 EXPECT_EQ(5u, GetNumHostedApps()); | 209 EXPECT_EQ(5u, GetNumHostedApps()); |
| 207 } | 210 } |
| 208 | 211 |
| 209 // Tests that the counter results contain names of the first two hosted apps | 212 // Tests that the counter results contain names of the first two hosted apps |
| 210 // in lexicographic ordering. | 213 // in lexicographic ordering. |
| 211 TEST_F(HostedAppsCounterTest, Examples) { | 214 TEST_F(HostedAppsCounterTest, Examples) { |
| 212 HostedAppsCounter counter; | 215 Profile* profile = GetProfile(); |
| 213 counter.Init(GetProfile(), | 216 HostedAppsCounter counter(profile); |
| 214 base::Bind(&HostedAppsCounterTest::Callback, | 217 counter.Init( |
| 215 base::Unretained(this))); | 218 profile->GetPrefs(), prefs::kDeleteTimePeriod, |
| 219 base::Bind(&HostedAppsCounterTest::Callback, base::Unretained(this))); |
| 216 counter.Restart(); | 220 counter.Restart(); |
| 217 EXPECT_EQ(0u, GetExamples().size()); | 221 EXPECT_EQ(0u, GetExamples().size()); |
| 218 | 222 |
| 219 AddHostedAppWithName("App 1"); | 223 AddHostedAppWithName("App 1"); |
| 220 counter.Restart(); | 224 counter.Restart(); |
| 221 EXPECT_EQ(1u, GetExamples().size()); | 225 EXPECT_EQ(1u, GetExamples().size()); |
| 222 EXPECT_EQ("App 1", GetExamples().front()); | 226 EXPECT_EQ("App 1", GetExamples().front()); |
| 223 | 227 |
| 224 AddHostedAppWithName("App 2"); | 228 AddHostedAppWithName("App 2"); |
| 225 counter.Restart(); | 229 counter.Restart(); |
| 226 EXPECT_EQ(2u, GetExamples().size()); | 230 EXPECT_EQ(2u, GetExamples().size()); |
| 227 EXPECT_EQ("App 1", GetExamples().front()); | 231 EXPECT_EQ("App 1", GetExamples().front()); |
| 228 EXPECT_EQ("App 2", GetExamples().back()); | 232 EXPECT_EQ("App 2", GetExamples().back()); |
| 229 | 233 |
| 230 AddHostedAppWithName("App 3"); | 234 AddHostedAppWithName("App 3"); |
| 231 counter.Restart(); | 235 counter.Restart(); |
| 232 EXPECT_EQ(2u, GetExamples().size()); | 236 EXPECT_EQ(2u, GetExamples().size()); |
| 233 EXPECT_EQ("App 1", GetExamples().front()); | 237 EXPECT_EQ("App 1", GetExamples().front()); |
| 234 EXPECT_EQ("App 2", GetExamples().back()); | 238 EXPECT_EQ("App 2", GetExamples().back()); |
| 235 } | 239 } |
| 236 | 240 |
| 237 } // namespace | 241 } // namespace |
| OLD | NEW |