| 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" |
| 18 #include "components/browsing_data/pref_names.h" |
| 17 #include "components/crx_file/id_util.h" | 19 #include "components/crx_file/id_util.h" |
| 18 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
| 19 #include "extensions/browser/extension_registry.h" | 21 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/common/extension_builder.h" | 22 #include "extensions/common/extension_builder.h" |
| 21 #include "extensions/common/value_builder.h" | 23 #include "extensions/common/value_builder.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 using extensions::DictionaryBuilder; | 28 using extensions::DictionaryBuilder; |
| 27 using extensions::ListBuilder; | 29 using extensions::ListBuilder; |
| 28 | 30 |
| 29 class HostedAppsCounterTest : public testing::Test { | 31 class HostedAppsCounterTest : public testing::Test { |
| 30 public: | 32 public: |
| 31 void SetUp() override { | 33 void SetUp() override { |
| 32 profile_.reset(new TestingProfile()); | 34 profile_.reset(new TestingProfile()); |
| 33 extension_registry_ = extensions::ExtensionRegistry::Get(profile_.get()); | 35 extension_registry_ = extensions::ExtensionRegistry::Get(profile_.get()); |
| 34 | 36 |
| 35 SetHostedAppsDeletionPref(true); | 37 SetHostedAppsDeletionPref(true); |
| 36 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 38 SetDeletionPeriodPref(browsing_data::EVERYTHING); |
| 37 } | 39 } |
| 38 | 40 |
| 39 // Adding and removing apps and extensions. ---------------------------------- | 41 // Adding and removing apps and extensions. ---------------------------------- |
| 40 | 42 |
| 41 std::string AddExtension() { | 43 std::string AddExtension() { |
| 42 return AddItem( | 44 return AddItem( |
| 43 base::GenerateGUID(), | 45 base::GenerateGUID(), |
| 44 std::unique_ptr<base::DictionaryValue>()); | 46 std::unique_ptr<base::DictionaryValue>()); |
| 45 } | 47 } |
| 46 | 48 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 void RemoveItem(const std::string& id) { | 94 void RemoveItem(const std::string& id) { |
| 93 extension_registry_->RemoveEnabled(id); | 95 extension_registry_->RemoveEnabled(id); |
| 94 } | 96 } |
| 95 | 97 |
| 96 // Setting preferences. ------------------------------------------------------ | 98 // Setting preferences. ------------------------------------------------------ |
| 97 | 99 |
| 98 void SetHostedAppsDeletionPref(bool value) { | 100 void SetHostedAppsDeletionPref(bool value) { |
| 99 GetProfile()->GetPrefs()->SetBoolean(prefs::kDeleteHostedAppsData, value); | 101 GetProfile()->GetPrefs()->SetBoolean(prefs::kDeleteHostedAppsData, value); |
| 100 } | 102 } |
| 101 | 103 |
| 102 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { | 104 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { |
| 103 GetProfile()->GetPrefs()->SetInteger( | 105 GetProfile()->GetPrefs()->SetInteger( |
| 104 prefs::kDeleteTimePeriod, static_cast<int>(period)); | 106 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); |
| 105 } | 107 } |
| 106 | 108 |
| 107 // Retrieving counter results. ----------------------------------------------- | 109 // Retrieving counter results. ----------------------------------------------- |
| 108 | 110 |
| 109 BrowsingDataCounter::ResultInt GetNumHostedApps() { | 111 browsing_data::BrowsingDataCounter::ResultInt GetNumHostedApps() { |
| 110 DCHECK(finished_); | 112 DCHECK(finished_); |
| 111 return num_apps_; | 113 return num_apps_; |
| 112 } | 114 } |
| 113 | 115 |
| 114 const std::vector<std::string>& GetExamples() { | 116 const std::vector<std::string>& GetExamples() { |
| 115 DCHECK(finished_); | 117 DCHECK(finished_); |
| 116 return examples_; | 118 return examples_; |
| 117 } | 119 } |
| 118 | 120 |
| 119 void Callback(std::unique_ptr<BrowsingDataCounter::Result> result) { | 121 void Callback( |
| 122 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 120 finished_ = result->Finished(); | 123 finished_ = result->Finished(); |
| 121 | 124 |
| 122 if (finished_) { | 125 if (finished_) { |
| 123 HostedAppsCounter::HostedAppsResult* hosted_apps_result = | 126 HostedAppsCounter::HostedAppsResult* hosted_apps_result = |
| 124 static_cast<HostedAppsCounter::HostedAppsResult*>(result.get()); | 127 static_cast<HostedAppsCounter::HostedAppsResult*>(result.get()); |
| 125 | 128 |
| 126 num_apps_ = hosted_apps_result->Value(); | 129 num_apps_ = hosted_apps_result->Value(); |
| 127 examples_ = hosted_apps_result->examples(); | 130 examples_ = hosted_apps_result->examples(); |
| 128 } | 131 } |
| 129 } | 132 } |
| 130 | 133 |
| 131 // Miscellaneous. ------------------------------------------------------------ | 134 // Miscellaneous. ------------------------------------------------------------ |
| 132 | 135 |
| 133 Profile* GetProfile() { | 136 Profile* GetProfile() { |
| 134 return profile_.get(); | 137 return profile_.get(); |
| 135 } | 138 } |
| 136 | 139 |
| 137 private: | 140 private: |
| 138 base::MessageLoop loop_; | 141 base::MessageLoop loop_; |
| 139 std::unique_ptr<TestingProfile> profile_; | 142 std::unique_ptr<TestingProfile> profile_; |
| 140 extensions::ExtensionRegistry* extension_registry_; | 143 extensions::ExtensionRegistry* extension_registry_; |
| 141 | 144 |
| 142 bool finished_; | 145 bool finished_; |
| 143 BrowsingDataCounter::ResultInt num_apps_; | 146 browsing_data::BrowsingDataCounter::ResultInt num_apps_; |
| 144 std::vector<std::string> examples_; | 147 std::vector<std::string> examples_; |
| 145 }; | 148 }; |
| 146 | 149 |
| 147 // Tests that we count the total number of hosted apps correctly. | 150 // Tests that we count the total number of hosted apps correctly. |
| 148 TEST_F(HostedAppsCounterTest, Count) { | 151 TEST_F(HostedAppsCounterTest, Count) { |
| 149 HostedAppsCounter counter; | 152 Profile* profile = GetProfile(); |
| 150 counter.Init(GetProfile(), | 153 HostedAppsCounter counter(profile); |
| 151 base::Bind(&HostedAppsCounterTest::Callback, | 154 counter.Init(profile->GetPrefs(), base::Bind(&HostedAppsCounterTest::Callback, |
| 152 base::Unretained(this))); | 155 base::Unretained(this))); |
| 153 counter.Restart(); | 156 counter.Restart(); |
| 154 EXPECT_EQ(0u, GetNumHostedApps()); | 157 EXPECT_EQ(0u, GetNumHostedApps()); |
| 155 | 158 |
| 156 std::string first_app = AddHostedApp(); | 159 std::string first_app = AddHostedApp(); |
| 157 AddHostedApp(); | 160 AddHostedApp(); |
| 158 std::string last_app = AddHostedApp(); | 161 std::string last_app = AddHostedApp(); |
| 159 | 162 |
| 160 counter.Restart(); | 163 counter.Restart(); |
| 161 EXPECT_EQ(3u, GetNumHostedApps()); | 164 EXPECT_EQ(3u, GetNumHostedApps()); |
| 162 | 165 |
| 163 RemoveItem(last_app); | 166 RemoveItem(last_app); |
| 164 RemoveItem(first_app); | 167 RemoveItem(first_app); |
| 165 counter.Restart(); | 168 counter.Restart(); |
| 166 EXPECT_EQ(1u, GetNumHostedApps()); | 169 EXPECT_EQ(1u, GetNumHostedApps()); |
| 167 | 170 |
| 168 AddHostedApp(); | 171 AddHostedApp(); |
| 169 counter.Restart(); | 172 counter.Restart(); |
| 170 EXPECT_EQ(2u, GetNumHostedApps()); | 173 EXPECT_EQ(2u, GetNumHostedApps()); |
| 171 } | 174 } |
| 172 | 175 |
| 173 // Tests that we only count hosted apps, not packaged apps or extensions. | 176 // Tests that we only count hosted apps, not packaged apps or extensions. |
| 174 TEST_F(HostedAppsCounterTest, OnlyHostedApps) { | 177 TEST_F(HostedAppsCounterTest, OnlyHostedApps) { |
| 175 HostedAppsCounter counter; | 178 Profile* profile = GetProfile(); |
| 176 counter.Init(GetProfile(), | 179 HostedAppsCounter counter(profile); |
| 177 base::Bind(&HostedAppsCounterTest::Callback, | 180 counter.Init(profile->GetPrefs(), base::Bind(&HostedAppsCounterTest::Callback, |
| 178 base::Unretained(this))); | 181 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(profile->GetPrefs(), base::Bind(&HostedAppsCounterTest::Callback, |
| 215 base::Unretained(this))); | 218 base::Unretained(this))); |
| 216 counter.Restart(); | 219 counter.Restart(); |
| 217 EXPECT_EQ(0u, GetExamples().size()); | 220 EXPECT_EQ(0u, GetExamples().size()); |
| 218 | 221 |
| 219 AddHostedAppWithName("App 1"); | 222 AddHostedAppWithName("App 1"); |
| 220 counter.Restart(); | 223 counter.Restart(); |
| 221 EXPECT_EQ(1u, GetExamples().size()); | 224 EXPECT_EQ(1u, GetExamples().size()); |
| 222 EXPECT_EQ("App 1", GetExamples().front()); | 225 EXPECT_EQ("App 1", GetExamples().front()); |
| 223 | 226 |
| 224 AddHostedAppWithName("App 2"); | 227 AddHostedAppWithName("App 2"); |
| 225 counter.Restart(); | 228 counter.Restart(); |
| 226 EXPECT_EQ(2u, GetExamples().size()); | 229 EXPECT_EQ(2u, GetExamples().size()); |
| 227 EXPECT_EQ("App 1", GetExamples().front()); | 230 EXPECT_EQ("App 1", GetExamples().front()); |
| 228 EXPECT_EQ("App 2", GetExamples().back()); | 231 EXPECT_EQ("App 2", GetExamples().back()); |
| 229 | 232 |
| 230 AddHostedAppWithName("App 3"); | 233 AddHostedAppWithName("App 3"); |
| 231 counter.Restart(); | 234 counter.Restart(); |
| 232 EXPECT_EQ(2u, GetExamples().size()); | 235 EXPECT_EQ(2u, GetExamples().size()); |
| 233 EXPECT_EQ("App 1", GetExamples().front()); | 236 EXPECT_EQ("App 1", GetExamples().front()); |
| 234 EXPECT_EQ("App 2", GetExamples().back()); | 237 EXPECT_EQ("App 2", GetExamples().back()); |
| 235 } | 238 } |
| 236 | 239 |
| 237 } // namespace | 240 } // namespace |
| OLD | NEW |