| 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/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/web_data_service_factory.h" | |
| 14 #include "chrome/test/base/testing_browser_process.h" | 13 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/browsing_data/core/counters/autofill_counter.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 16 |
| 19 #if defined(ENABLE_EXTENSIONS) | 17 #if defined(ENABLE_EXTENSIONS) |
| 20 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 21 #include "chrome/browser/browsing_data/hosted_apps_counter.h" | 19 #include "chrome/browser/browsing_data/hosted_apps_counter.h" |
| 22 #endif | 20 #endif |
| 23 | 21 |
| 24 class BrowsingDataCounterUtilsTest : public testing::Test { | 22 class BrowsingDataCounterUtilsTest : public testing::Test { |
| 25 public: | 23 public: |
| 26 BrowsingDataCounterUtilsTest() {} | 24 BrowsingDataCounterUtilsTest() {} |
| 27 ~BrowsingDataCounterUtilsTest() override {} | 25 ~BrowsingDataCounterUtilsTest() override {} |
| 28 | 26 |
| 29 TestingProfile* GetProfile() { return &profile_; } | 27 TestingProfile* GetProfile() { return &profile_; } |
| 30 | 28 |
| 31 private: | 29 private: |
| 32 base::MessageLoop loop_; | 30 base::MessageLoop loop_; |
| 33 TestingProfile profile_; | 31 TestingProfile profile_; |
| 34 }; | 32 }; |
| 35 | 33 |
| 36 // Tests the complex output of the Autofill counter. | |
| 37 TEST_F(BrowsingDataCounterUtilsTest, AutofillCounterResult) { | |
| 38 browsing_data::AutofillCounter counter( | |
| 39 WebDataServiceFactory::GetAutofillWebDataForProfile( | |
| 40 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS) | |
| 41 .get()); | |
| 42 | |
| 43 // This test assumes that the strings are served exactly as defined, | |
| 44 // i.e. that the locale is set to the default "en". | |
| 45 ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale()); | |
| 46 | |
| 47 // Test all configurations of zero and nonzero partial results for datatypes. | |
| 48 // Test singular and plural for each datatype. | |
| 49 const struct TestCase { | |
| 50 int num_credit_cards; | |
| 51 int num_addresses; | |
| 52 int num_suggestions; | |
| 53 std::string expected_output; | |
| 54 } kTestCases[] = { | |
| 55 {0, 0, 0, "none"}, | |
| 56 {1, 0, 0, "1 credit card"}, | |
| 57 {0, 5, 0, "5 addresses"}, | |
| 58 {0, 0, 1, "1 suggestion"}, | |
| 59 {0, 0, 2, "2 suggestions"}, | |
| 60 {4, 7, 0, "4 credit cards, 7 addresses"}, | |
| 61 {3, 0, 9, "3 credit cards, 9 other suggestions"}, | |
| 62 {0, 1, 1, "1 address, 1 other suggestion"}, | |
| 63 {9, 6, 3, "9 credit cards, 6 addresses, 3 others"}, | |
| 64 {4, 2, 1, "4 credit cards, 2 addresses, 1 other"}, | |
| 65 }; | |
| 66 | |
| 67 for (const TestCase& test_case : kTestCases) { | |
| 68 browsing_data::AutofillCounter::AutofillResult result( | |
| 69 &counter, test_case.num_suggestions, test_case.num_credit_cards, | |
| 70 test_case.num_addresses); | |
| 71 | |
| 72 SCOPED_TRACE(base::StringPrintf( | |
| 73 "Test params: %d credit card(s), " | |
| 74 "%d address(es), %d suggestion(s).", | |
| 75 test_case.num_credit_cards, | |
| 76 test_case.num_addresses, | |
| 77 test_case.num_suggestions | |
| 78 )); | |
| 79 | |
| 80 base::string16 output = GetCounterTextFromResult(&result); | |
| 81 EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 #if defined(ENABLE_EXTENSIONS) | 34 #if defined(ENABLE_EXTENSIONS) |
| 86 // Tests the complex output of the hosted apps counter. | 35 // Tests the complex output of the hosted apps counter. |
| 87 TEST_F(BrowsingDataCounterUtilsTest, HostedAppsCounterResult) { | 36 TEST_F(BrowsingDataCounterUtilsTest, HostedAppsCounterResult) { |
| 88 HostedAppsCounter counter(GetProfile()); | 37 HostedAppsCounter counter(GetProfile()); |
| 89 | 38 |
| 90 // This test assumes that the strings are served exactly as defined, | 39 // This test assumes that the strings are served exactly as defined, |
| 91 // i.e. that the locale is set to the default "en". | 40 // i.e. that the locale is set to the default "en". |
| 92 ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale()); | 41 ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale()); |
| 93 | 42 |
| 94 // Test the output for various numbers of hosted apps. | 43 // Test the output for various numbers of hosted apps. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 113 // The first two apps in the list are used as examples. | 62 // The first two apps in the list are used as examples. |
| 114 std::vector<std::string> examples; | 63 std::vector<std::string> examples; |
| 115 examples.assign( | 64 examples.assign( |
| 116 apps.begin(), apps.begin() + (apps.size() > 2 ? 2 : apps.size())); | 65 apps.begin(), apps.begin() + (apps.size() > 2 ? 2 : apps.size())); |
| 117 | 66 |
| 118 HostedAppsCounter::HostedAppsResult result( | 67 HostedAppsCounter::HostedAppsResult result( |
| 119 &counter, | 68 &counter, |
| 120 apps.size(), | 69 apps.size(), |
| 121 examples); | 70 examples); |
| 122 | 71 |
| 123 base::string16 output = GetCounterTextFromResult(&result); | 72 base::string16 output = GetChromeCounterTextFromResult(&result); |
| 124 EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output)); | 73 EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output)); |
| 125 } | 74 } |
| 126 } | 75 } |
| 127 #endif | 76 #endif |
| OLD | NEW |