File: theme_service_test_utils.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 6,094 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2022 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/themes/theme_service_test_utils.h"

#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/themes/theme_syncable_service.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace theme_service::test {

std::ostream& operator<<(std::ostream& os, PrintableSkColor printable_color) {
  SkColor color = printable_color.color;
  return os << base::StringPrintf("#%02x%02x%02x%02x", SkColorGetA(color),
                                  SkColorGetR(color), SkColorGetG(color),
                                  SkColorGetB(color));
}

std::string ColorIdToString(ui::ColorId id) {
#define E_CPONLY(color_id, ...) {color_id, #color_id},

  static constexpr const auto kMap =
      base::MakeFixedFlatMap<ui::ColorId, const char*>({CHROME_COLOR_IDS});

#undef E_CPONLY
  return kMap.find(id)->second;
}

scoped_refptr<extensions::Extension> MakeThemeExtension(
    const base::FilePath& extension_path,
    const std::string& id,
    const std::string& name,
    extensions::mojom::ManifestLocation location,
    const std::string& update_url) {
  base::Value::Dict source;
  source.Set(extensions::manifest_keys::kName, name);
  source.Set(extensions::manifest_keys::kTheme, base::Value::Dict());
  source.Set(extensions::manifest_keys::kUpdateURL, update_url);
  source.Set(extensions::manifest_keys::kVersion, "0.0.0.0");
  std::string error;
  scoped_refptr<extensions::Extension> extension =
      extensions::Extension::Create(extension_path, location, source,
                                    extensions::Extension::NO_FLAGS, id,
                                    &error);
  EXPECT_TRUE(extension.get());
  EXPECT_EQ("", error);
  return extension;
}

syncer::SyncDataList MakeThemeDataList(
    const sync_pb::ThemeSpecifics& theme_specifics) {
  syncer::SyncDataList list;
  sync_pb::EntitySpecifics entity_specifics;
  entity_specifics.mutable_theme()->CopyFrom(theme_specifics);
  list.push_back(syncer::SyncData::CreateLocalData(
      ThemeSyncableService::kSyncEntityClientTag,
      ThemeSyncableService::kSyncEntityTitle, entity_specifics));
  return list;
}

syncer::SyncChangeList MakeThemeChangeList(
    const sync_pb::ThemeSpecifics& theme_specifics) {
  syncer::SyncChangeList change_list;
  sync_pb::EntitySpecifics entity_specifics;
  entity_specifics.mutable_theme()->CopyFrom(theme_specifics);
  change_list.emplace_back(
      FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
      syncer::SyncData::CreateRemoteData(
          entity_specifics, syncer::ClientTagHash::FromHashed("unused")));
  return change_list;
}

sync_pb::ThemeSpecifics EmptySpecifics() {
  sync_pb::ThemeSpecifics theme_specifics;
  theme_specifics.set_use_custom_theme(false);
  theme_specifics.set_use_system_theme_by_default(false);
  theme_specifics.set_browser_color_scheme(
      ::sync_pb::ThemeSpecifics_BrowserColorScheme_SYSTEM);
  return theme_specifics;
}

sync_pb::ThemeSpecifics CreateThemeSpecificsWithExtensionTheme(
    const std::string& id,
    const std::string& name,
    const std::string& url) {
  sync_pb::ThemeSpecifics theme_specifics;
  theme_specifics.set_use_system_theme_by_default(false);
  theme_specifics.set_browser_color_scheme(
      sync_pb::ThemeSpecifics_BrowserColorScheme_SYSTEM);
  theme_specifics.set_use_custom_theme(true);
  theme_specifics.set_custom_theme_id(id);
  theme_specifics.set_custom_theme_name(name);
  theme_specifics.set_custom_theme_update_url(url);
  return theme_specifics;
}

sync_pb::ThemeSpecifics CreateThemeSpecificsWithAutogeneratedColorTheme() {
  sync_pb::ThemeSpecifics theme_specifics;
  theme_specifics.set_use_custom_theme(false);
  theme_specifics.set_use_system_theme_by_default(false);
  theme_specifics.set_browser_color_scheme(
      sync_pb::ThemeSpecifics_BrowserColorScheme_SYSTEM);
  theme_specifics.mutable_autogenerated_color_theme()->set_color(SK_ColorRED);
  return theme_specifics;
}

sync_pb::ThemeSpecifics CreateThemeSpecificsWithColorTheme() {
  sync_pb::ThemeSpecifics theme_specifics;
  theme_specifics.set_use_custom_theme(false);
  theme_specifics.set_use_system_theme_by_default(false);
  theme_specifics.set_browser_color_scheme(
      sync_pb::ThemeSpecifics_BrowserColorScheme_SYSTEM);
  sync_pb::ThemeSpecifics::UserColorTheme* user_color_theme =
      theme_specifics.mutable_user_color_theme();
  user_color_theme->set_color(SK_ColorRED);
  user_color_theme->set_browser_color_variant(
      sync_pb::ThemeSpecifics_UserColorTheme_BrowserColorVariant_TONAL_SPOT);
  return theme_specifics;
}

sync_pb::ThemeSpecifics CreateThemeSpecificsWithGrayscaleTheme() {
  sync_pb::ThemeSpecifics theme_specifics;
  theme_specifics.set_use_custom_theme(false);
  theme_specifics.set_use_system_theme_by_default(false);
  theme_specifics.set_browser_color_scheme(
      sync_pb::ThemeSpecifics_BrowserColorScheme_SYSTEM);
  theme_specifics.mutable_grayscale_theme_enabled();
  return theme_specifics;
}

sync_pb::ThemeSpecifics CreateThemeSpecificsWithCustomNtpBackground(
    const std::string& background_url) {
  sync_pb::ThemeSpecifics theme_specifics;
  theme_specifics.set_use_custom_theme(false);
  theme_specifics.set_use_system_theme_by_default(false);
  theme_specifics.set_browser_color_scheme(
      sync_pb::ThemeSpecifics_BrowserColorScheme_SYSTEM);
  sync_pb::ThemeSpecifics::NtpCustomBackground* background =
      theme_specifics.mutable_ntp_background();
  background->set_url(background_url);
  background->set_attribution_line_1("");
  background->set_attribution_line_2("");
  background->set_attribution_action_url("");
  background->set_collection_id("");
  background->set_resume_token("");
  background->set_refresh_timestamp_unix_epoch_seconds(0);
  return theme_specifics;
}

}  // namespace theme_service::test