OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browsing_data_counter_utils.h" | 5 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/stringprintf.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/browsing_data/cache_counter.h" |
12 #include "chrome/test/base/testing_browser_process.h" | 14 #include "chrome/test/base/testing_browser_process.h" |
13 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "extensions/features/features.h" | 17 #include "extensions/features/features.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 #if BUILDFLAG(ENABLE_EXTENSIONS) | 20 #if BUILDFLAG(ENABLE_EXTENSIONS) |
19 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
20 #include "chrome/browser/browsing_data/hosted_apps_counter.h" | 22 #include "chrome/browser/browsing_data/hosted_apps_counter.h" |
21 #endif | 23 #endif |
22 | 24 |
23 class BrowsingDataCounterUtilsTest : public testing::Test { | 25 class BrowsingDataCounterUtilsTest : public testing::Test { |
24 public: | 26 public: |
25 BrowsingDataCounterUtilsTest() {} | 27 BrowsingDataCounterUtilsTest() {} |
26 ~BrowsingDataCounterUtilsTest() override {} | 28 ~BrowsingDataCounterUtilsTest() override {} |
27 | 29 |
28 TestingProfile* GetProfile() { return &profile_; } | 30 TestingProfile* GetProfile() { return &profile_; } |
29 | 31 |
30 private: | 32 private: |
31 content::TestBrowserThreadBundle thread_bundle_; | 33 content::TestBrowserThreadBundle thread_bundle_; |
32 TestingProfile profile_; | 34 TestingProfile profile_; |
33 }; | 35 }; |
34 | 36 |
| 37 TEST_F(BrowsingDataCounterUtilsTest, CacheCounterResult) { |
| 38 // This test assumes that the strings are served exactly as defined, |
| 39 // i.e. that the locale is set to the default "en". |
| 40 ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale()); |
| 41 const int kBytesInAMegabyte = 1024 * 1024; |
| 42 |
| 43 // Test the output for various forms of CacheResults. |
| 44 const struct TestCase { |
| 45 int bytes; |
| 46 bool is_upper_limit; |
| 47 bool is_basic_tab; |
| 48 std::string expected_output; |
| 49 } kTestCases[] = { |
| 50 {42, false, false, "Less than 1 MB"}, |
| 51 {42, false, true, |
| 52 "Frees up less than 1 MB. Some sites may load more slowly on your next " |
| 53 "visit."}, |
| 54 {2.312 * kBytesInAMegabyte, false, false, "2.3 MB"}, |
| 55 {2.312 * kBytesInAMegabyte, false, true, |
| 56 "Frees up 2.3 MB. Some sites may load more slowly on your next visit."}, |
| 57 {2.312 * kBytesInAMegabyte, true, false, "Less than 2.3 MB"}, |
| 58 // TODO(725401): Fix upper case L in estimate string for desktop. |
| 59 {2.312 * kBytesInAMegabyte, true, true, |
| 60 "Frees up Less than 2.3 MB. Some sites may load more slowly on your " |
| 61 "next visit."}, |
| 62 {500.2 * kBytesInAMegabyte, false, false, "500 MB"}, |
| 63 {500.2 * kBytesInAMegabyte, true, false, "Less than 500 MB"}, |
| 64 }; |
| 65 |
| 66 for (const TestCase& test_case : kTestCases) { |
| 67 CacheCounter counter(GetProfile()); |
| 68 browsing_data::ClearBrowsingDataTab tab = |
| 69 test_case.is_basic_tab ? browsing_data::ClearBrowsingDataTab::BASIC |
| 70 : browsing_data::ClearBrowsingDataTab::ADVANCED; |
| 71 counter.Init(GetProfile()->GetPrefs(), tab, |
| 72 browsing_data::BrowsingDataCounter::Callback()); |
| 73 CacheCounter::CacheResult result(&counter, test_case.bytes, |
| 74 test_case.is_upper_limit); |
| 75 SCOPED_TRACE(base::StringPrintf( |
| 76 "Test params: %d bytes, %d is_upper_limit, %d is_basic_tab.", |
| 77 test_case.bytes, test_case.is_upper_limit, test_case.is_basic_tab)); |
| 78 |
| 79 base::string16 output = GetChromeCounterTextFromResult(&result); |
| 80 EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output)); |
| 81 } |
| 82 } |
| 83 |
35 #if BUILDFLAG(ENABLE_EXTENSIONS) | 84 #if BUILDFLAG(ENABLE_EXTENSIONS) |
36 // Tests the complex output of the hosted apps counter. | 85 // Tests the complex output of the hosted apps counter. |
37 TEST_F(BrowsingDataCounterUtilsTest, HostedAppsCounterResult) { | 86 TEST_F(BrowsingDataCounterUtilsTest, HostedAppsCounterResult) { |
38 HostedAppsCounter counter(GetProfile()); | 87 HostedAppsCounter counter(GetProfile()); |
39 | 88 |
40 // This test assumes that the strings are served exactly as defined, | 89 // This test assumes that the strings are served exactly as defined, |
41 // i.e. that the locale is set to the default "en". | 90 // i.e. that the locale is set to the default "en". |
42 ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale()); | 91 ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale()); |
43 | 92 |
44 // Test the output for various numbers of hosted apps. | 93 // Test the output for various numbers of hosted apps. |
(...skipping 23 matching lines...) Expand all Loading... |
68 HostedAppsCounter::HostedAppsResult result( | 117 HostedAppsCounter::HostedAppsResult result( |
69 &counter, | 118 &counter, |
70 apps.size(), | 119 apps.size(), |
71 examples); | 120 examples); |
72 | 121 |
73 base::string16 output = GetChromeCounterTextFromResult(&result); | 122 base::string16 output = GetChromeCounterTextFromResult(&result); |
74 EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output)); | 123 EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output)); |
75 } | 124 } |
76 } | 125 } |
77 #endif | 126 #endif |
OLD | NEW |