File: extension_error_ui_default_unittest.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 (203 lines) | stat: -rw-r--r-- 7,926 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2019 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/extensions/extension_error_ui_desktop.h"

#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_error_ui.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/blocklist_extension_prefs.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/mock_extension_system.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_set.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

class TestErrorUIDelegate : public extensions::ExtensionErrorUI::Delegate {
 public:
  // extensions::ExtensionErrorUI::Delegate:
  content::BrowserContext* GetContext() override { return &profile_; }
  const extensions::ExtensionSet& GetBlocklistedExtensions() override {
    return forbidden_;
  }
  void OnAlertDetails() override {}
  void OnAlertAccept() override {}
  void OnAlertClosed() override {}

  void InsertForbidden(scoped_refptr<const extensions::Extension> ext) {
    forbidden_.Insert(ext);
  }

 private:
  content::BrowserTaskEnvironment environment_;
  TestingProfile profile_;
  extensions::ExtensionSet forbidden_;
};

class ManagementPolicyMock : public extensions::ManagementPolicy::Provider {
 public:
  ManagementPolicyMock(const extensions::Extension* extension, bool may_load)
      : extension_(extension), may_load_(may_load) {}

  std::string GetDebugPolicyProviderName() const override {
    return "ManagementPolicyMock";
  }

  bool UserMayLoad(const extensions::Extension* extension,
                   std::u16string* error) const override {
    EXPECT_EQ(extension_, extension);
    return may_load_;
  }

 private:
  raw_ptr<const extensions::Extension> extension_;
  bool may_load_;
};

}  // namespace

TEST(ExtensionErrorUIDesktopTest, BubbleTitleAndMessageMentionsExtension) {
  TestErrorUIDelegate delegate;

  delegate.InsertForbidden(extensions::ExtensionBuilder("Bar").Build());
  delegate.InsertForbidden(extensions::ExtensionBuilder("Baz").Build());

  extensions::ExtensionErrorUIDesktop ui(&delegate);
  GlobalErrorWithStandardBubble* bubble = ui.GetErrorForTesting();

  std::u16string title = bubble->GetBubbleViewTitle();
  EXPECT_THAT(title,
              l10n_util::GetPluralStringFUTF16(IDS_EXTENSION_ALERT_TITLE, 2));

  std::vector<std::u16string> messages = bubble->GetBubbleViewMessages();

  EXPECT_THAT(messages,
              testing::ElementsAre(
                  l10n_util::GetStringUTF16(
                      IDS_EXTENSIONS_ALERT_ITEM_BLOCKLISTED_MALWARE_TITLE),
                  l10n_util::GetStringFUTF16(
                      IDS_BLOCKLISTED_EXTENSIONS_ALERT_ITEM, u"Bar"),
                  l10n_util::GetStringFUTF16(
                      IDS_BLOCKLISTED_EXTENSIONS_ALERT_ITEM, u"Baz")));
}

// Test that unusually long extension names in the bubble message are truncated.
TEST(ExtensionErrorUIDesktopTest, BubbleMessageWithLongNameExtension) {
  std::string long_name_a(128, 'a');
  std::string long_name_b(128, 'b');

  TestErrorUIDelegate delegate;
  delegate.InsertForbidden(extensions::ExtensionBuilder(long_name_a).Build());
  delegate.InsertForbidden(extensions::ExtensionBuilder(long_name_b).Build());

  extensions::ExtensionErrorUIDesktop ui(&delegate);
  GlobalErrorWithStandardBubble* bubble = ui.GetErrorForTesting();

  std::u16string title = bubble->GetBubbleViewTitle();
  EXPECT_THAT(title,
              l10n_util::GetPluralStringFUTF16(IDS_EXTENSION_ALERT_TITLE, 2));

  std::vector<std::u16string> messages = bubble->GetBubbleViewMessages();

  // Expected values of extension names in the returned messages.
  std::u16string truncated_name_a =
      extensions::util::GetFixupExtensionNameForUIDisplay(long_name_a);
  std::u16string truncated_name_b =
      extensions::util::GetFixupExtensionNameForUIDisplay(long_name_b);

  ASSERT_LT(truncated_name_a.size(), long_name_a.size());
  ASSERT_LT(truncated_name_b.size(), long_name_b.size());

  EXPECT_THAT(
      messages,
      testing::ElementsAre(
          l10n_util::GetStringUTF16(
              IDS_EXTENSIONS_ALERT_ITEM_BLOCKLISTED_MALWARE_TITLE),
          l10n_util::GetStringFUTF16(IDS_BLOCKLISTED_EXTENSIONS_ALERT_ITEM,
                                     truncated_name_a),
          l10n_util::GetStringFUTF16(IDS_BLOCKLISTED_EXTENSIONS_ALERT_ITEM,
                                     truncated_name_b)));
}

TEST(ExtensionErrorUIDesktopTest, BubbleTitleAndMessageMentionsApp) {
  TestErrorUIDelegate delegate;

  delegate.InsertForbidden(
      extensions::ExtensionBuilder(
          "Bar", extensions::ExtensionBuilder::Type::PLATFORM_APP)
          .Build());

  extensions::ExtensionErrorUIDesktop ui(&delegate);
  GlobalErrorWithStandardBubble* bubble = ui.GetErrorForTesting();

  std::u16string title = bubble->GetBubbleViewTitle();
  EXPECT_THAT(title, l10n_util::GetPluralStringFUTF16(IDS_APP_ALERT_TITLE, 1));

  std::vector<std::u16string> messages = bubble->GetBubbleViewMessages();

  EXPECT_THAT(messages,
              testing::ElementsAre(l10n_util::GetStringFUTF16(
                  IDS_EXTENSION_ALERT_ITEM_BLOCKLISTED_MALWARE, u"Bar")));
}

TEST(ExtensionErrorUIDesktopTest, BubbleMessageMentionsMalware) {
  TestErrorUIDelegate delegate;
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder(
          "Bar", extensions::ExtensionBuilder::Type::PLATFORM_APP)
          .Build();
  extensions::blocklist_prefs::AddOmahaBlocklistState(
      extension->id(), extensions::BitMapBlocklistState::BLOCKLISTED_MALWARE,
      extensions::ExtensionPrefs::Get(delegate.GetContext()));
  delegate.InsertForbidden(extension);

  extensions::ExtensionErrorUIDesktop ui(&delegate);
  GlobalErrorWithStandardBubble* bubble = ui.GetErrorForTesting();

  std::u16string title = bubble->GetBubbleViewTitle();
  EXPECT_THAT(title, l10n_util::GetPluralStringFUTF16(IDS_APP_ALERT_TITLE, 1));

  std::vector<std::u16string> messages = bubble->GetBubbleViewMessages();

  EXPECT_THAT(messages, testing::ElementsAre(l10n_util::GetStringFUTF16(
                            IDS_EXTENSION_ALERT_ITEM_BLOCKLISTED_MALWARE,
                            base::UTF8ToUTF16(extension->name()))));
}

TEST(ExtensionErrorUIDesktopTest, BubbleTitleForEnterpriseBlockedExtensions) {
  TestErrorUIDelegate delegate;

  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder("Bar").Build();
  delegate.InsertForbidden(extension);

  extensions::ExtensionErrorUIDesktop ui(&delegate);
  ManagementPolicyMock provider(extension.get(), false);
  std::unique_ptr<extensions::ManagementPolicy> management_policy =
      std::make_unique<extensions::ManagementPolicy>();
  management_policy->RegisterProvider(&provider);
  ui.SetManagementPolicyForTesting(management_policy.get());
  GlobalErrorWithStandardBubble* bubble = ui.GetErrorForTesting();
  std::u16string title = bubble->GetBubbleViewTitle();
  EXPECT_THAT(title, l10n_util::GetPluralStringFUTF16(
                         IDS_POLICY_BLOCKED_EXTENSION_ALERT_TITLE, 1));

  std::vector<std::u16string> messages = bubble->GetBubbleViewMessages();

  EXPECT_THAT(messages,
              testing::ElementsAre(l10n_util::GetStringFUTF16(
                  IDS_POLICY_BLOCKED_EXTENSION_ALERT_ITEM_DETAIL, u"Bar")));
}