File: exclusive_access_bubble_views_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (309 lines) | stat: -rw-r--r-- 12,253 bytes parent folder | download | duplicates (4)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2024 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/ui/views/exclusive_access_bubble_views.h"

#include <string>

#include "base/strings/string_util.h"
#include "build/buildflag.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/ui/accelerator_utils.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_bubble_type.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "components/fullscreen_control/subtle_notification_view.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/accessibility/view_accessibility.h"

namespace {

enum UserGoal {
  kExitFullscreen = 0,
  kExitPointerLock = 1,
  kExitFullscreenAndSeeDownload = 2,
  kSeeDownload = 3,
};

enum Shortcut {
  kPressEsc = 0,
  kPressAndHoldEsc = 1,
  kAccelerator = 2,
};

struct InstructionTextTestCase {
  std::string test_name;
  ExclusiveAccessBubbleType type;
  UserGoal goal;
  Shortcut shortcut;
  bool enable_feature = false;
};

std::u16string GetUserGoalText(UserGoal goal) {
  switch (goal) {
    case kExitFullscreen:
      return u"To exit full screen";
    case kExitPointerLock:
      return u"To show your cursor";
    case kExitFullscreenAndSeeDownload:
      return u"To exit full screen and see download";
    case kSeeDownload:
      return u"Download started. To see it";
    default:
      return u"";
  }
}

std::u16string GetEscShortcutString(bool press_and_hold) {
  std::u16string esc = u"Esc";
#if BUILDFLAG(IS_MAC)
  esc = u"esc";
#endif

  return (press_and_hold ? u"press and hold |" : u"press |") + esc + u"|";
}

}  // namespace

class ExclusiveAccessBubbleViewsTest : public TestWithBrowserView {
 public:
  ExclusiveAccessBubbleViewsTest() = default;

  void SetUp() override {
    TestWithBrowserView::SetUp();
    ExclusiveAccessBubbleParams params{
        .type = EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION};
    bubble_view_ = std::make_unique<ExclusiveAccessBubbleViews>(
        browser_view(), params, base::NullCallback());
  }

  void TearDown() override {
    bubble_view_.reset();
    TestWithBrowserView::TearDown();
  }

  void UpdateExclusiveAccessBubbleType(ExclusiveAccessBubbleType type,
                                       bool has_download) {
    ExclusiveAccessBubbleParams params{
        .type = type, .has_download = has_download, .force_update = true};
    bubble_view_->Update(params, base::NullCallback());
  }

  std::u16string GetFullscreenAcceleratorString() {
#if BUILDFLAG(IS_CHROMEOS)
    return u"Fullscreen";
#else
    ui::Accelerator accelerator;
    chrome::AcceleratorProviderForBrowser(browser_view()->browser())
        ->GetAcceleratorForCommandId(IDC_FULLSCREEN, &accelerator);
    return accelerator.GetShortcutText();
#endif
  }

  std::u16string GetShortcutText(Shortcut shortcut) {
    switch (shortcut) {
      case kPressEsc:
        return GetEscShortcutString(false);
      case kPressAndHoldEsc:
        return GetEscShortcutString(true);
      case kAccelerator:
        return u"press |" + GetFullscreenAcceleratorString() + u"|";
      default:
        return u"";
    }
  }

  std::u16string CreateInstructionText(UserGoal goal, Shortcut shortcut) {
    return GetUserGoalText(goal) + u", " + GetShortcutText(shortcut);
  }

  std::u16string GetInstructionViewText() {
    return static_cast<SubtleNotificationView*>(bubble_view_->GetView())
        ->GetInstructionTextForTest();
  }

  SubtleNotificationView* GetSubtleNotificationView() {
    return static_cast<SubtleNotificationView*>(bubble_view_->GetView());
  }

 protected:
  std::unique_ptr<ExclusiveAccessBubbleViews> bubble_view_;
};

class ExclusiveAccessBubbleViewsInstructionTextTest
    : public ExclusiveAccessBubbleViewsTest,
      public testing::WithParamInterface<InstructionTextTestCase> {
 public:
  ExclusiveAccessBubbleViewsInstructionTextTest() {
    if (GetParam().enable_feature) {
      scoped_feature_list_.InitAndEnableFeature(
          features::kPressAndHoldEscToExitBrowserFullscreen);
    } else {
      scoped_feature_list_.InitAndDisableFeature(
          features::kPressAndHoldEscToExitBrowserFullscreen);
    }
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_P(ExclusiveAccessBubbleViewsInstructionTextTest, UpdateViewContent) {
  const InstructionTextTestCase& test_case = GetParam();
  UpdateExclusiveAccessBubbleType(test_case.type, /*has_download=*/false);
  EXPECT_EQ(GetInstructionViewText(),
            CreateInstructionText(test_case.goal, test_case.shortcut));
}

TEST_F(ExclusiveAccessBubbleViewsTest,
       SubtleNotificationViewAccessibleProperties) {
  ui::AXNodeData data;
  GetSubtleNotificationView()->GetViewAccessibility().GetAccessibleNodeData(
      &data);
  EXPECT_EQ(data.role, ax::mojom::Role::kAlert);
  EXPECT_EQ(GetSubtleNotificationView()->GetViewAccessibility().GetCachedRole(),
            ax::mojom::Role::kAlert);

  GetSubtleNotificationView()->UpdateContent(u"Sample |Accessible| Text");

  data = ui::AXNodeData();
  GetSubtleNotificationView()->GetViewAccessibility().GetAccessibleNodeData(
      &data);
  EXPECT_EQ(data.GetString16Attribute(ax::mojom::StringAttribute::kName),
            u"Sample Accessible Text");
}

// Tests where the bubble is updated once with the specified type.
INSTANTIATE_TEST_SUITE_P(
    /* no label */,
    ExclusiveAccessBubbleViewsInstructionTextTest,
    testing::ValuesIn<InstructionTextTestCase>({
        {"tabFullscreen",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressEsc, false},
        {"tabFullscreenAndPointerLock",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_POINTERLOCK_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressEsc, false},
        {"pointerLock",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_POINTERLOCK_EXIT_INSTRUCTION,
         UserGoal::kExitPointerLock, Shortcut::kPressEsc, false},
        {"tabFullscreenAndKeyboardLock",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_KEYBOARD_LOCK_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressAndHoldEsc, false},
        {"browserFullscreen",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kAccelerator, false},
        {"extensionInitiatedBrowserFullscreen",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kAccelerator, false},
        {"tabFullscreen_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressEsc, true},
        {"tabFullscreenAndPointerLock_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_POINTERLOCK_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressEsc, true},
        {"pointerLock_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_POINTERLOCK_EXIT_INSTRUCTION,
         UserGoal::kExitPointerLock, Shortcut::kPressEsc, true},
        {"tabFullscreenAndKeyboardLock_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_KEYBOARD_LOCK_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressAndHoldEsc, true},
        {"browserFullscreen_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressAndHoldEsc, true},
        {"extensionInitiatedBrowserFullscreen_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreen, Shortcut::kPressAndHoldEsc, true},
    }),
    [](const testing::TestParamInfo<
        ExclusiveAccessBubbleViewsInstructionTextTest::ParamType>& info) {
      return info.param.test_name;
    });

// Tests the creation of a has_download, non-overriding notification.
TEST_F(ExclusiveAccessBubbleViewsTest, CreateForDownload) {
  ExclusiveAccessBubbleParams params{.type = EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE,
                                     .has_download = true};
  bubble_view_ = std::make_unique<ExclusiveAccessBubbleViews>(
      browser_view(), params, base::NullCallback());
  EXPECT_TRUE(base::StartsWith(
      GetInstructionViewText(),
      CreateInstructionText(UserGoal::kSeeDownload, Shortcut::kPressEsc)));
}

// Tests the updating of a has_download, non-overriding notification with a
// second one of the same.
TEST_F(ExclusiveAccessBubbleViewsTest, CreateForDownloadUpdateForDownload) {
  ExclusiveAccessBubbleParams params{.type = EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE,
                                     .has_download = true};
  bubble_view_ = std::make_unique<ExclusiveAccessBubbleViews>(
      browser_view(), params, base::NullCallback());
  UpdateExclusiveAccessBubbleType(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE,
                                  /*has_download=*/true);
  EXPECT_TRUE(base::StartsWith(
      GetInstructionViewText(),
      CreateInstructionText(UserGoal::kSeeDownload, Shortcut::kPressEsc)));
}

// Tests updating the bubble first with the type, then a second time for a
// download notification.
using ExclusiveAccessBubbleViewsTestWithDownload =
    ExclusiveAccessBubbleViewsInstructionTextTest;
TEST_P(ExclusiveAccessBubbleViewsTestWithDownload,
       UpdateViewContentThenDownload) {
  const InstructionTextTestCase& test_case = GetParam();
  UpdateExclusiveAccessBubbleType(test_case.type, /*has_download=*/false);
  // Download notifications pass the type NONE.
  UpdateExclusiveAccessBubbleType(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE,
                                  /*has_download=*/true);
  EXPECT_EQ(GetInstructionViewText(),
            CreateInstructionText(test_case.goal, test_case.shortcut));
}

// Tests the above but with the Update() calls coming in the other order.
TEST_P(ExclusiveAccessBubbleViewsTestWithDownload,
       UpdateViewContentAfterDownload) {
  const InstructionTextTestCase& test_case = GetParam();
  // Update it for a download.
  UpdateExclusiveAccessBubbleType(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE,
                                  /*has_download=*/true);
  // A subsequent Update() call with the test-specified type.
  UpdateExclusiveAccessBubbleType(test_case.type, /*has_download=*/false);
  EXPECT_EQ(GetInstructionViewText(),
            CreateInstructionText(test_case.goal, test_case.shortcut));
}

INSTANTIATE_TEST_SUITE_P(
    /* no label */,
    ExclusiveAccessBubbleViewsTestWithDownload,
    testing::ValuesIn<InstructionTextTestCase>({
        {"tabFullscreenSeeDownload",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreenAndSeeDownload, Shortcut::kPressEsc, false},
        {"browserFullscreenSeeDownload",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreenAndSeeDownload, Shortcut::kAccelerator,
         false},
        {"extensionInitiatedFullscreenSeeDownload",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreenAndSeeDownload, Shortcut::kAccelerator,
         false},
        {"tabFullscreenSeeDownload_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreenAndSeeDownload, Shortcut::kPressEsc, true},
        {"browserFullscreenSeeDownload_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreenAndSeeDownload, Shortcut::kPressAndHoldEsc,
         true},
        {"extensionInitiatedFullscreenSeeDownload_EnablePressAndHoldEsc",
         EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION,
         UserGoal::kExitFullscreenAndSeeDownload, Shortcut::kPressAndHoldEsc,
         true},
    }),
    [](const testing::TestParamInfo<
        ExclusiveAccessBubbleViewsTestWithDownload::ParamType>& info) {
      return info.param.test_name;
    });