chromium/chrome/browser/browsing_data/counters/browsing_data_counter_utils_unittest.cc

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/browsing_data/counters/browsing_data_counter_utils.h"

#include <string>
#include <vector>

#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/browsing_data/counters/cache_counter.h"
#include "chrome/browser/browsing_data/counters/signin_data_counter.h"
#include "chrome/browser/browsing_data/counters/site_data_counter.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/password_manager/core/browser/password_store/test_password_store.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/test/test_sync_service.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/buildflags/buildflags.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/browsing_data/counters/tabs_counter.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#endif

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/strings/string_split.h"
#include "chrome/browser/browsing_data/counters/hosted_apps_counter.h"
#endif

namespace browsing_data_counter_utils {

class BrowsingDataCounterUtilsTest : public testing::Test {};

TEST_F(BrowsingDataCounterUtilsTest, CacheCounterResult) {}

#if BUILDFLAG(IS_ANDROID)
// Tests the output of the hosted apps counter.
TEST_F(BrowsingDataCounterUtilsTest, QuickDeleteAdvancedCacheCounterResult) {
  scoped_feature_list_.InitAndEnableFeature(
      chrome::android::kQuickDeleteForAndroid);

  // This test assumes that the strings are served exactly as defined,
  // i.e. that the locale is set to the default "en".
  ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale());
  const int kBytesInAMegabyte = 1024 * 1024;

  // Test the output for various forms of CacheResults.
  const struct TestCase {
    int bytes;
    bool is_upper_limit;
    std::string expected_output;
  } kTestCases[] = {
      {42, false,
       "Less than 1 MB. Some sites may load more slowly on your next "
       "visit."},
      {static_cast<int>(2.312 * kBytesInAMegabyte), false,
       "2.3 MB. Some sites may load more slowly on your next "
       "visit."},
      {static_cast<int>(2.312 * kBytesInAMegabyte), true,
       "Less than 2.3 MB. Some sites may load more slowly on your next "
       "visit."}};

  for (const TestCase& test_case : kTestCases) {
    CacheCounter counter(GetProfile());
    browsing_data::ClearBrowsingDataTab tab =
        browsing_data::ClearBrowsingDataTab::ADVANCED;
    counter.Init(GetProfile()->GetPrefs(), tab,
                 browsing_data::BrowsingDataCounter::ResultCallback());
    CacheCounter::CacheResult result(&counter, test_case.bytes,
                                     test_case.is_upper_limit);
    SCOPED_TRACE(base::StringPrintf("Test params: %d bytes, %d is_upper_limit",
                                    test_case.bytes, test_case.is_upper_limit));

    std::u16string output =
        GetChromeCounterTextFromResult(&result, GetProfile());
    EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output));
  }
}
#endif

#if BUILDFLAG(ENABLE_EXTENSIONS)
// Tests the complex output of the hosted apps counter.
TEST_F(BrowsingDataCounterUtilsTest, HostedAppsCounterResult) {}
#endif

// Tests the output for "Passwords and passkeys" on the advanced tab.
TEST_F(BrowsingDataCounterUtilsTest, DeletePasswordsAndSigninData) {}

#if BUILDFLAG(IS_ANDROID)
TEST_F(BrowsingDataCounterUtilsTest, TabsCounterResult) {
  // This test assumes that the strings are served exactly as defined,
  // i.e. that the locale is set to the default "en".
  ASSERT_EQ("en", TestingBrowserProcess::GetGlobal()->GetApplicationLocale());
  browsing_data::ClearBrowsingDataTab tab =
      browsing_data::ClearBrowsingDataTab::ADVANCED;

  // Test the output for various forms of CacheResults.
  const struct TestCase {
    int tab_count;
    int window_count;
    std::string expected_output;
  } kTestCases[] = {
      {0, 0, "None"},
      {1, 0, "1 tab on this device"},
      {5, 1, "5 tabs on this device"},
      {5, 2, "5 tabs from 2 windows on this device"},
  };

  for (const TestCase& test_case : kTestCases) {
    TabsCounter counter(GetProfile());
    counter.Init(GetProfile()->GetPrefs(), tab,
                 browsing_data::BrowsingDataCounter::ResultCallback());
    TabsCounter::TabsResult result(&counter, test_case.tab_count,
                                   test_case.window_count);
    SCOPED_TRACE(
        base::StringPrintf("Test params: %d tab_count, %d window_count.",
                           test_case.tab_count, test_case.window_count));

    std::u16string output =
        GetChromeCounterTextFromResult(&result, GetProfile());
    EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output));
  }
}
#endif  // BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

class CookieBrowsingDataCounterUtilsTest : public BrowsingDataCounterUtilsTest {};

TEST_F(CookieBrowsingDataCounterUtilsTest,
       CookieCounterResultExplicitSigninDisabled) {}

TEST_F(CookieBrowsingDataCounterUtilsTest, CookieCounterResult) {}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

}  // namespace browsing_data_counter_utils