File: live_caption_controller_browsertest.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 (452 lines) | stat: -rw-r--r-- 16,677 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/live_caption/live_caption_controller.h"

#include <ranges>

#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "build/build_config.h"
#include "chrome/browser/accessibility/caption_bubble_context_browser.h"
#include "chrome/browser/accessibility/live_caption/live_caption_controller_factory.h"
#include "chrome/browser/accessibility/live_caption/live_caption_test_util.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/live_caption/caption_bubble_controller.h"
#include "components/live_caption/pref_names.h"
#include "components/soda/constants.h"
#include "components/soda/pref_names.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "content/public/test/browser_test.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#endif

namespace captions {

namespace {

speech::LanguageCode en_us() {
  return speech::LanguageCode::kEnUs;
}

speech::LanguageCode fr_fr() {
  return speech::LanguageCode::kFrFr;
}

}  // namespace

Profile* CreateProfile() {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  base::FilePath profile_path =
      profile_manager->GenerateNextProfileDirectoryPath();
  return &profiles::testing::CreateProfileSync(profile_manager, profile_path);
}

class LiveCaptionControllerTest : public LiveCaptionBrowserTest {
 public:
  LiveCaptionControllerTest() = default;
  ~LiveCaptionControllerTest() override = default;
  LiveCaptionControllerTest(const LiveCaptionControllerTest&) = delete;
  LiveCaptionControllerTest& operator=(const LiveCaptionControllerTest&) =
      delete;

  LiveCaptionController* GetControllerForProfile(Profile* profile) {
    return LiveCaptionControllerFactory::GetForProfile(profile);
  }

  CaptionBubbleController* GetBubbleController() {
    return GetBubbleControllerForProfile(browser()->profile());
  }

  CaptionBubbleController* GetBubbleControllerForProfile(Profile* profile) {
    return GetControllerForProfile(profile)
        ->caption_bubble_controller_for_testing();
  }

  CaptionBubbleContextBrowser* GetCaptionBubbleContextBrowser() {
    if (!caption_bubble_context_) {
      caption_bubble_context_ = CaptionBubbleContextBrowser::Create(
          browser()->tab_strip_model()->GetActiveWebContents());
    }
    return caption_bubble_context_.get();
  }

  bool DispatchTranscription(std::string text) {
    return DispatchTranscriptionToProfile(text, browser()->profile());
  }

  bool DispatchTranscriptionToProfile(std::string text, Profile* profile) {
    return GetControllerForProfile(profile)->DispatchTranscription(
        browser()->tab_strip_model()->GetActiveWebContents(),
        GetCaptionBubbleContextBrowser(),
        media::SpeechRecognitionResult(text, /* is_final */ false));
  }

  void OnError() { OnErrorOnProfile(browser()->profile()); }

  void OnErrorOnProfile(Profile* profile) {
    GetControllerForProfile(profile)->OnError(
        GetCaptionBubbleContextBrowser(), CaptionBubbleErrorType::kGeneric,
        base::RepeatingClosure(),
        base::BindRepeating(
            [](CaptionBubbleErrorType error_type, bool checked) {}));
  }

  void OnAudioStreamEnd() { OnAudioStreamEndOnProfile(browser()->profile()); }

  void OnAudioStreamEndOnProfile(Profile* profile) {
    GetControllerForProfile(profile)->OnAudioStreamEnd(
        browser()->tab_strip_model()->GetActiveWebContents(),
        GetCaptionBubbleContextBrowser());
  }

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS)
  void OnToggleFullscreen() {
    GetControllerForProfile(browser()->profile())
        ->OnToggleFullscreen(GetCaptionBubbleContextBrowser());
  }
#endif

  bool HasBubbleController() {
    return HasBubbleControllerOnProfile(browser()->profile());
  }

  bool HasBubbleControllerOnProfile(Profile* profile) {
    return GetControllerForProfile(profile)
               ->caption_bubble_controller_for_testing() != nullptr;
  }

  void ExpectIsWidgetVisible(bool visible) {
    ExpectIsWidgetVisibleOnProfile(visible, browser()->profile());
  }

  void ExpectIsWidgetVisibleOnProfile(bool visible, Profile* profile) {
#if defined(TOOLKIT_VIEWS)
    EXPECT_EQ(
        visible,
        GetBubbleControllerForProfile(profile)->IsWidgetVisibleForTesting());
#endif
  }

  void ExpectBubbleLabelTextEquals(std::string text) {
    ExpectBubbleLabelTextOnProfileEquals(text, browser()->profile());
  }

  void ExpectBubbleLabelTextOnProfileEquals(std::string text,
                                            Profile* profile) {
#if defined(TOOLKIT_VIEWS)
    EXPECT_EQ(
        text,
        GetBubbleControllerForProfile(profile)->GetBubbleLabelTextForTesting());
#endif
  }

 private:
  std::unique_ptr<CaptionBubbleContextBrowser> caption_bubble_context_;
};

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, ProfilePrefsAreRegistered) {
  EXPECT_FALSE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));

#if !BUILDFLAG(IS_CHROMEOS)
  // These prefs are used for the component updater, but SODA does not use the
  // component updater on Chrome OS.
  EXPECT_EQ(base::FilePath(), g_browser_process->local_state()->GetFilePath(
                                  prefs::kSodaBinaryPath));
  EXPECT_EQ(base::FilePath(), g_browser_process->local_state()->GetFilePath(
                                  prefs::kSodaEnUsConfigPath));
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest,
                       ProfilePrefsAreRegistered_Incognito) {
  // Set live caption enabled on the regular profile.
  SetLiveCaptionEnabled(true);
  EXPECT_TRUE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
#if !BUILDFLAG(IS_CHROMEOS)
  // These prefs are used for the component updater, but SODA does not use the
  // component updater on Chrome OS.
  EXPECT_EQ(base::FilePath(), g_browser_process->local_state()->GetFilePath(
                                  prefs::kSodaBinaryPath));
  EXPECT_EQ(base::FilePath(), g_browser_process->local_state()->GetFilePath(
                                  prefs::kSodaEnUsConfigPath));
#endif  // !BUILDFLAG(IS_CHROMEOS)

  // Ensure that live caption is also enabled in the incognito profile.
  Profile* incognito_profile =
      browser()->profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  EXPECT_TRUE(
      incognito_profile->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
#if !BUILDFLAG(IS_CHROMEOS)
  // These prefs are used for the component updater, but SODA does not use the
  // component updater on Chrome OS.
  EXPECT_EQ(base::FilePath(), g_browser_process->local_state()->GetFilePath(
                                  prefs::kSodaBinaryPath));
  EXPECT_EQ(base::FilePath(), g_browser_process->local_state()->GetFilePath(
                                  prefs::kSodaEnUsConfigPath));
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, LiveCaptionEnabledChanged) {
  EXPECT_EQ(nullptr, GetBubbleController());
  EXPECT_FALSE(HasBubbleController());

  SetLiveCaptionEnabled(true);
  EXPECT_NE(nullptr, GetBubbleController());
  EXPECT_TRUE(HasBubbleController());

  SetLiveCaptionEnabled(false);
  EXPECT_EQ(nullptr, GetBubbleController());
  EXPECT_FALSE(HasBubbleController());
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest,
                       LiveCaptionEnabledChanged_BubbleVisible) {
  SetLiveCaptionEnabled(true);
  // Make the bubble visible by dispatching a transcription.
  DispatchTranscription(
      "In Switzerland it is illegal to own just one guinea pig.");
  ExpectIsWidgetVisible(true);

  SetLiveCaptionEnabled(false);
  EXPECT_EQ(nullptr, GetBubbleController());
  EXPECT_FALSE(HasBubbleController());
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, OnSodaInstalled) {
  EXPECT_FALSE(HasBubbleController());
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kLiveCaptionEnabled,
                                               true);
  EXPECT_FALSE(HasBubbleController());

  // The UI is only created after SODA is installed.
  speech::SodaInstaller::GetInstance()->NotifySodaInstalledForTesting(en_us());
  speech::SodaInstaller::GetInstance()->NotifySodaInstalledForTesting();
  EXPECT_TRUE(HasBubbleController());
}

// TODO(crbug.com/40936746): Re-enable this test.
IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, DISABLED_OnSodaError) {
  // Live Caption is disabled when there is an error in the SODA download for
  // the language belonging to Live Caption.
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kLiveCaptionEnabled,
                                               true);
  EXPECT_TRUE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
  speech::SodaInstaller::GetInstance()->NotifySodaErrorForTesting(en_us());
  EXPECT_FALSE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));

  // Live Caption is disabled when there is an error in the SODA binary
  // download.
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kLiveCaptionEnabled,
                                               true);
  EXPECT_TRUE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
  speech::SodaInstaller::GetInstance()->NotifySodaErrorForTesting();
  EXPECT_FALSE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));

  // Live Caption is not disabled when there is an error in the SODA download
  // for a language not belonging to Live Caption.
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kLiveCaptionEnabled,
                                               true);
  EXPECT_TRUE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
  speech::SodaInstaller::GetInstance()->NotifySodaErrorForTesting(fr_fr());
  EXPECT_TRUE(
      browser()->profile()->GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, DispatchTranscription) {
  bool success = DispatchTranscription("A baby spider is called a spiderling.");
  EXPECT_FALSE(success);
  EXPECT_FALSE(HasBubbleController());

  SetLiveCaptionEnabled(true);
  success = DispatchTranscription(
      "A baby octopus is about the size of a flea when it is born.");
  EXPECT_TRUE(success);
  ExpectIsWidgetVisible(true);
  ExpectBubbleLabelTextEquals(
      "A baby octopus is about the size of a flea when it is born.");

  SetLiveCaptionEnabled(false);
  success = DispatchTranscription(
      "Approximately 10-20% of power outages in the US are caused by "
      "squirrels.");
  EXPECT_FALSE(success);
  EXPECT_FALSE(HasBubbleController());
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, OnError) {
  EXPECT_FALSE(HasBubbleController());
  OnError();
  EXPECT_TRUE(HasBubbleController());

  SetLiveCaptionEnabled(true);
  OnError();
  ExpectIsWidgetVisible(true);

  SetLiveCaptionEnabled(false);
  OnError();
  EXPECT_TRUE(HasBubbleController());
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, OnAudioStreamEnd) {
  OnAudioStreamEnd();
  EXPECT_FALSE(HasBubbleController());

  SetLiveCaptionEnabled(true);
  DispatchTranscription("Some cicadas appear only once every 17 years.");
  ExpectIsWidgetVisible(true);

  OnAudioStreamEnd();
  ExpectIsWidgetVisible(false);

  SetLiveCaptionEnabled(false);
  OnAudioStreamEnd();
  EXPECT_FALSE(HasBubbleController());
}

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, OnToggleFullscreen) {
  OnToggleFullscreen();
  EXPECT_FALSE(HasBubbleController());

  SetLiveCaptionEnabled(true);
  EXPECT_TRUE(HasBubbleController());

  OnToggleFullscreen();
  EXPECT_TRUE(HasBubbleController());

  SetLiveCaptionEnabled(false);
  OnToggleFullscreen();
  EXPECT_FALSE(HasBubbleController());
}
#endif

#if !BUILDFLAG(IS_CHROMEOS)  // No multi-profile on ChromeOS.

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest,
                       LiveCaptionEnabledChanged_MultipleProfiles) {
  Profile* profile1 = browser()->profile();
  Profile* profile2 = CreateProfile();

  // The profiles start with no caption bubble controllers.
  EXPECT_FALSE(HasBubbleControllerOnProfile(profile1));
  EXPECT_FALSE(HasBubbleControllerOnProfile(profile2));

  // Enable live caption on profile1.
  SetLiveCaptionEnabled(true);
  EXPECT_TRUE(HasBubbleControllerOnProfile(profile1));
  EXPECT_FALSE(HasBubbleControllerOnProfile(profile2));

  // Enable live caption on profile2.
  SetLiveCaptionEnabledOnProfile(true, profile2);
  EXPECT_TRUE(HasBubbleControllerOnProfile(profile1));
  EXPECT_TRUE(HasBubbleControllerOnProfile(profile2));

  // Disable live caption on profile1.
  SetLiveCaptionEnabled(false);
  EXPECT_FALSE(HasBubbleControllerOnProfile(profile1));
  EXPECT_TRUE(HasBubbleControllerOnProfile(profile2));

  // Disable live caption on profile2.
  SetLiveCaptionEnabledOnProfile(false, profile2);
  EXPECT_FALSE(HasBubbleControllerOnProfile(profile1));
  EXPECT_FALSE(HasBubbleControllerOnProfile(profile2));
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest,
                       DispatchTranscription_MultipleProfiles) {
  Profile* profile1 = browser()->profile();
  Profile* profile2 = CreateProfile();

  // Enable live caption on both profiles.
  SetLiveCaptionEnabled(true);
  SetLiveCaptionEnabledOnProfile(true, profile2);

  // Dispatch transcription routes the transcription to the right profile.
  bool success =
      DispatchTranscriptionToProfile("Only female mosquitos bite.", profile1);
  EXPECT_TRUE(success);
  ExpectIsWidgetVisibleOnProfile(true, profile1);
  ExpectIsWidgetVisibleOnProfile(false, profile2);
  ExpectBubbleLabelTextOnProfileEquals("Only female mosquitos bite.", profile1);
  ExpectBubbleLabelTextOnProfileEquals("", profile2);

  success = DispatchTranscriptionToProfile(
      "Mosquitos were around at the time of the dinosaurs.", profile2);
  EXPECT_TRUE(success);
  ExpectIsWidgetVisibleOnProfile(true, profile1);
  ExpectIsWidgetVisibleOnProfile(true, profile2);
  ExpectBubbleLabelTextOnProfileEquals("Only female mosquitos bite.", profile1);
  ExpectBubbleLabelTextOnProfileEquals(
      "Mosquitos were around at the time of the dinosaurs.", profile2);
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest, OnError_MultipleProfiles) {
  Profile* profile1 = browser()->profile();
  Profile* profile2 = CreateProfile();

  // Enable live caption on both profiles.
  SetLiveCaptionEnabled(true);
  SetLiveCaptionEnabledOnProfile(true, profile2);

  // OnError routes to the right profile.
  OnErrorOnProfile(profile1);
  ExpectIsWidgetVisibleOnProfile(true, profile1);
  ExpectIsWidgetVisibleOnProfile(false, profile2);

  OnErrorOnProfile(profile2);
  ExpectIsWidgetVisibleOnProfile(true, profile1);
  ExpectIsWidgetVisibleOnProfile(true, profile2);
}

IN_PROC_BROWSER_TEST_F(LiveCaptionControllerTest,
                       OnAudioStreamEnd_MultipleProfiles) {
  Profile* profile1 = browser()->profile();
  Profile* profile2 = CreateProfile();

  // Enable live caption on both profiles.
  SetLiveCaptionEnabled(true);
  SetLiveCaptionEnabledOnProfile(true, profile2);

  DispatchTranscriptionToProfile(
      "Capybaras are the largest rodents in the world.", profile1);
  DispatchTranscriptionToProfile("Capybaras' teeth grow continuously.",
                                 profile2);
  ExpectIsWidgetVisibleOnProfile(true, profile1);
  ExpectIsWidgetVisibleOnProfile(true, profile2);

  // OnAudioStreamEnd routes to the right profile.
  OnAudioStreamEndOnProfile(profile1);
  ExpectIsWidgetVisibleOnProfile(false, profile1);
  ExpectIsWidgetVisibleOnProfile(true, profile2);

  OnAudioStreamEndOnProfile(profile2);
  ExpectIsWidgetVisibleOnProfile(false, profile1);
  ExpectIsWidgetVisibleOnProfile(false, profile2);
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace captions