File: prefs_tab_helper_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 5,398 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_util.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "extensions/buildflags/buildflags.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"

namespace {

base::FilePath GetPreferencesFilePath() {
  base::FilePath test_data_directory;
  base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory);
  return test_data_directory.AppendASCII("profiles")
      .AppendASCII("web_prefs")
      .AppendASCII("Default")
      .Append(chrome::kPreferencesFilename);
}

}  // namespace

class PrefsTabHelperBrowserTest : public PlatformBrowserTest {
 protected:
  bool SetUpUserDataDirectory() override {
    base::FilePath user_data_directory;
    base::PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
    base::FilePath default_profile =
        user_data_directory.AppendASCII(TestingProfile::kTestUserProfileDir);
    if (!base::CreateDirectory(default_profile)) {
      LOG(ERROR) << "Can't create " << default_profile.MaybeAsASCII();
      return false;
    }
    base::FilePath pref_file = GetPreferencesFilePath();
    if (!base::PathExists(pref_file)) {
      LOG(ERROR) << "Doesn't exist " << pref_file.MaybeAsASCII();
      return false;
    }
    base::FilePath default_pref_file =
        default_profile.Append(chrome::kPreferencesFilename);
    if (!base::CopyFile(pref_file, default_pref_file)) {
      LOG(ERROR) << "Copy error from " << pref_file.MaybeAsASCII() << " to "
                 << default_pref_file.MaybeAsASCII();
      return false;
    }

#if BUILDFLAG(IS_WIN)
    // Make the copy writable.  On POSIX we assume the umask allows files
    // we create to be writable.
    if (!::SetFileAttributesW(default_pref_file.value().c_str(),
                              FILE_ATTRIBUTE_NORMAL)) {
      return false;
    }
#endif
    return true;
  }
};

// Tests that a sampling of web prefs are registered and ones with values in the
// test user preferences file take on those values.
IN_PROC_BROWSER_TEST_F(PrefsTabHelperBrowserTest, WebPrefs) {
  PrefService* prefs = chrome_test_utils::GetProfile(this)->GetPrefs();

  EXPECT_TRUE(
      prefs->FindPreference(prefs::kWebKitCursiveFontFamily)->IsDefaultValue());
  EXPECT_TRUE(
      prefs->FindPreference(prefs::kWebKitSerifFontFamily)->IsDefaultValue());
  EXPECT_TRUE(prefs->FindPreference(prefs::kWebKitSerifFontFamilyJapanese)
                  ->IsDefaultValue());

  EXPECT_EQ("windows-1251", prefs->GetString(prefs::kDefaultCharset));
  EXPECT_EQ(16, prefs->GetInteger(prefs::kWebKitDefaultFontSize));
  EXPECT_EQ("Noto Sans CJK KR",
            prefs->GetString(prefs::kWebKitStandardFontFamilyKorean));
  EXPECT_EQ("Tinos", prefs->GetString(prefs::kWebKitStandardFontFamily));
  EXPECT_EQ("DejaVu Sans", prefs->GetString(prefs::kWebKitSansSerifFontFamily));
}

// Tests that changes in browser preferences are reflected in Blink's web
// preferences. Note that these preferences are not handled on non-desktop
// Android, see http://crbug.com/308033, but can be modified by extension APIs
// on desktop Android.
#if !BUILDFLAG(IS_ANDROID) || BUILDFLAG(ENABLE_DESKTOP_ANDROID_EXTENSIONS)
IN_PROC_BROWSER_TEST_F(PrefsTabHelperBrowserTest, GenericFontFamilies) {
  PrefService* prefs = chrome_test_utils::GetProfile(this)->GetPrefs();
  prefs->SetString(prefs::kWebKitStandardFontFamily, "CustomStandard");
  prefs->SetString(prefs::kWebKitSerifFontFamily, "CustomSerif");
  prefs->SetString(prefs::kWebKitSansSerifFontFamily, "CustomSansSerif");
  prefs->SetString(prefs::kWebKitCursiveFontFamily, "CustomCursive");
  prefs->SetString(prefs::kWebKitFantasyFontFamily, "CustomFantasy");
  prefs->SetString(prefs::kWebKitFixedFontFamily, "CustomFixed");
  prefs->SetString(prefs::kWebKitMathFontFamily, "CustomMath");

  content::WebContents* web_contents =
      chrome_test_utils::GetActiveWebContents(this);
  web_contents->NotifyPreferencesChanged();
  blink::web_pref::WebPreferences web_prefs =
      web_contents->GetOrCreateWebPreferences();

  EXPECT_EQ(u"CustomStandard",
            web_prefs.standard_font_family_map[blink::web_pref::kCommonScript]);
  EXPECT_EQ(u"CustomSerif",
            web_prefs.serif_font_family_map[blink::web_pref::kCommonScript]);
  EXPECT_EQ(
      u"CustomSansSerif",
      web_prefs.sans_serif_font_family_map[blink::web_pref::kCommonScript]);
  EXPECT_EQ(u"CustomCursive",
            web_prefs.cursive_font_family_map[blink::web_pref::kCommonScript]);
  EXPECT_EQ(u"CustomFantasy",
            web_prefs.fantasy_font_family_map[blink::web_pref::kCommonScript]);
  EXPECT_EQ(u"CustomFixed",
            web_prefs.fixed_font_family_map[blink::web_pref::kCommonScript]);
  EXPECT_EQ(u"CustomMath",
            web_prefs.math_font_family_map[blink::web_pref::kCommonScript]);
}
#endif