File: focus_mode_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 (615 lines) | stat: -rw-r--r-- 24,584 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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// 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 "ash/constants/ash_features.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/style/pill_button.h"
#include "ash/style/system_textfield.h"
#include "ash/system/focus_mode/focus_mode_controller.h"
#include "ash/system/focus_mode/focus_mode_detailed_view.h"
#include "ash/system/focus_mode/focus_mode_histogram_names.h"
#include "ash/system/focus_mode/focus_mode_tray.h"
#include "ash/system/focus_mode/focus_mode_util.h"
#include "ash/system/status_area_widget_test_helper.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "ash/system/unified/quick_settings_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_bubble.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_test_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ash/accessibility/spoken_feedback_browsertest.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/web_view/ash_web_view_impl.h"
#include "chrome/test/base/ash/util/ash_test_util.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "ui/views/controls/button/button_controller.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

constexpr char kFocusModeMediaWidgetName[] = "FocusModeMediaWidget";

views::Widget* FindMediaWidgetFromWindow(aura::Window* search_root) {
  if (views::Widget* const widget =
          views::Widget::GetWidgetForNativeWindow(search_root);
      widget && widget->GetName() == kFocusModeMediaWidgetName) {
    return widget;
  }

  // Keep searching in children.
  for (aura::Window* const child : search_root->children()) {
    if (views::Widget* const found = FindMediaWidgetFromWindow(child)) {
      return found;
    }
  }

  return nullptr;
}

views::Widget* FindMediaWidget() {
  return FindMediaWidgetFromWindow(Shell::GetContainer(
      Shell::GetPrimaryRootWindow(), kShellWindowId_OverlayContainer));
}

void SimulatePlaybackState(bool is_playing) {
  media_session::mojom::MediaSessionInfoPtr session_info(
      media_session::mojom::MediaSessionInfo::New());

  session_info->state =
      media_session::mojom::MediaSessionInfo::SessionState::kActive;
  session_info->playback_state =
      is_playing ? media_session::mojom::MediaPlaybackState::kPlaying
                 : media_session::mojom::MediaPlaybackState::kPaused;

  FocusModeController::Get()
      ->focus_mode_sounds_controller()
      ->MediaSessionInfoChanged(std::move(session_info));
}

// Dependent on `focus_gained`, the media with `id_observed` will either gain or
// lose audio focus.
void SimulateAudioFocusChange(bool focus_gained,
                              const base::UnguessableToken& id_observed) {
  media_session::mojom::AudioFocusRequestStatePtr focus(
      media_session::mojom::AudioFocusRequestState::New());
  auto* controller = FocusModeController::Get();
  focus->request_id = id_observed;

  auto* sounds_controller = controller->focus_mode_sounds_controller();
  if (focus_gained) {
    sounds_controller->OnFocusGained(std::move(focus));
    SimulatePlaybackState(/*is_playing=*/true);
  } else {
    sounds_controller->OnFocusLost(std::move(focus));
  }
}

// Simulate playing a newly selected playlist during an active session.
void SimulateStartPlaying() {
  auto* controller = FocusModeController::Get();
  controller->SetMediaSessionRequestIdForTesting(/*create_media_widget=*/true);
  SimulateAudioFocusChange(
      /*focus_gained=*/true,
      /*id_observed=*/controller->GetMediaSessionRequestId());
}

QuickSettingsView* OpenQuickSettings() {
  UnifiedSystemTray* system_tray = Shell::GetPrimaryRootWindowController()
                                       ->shelf()
                                       ->GetStatusAreaWidget()
                                       ->unified_system_tray();
  system_tray->ShowBubble();
  return system_tray->bubble()->quick_settings_view();
}

void ClickOnFocusTile(QuickSettingsView* panel) {
  views::Button* tile = views::AsViewClass<views::Button>(
      panel->GetViewByID(VIEW_ID_FEATURE_TILE_FOCUS_MODE));
  tile->button_controller()->NotifyClick();
  base::RunLoop().RunUntilIdle();
}

SystemTextfield* GetTimerTextfield(QuickSettingsView* quick_settings) {
  FocusModeDetailedView* detailed_view =
      quick_settings->GetDetailedViewForTest<FocusModeDetailedView>();
  EXPECT_TRUE(detailed_view);
  return views::AsViewClass<SystemTextfield>(detailed_view->GetViewByID(
      FocusModeDetailedView::ViewId::kTimerTextfield));
}

PillButton* GetToggleFocusButton(QuickSettingsView* quick_settings) {
  FocusModeDetailedView* detailed_view =
      quick_settings->GetDetailedViewForTest<FocusModeDetailedView>();
  EXPECT_TRUE(detailed_view);
  return views::AsViewClass<PillButton>(detailed_view->GetViewByID(
      FocusModeDetailedView::ViewId::kToggleFocusButton));
}

}  // namespace

class FocusModeBrowserTest : public InProcessBrowserTest {
 public:
  FocusModeBrowserTest() = default;
  ~FocusModeBrowserTest() override = default;
  FocusModeBrowserTest(const FocusModeBrowserTest&) = delete;
  FocusModeBrowserTest& operator=(const FocusModeBrowserTest&) = delete;

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    FocusModeController::Get()
        ->focus_mode_sounds_controller()
        ->SetIsMinorUserForTesting(false);
  }
};

// Tests basic create/close media widget functionality.
IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest, MediaWidget) {
  auto* controller = FocusModeController::Get();
  EXPECT_FALSE(controller->in_focus_session());

  // Toggle on focus mode. Verify that there is no media widget since there is
  // no selected playlist.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  auto* sounds_controller = controller->focus_mode_sounds_controller();
  EXPECT_TRUE(sounds_controller->selected_playlist().empty());
  EXPECT_FALSE(FindMediaWidget());

  // Select a playlist with a type and verify that a media widget is created.
  focus_mode_util::SelectedPlaylist selected_playlist;
  selected_playlist.id = "id0";
  selected_playlist.type = focus_mode_util::SoundType::kSoundscape;
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());
  EXPECT_TRUE(FindMediaWidget());

  // Swap playlists, then verify that the media widget still exists.
  selected_playlist.id = "id1";
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());
  EXPECT_TRUE(FindMediaWidget());

  // The media widget should still exist when the ending moment is triggered.
  controller->TriggerEndingMomentImmediately();
  EXPECT_TRUE(controller->in_ending_moment());
  EXPECT_TRUE(FindMediaWidget());

  // If the user extends the time during the ending moment, the media widget
  // should be recreated.
  controller->ExtendSessionDuration();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_TRUE(FindMediaWidget());

  // Toggling off focus mode should close the media widget.
  controller->ToggleFocusMode();
  EXPECT_FALSE(controller->in_focus_session());
  EXPECT_FALSE(FindMediaWidget());

  // Toggling on focus mode with a selected playlist should trigger creating a
  // media widget.
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_TRUE(FindMediaWidget());
}

// Tests that the ending moment will pause the playlist without closing the
// media widget.
IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest, PauseMusicDuringEndingMoment) {
  auto* controller = FocusModeController::Get();
  EXPECT_FALSE(controller->in_focus_session());

  // Toggle on focus mode. Verify that there is no media widget since there is
  // no selected playlist.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  auto* sounds_controller = controller->focus_mode_sounds_controller();
  sounds_controller->set_simulate_playback_for_testing();

  // Case 1. If the music is paused by the ending moment, when extending the
  // session, it should be resumed. Select a playlist with a type and verify
  // that a media widget is created.
  focus_mode_util::SelectedPlaylist selected_playlist;
  selected_playlist.id = "id0";
  selected_playlist.type = focus_mode_util::SoundType::kSoundscape;
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_TRUE(FindMediaWidget());
  // Simulate the playlist is playing.
  SimulatePlaybackState(/*is_playing=*/true);
  EXPECT_EQ(focus_mode_util::SoundState::kPlaying,
            sounds_controller->selected_playlist().state);

  controller->TriggerEndingMomentImmediately();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_ending_moment());
  EXPECT_TRUE(FindMediaWidget());
  EXPECT_EQ(focus_mode_util::SoundState::kPaused,
            sounds_controller->selected_playlist().state);

  // Extending the session will resume the playlist.
  controller->ExtendSessionDuration();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_EQ(focus_mode_util::SoundState::kPlaying,
            sounds_controller->selected_playlist().state);

  // Case 2. If the user paused the playlist before ending moment, after
  // extending the session, the playlist should still in paused state.
  SimulatePlaybackState(/*is_playing=*/false);
  EXPECT_EQ(focus_mode_util::SoundState::kPaused,
            sounds_controller->selected_playlist().state);

  controller->TriggerEndingMomentImmediately();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_ending_moment());
  EXPECT_TRUE(FindMediaWidget());

  controller->ExtendSessionDuration();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_EQ(focus_mode_util::SoundState::kPaused,
            sounds_controller->selected_playlist().state);

  // Case 3. If the user selected another playlist during the ending moment,
  // after extending the session, it should be the new playlist is playing.
  controller->TriggerEndingMomentImmediately();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_ending_moment());
  const auto old_playlist_id = sounds_controller->selected_playlist().id;

  selected_playlist.id = "id1";
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_EQ(focus_mode_util::SoundState::kSelected,
            sounds_controller->selected_playlist().state);

  controller->ExtendSessionDuration();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_TRUE(FindMediaWidget());
  EXPECT_NE(old_playlist_id, sounds_controller->selected_playlist().id);
}

// Tests that the ending moment will pause the playlist even if it is not the
// initial track. This is because when the next track is played, it loses and
// regains focus, which means that the media controller is reset.
// Regression test for crbug.com/380173752
IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest, PauseNextTrackDuringEndingMoment) {
  auto* controller = FocusModeController::Get();
  EXPECT_FALSE(controller->in_focus_session());

  // Toggle on focus mode.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  auto* sounds_controller = controller->focus_mode_sounds_controller();
  sounds_controller->set_simulate_playback_for_testing();

  // Select a playlist with a type and verify that a media widget is created.
  focus_mode_util::SelectedPlaylist selected_playlist;
  selected_playlist.id = "id0";
  selected_playlist.type = focus_mode_util::SoundType::kSoundscape;
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_TRUE(FindMediaWidget());

  // Simulate the playlist is playing.
  SimulateStartPlaying();
  EXPECT_EQ(focus_mode_util::SoundState::kPlaying,
            sounds_controller->selected_playlist().state);

  // Simulate going to the next track, which includes audio focus changes.
  SimulateAudioFocusChange(
      /*focus_gained=*/false,
      /*id_observed=*/controller->GetMediaSessionRequestId());
  // Verify that we have lost audio focus.
  EXPECT_FALSE(sounds_controller->has_audio_focus_for_testing());
  SimulateAudioFocusChange(
      /*focus_gained=*/true,
      /*id_observed=*/controller->GetMediaSessionRequestId());
  // Verify that we have gained audio focus.
  EXPECT_TRUE(sounds_controller->has_audio_focus_for_testing());

  // Triggering the ending moment should pause the playlist.
  controller->TriggerEndingMomentImmediately();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_ending_moment());
  EXPECT_TRUE(FindMediaWidget());
  EXPECT_EQ(focus_mode_util::SoundState::kPaused,
            sounds_controller->selected_playlist().state);

  // Extending the session will resume the playlist.
  controller->ExtendSessionDuration();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_EQ(focus_mode_util::SoundState::kPlaying,
            sounds_controller->selected_playlist().state);
}

IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest,
                       CheckSoundsPlayedDuringSessionHistogram) {
  base::HistogramTester histogram_tester;

  auto* controller = FocusModeController::Get();
  auto* sounds_controller = controller->focus_mode_sounds_controller();

  // 1. No playlist playing during the session.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_TRUE(sounds_controller->selected_playlist().empty());

  controller->ToggleFocusMode();
  EXPECT_FALSE(controller->in_focus_session());
  histogram_tester.ExpectBucketCount(
      /*name=*/focus_mode_histogram_names::kPlaylistTypesSelectedDuringSession,
      /*sample=*/
      focus_mode_histogram_names::PlaylistTypesSelectedDuringFocusSessionType::
          kNone,
      /*expected_count=*/1);

  // 2. Only the type of soundscape playlist playing during the session.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());

  focus_mode_util::SelectedPlaylist selected_playlist;
  selected_playlist.id = "id0";
  selected_playlist.type = focus_mode_util::SoundType::kSoundscape;
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());
  EXPECT_TRUE(FindMediaWidget());

  controller->ToggleFocusMode();
  EXPECT_FALSE(controller->in_focus_session());
  histogram_tester.ExpectBucketCount(
      /*name=*/focus_mode_histogram_names::kPlaylistTypesSelectedDuringSession,
      /*sample=*/
      focus_mode_histogram_names::PlaylistTypesSelectedDuringFocusSessionType::
          kSoundscapes,
      /*expected_count=*/1);

  // 3. Only the type of YouTube Music playlist playing during the session.
  selected_playlist.id = "id1";
  selected_playlist.type = focus_mode_util::SoundType::kYouTubeMusic;
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());

  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());

  controller->ToggleFocusMode();
  EXPECT_FALSE(controller->in_focus_session());
  histogram_tester.ExpectBucketCount(
      /*name=*/focus_mode_histogram_names::kPlaylistTypesSelectedDuringSession,
      /*sample=*/
      focus_mode_histogram_names::PlaylistTypesSelectedDuringFocusSessionType::
          kYouTubeMusic,
      /*expected_count=*/1);

  // 4. The two types of playlists playing during the session.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());

  selected_playlist.id = "id3";
  selected_playlist.type = focus_mode_util::SoundType::kSoundscape;
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_EQ(sounds_controller->selected_playlist().type,
            focus_mode_util::SoundType::kSoundscape);

  controller->ToggleFocusMode();
  EXPECT_FALSE(controller->in_focus_session());
  histogram_tester.ExpectBucketCount(
      /*name=*/focus_mode_histogram_names::kPlaylistTypesSelectedDuringSession,
      /*sample=*/
      focus_mode_histogram_names::PlaylistTypesSelectedDuringFocusSessionType::
          kYouTubeMusicAndSoundscapes,
      /*expected_count=*/1);
}

IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest,
                       CheckPlaylistsPlayedDuringSessionHistogram) {
  base::HistogramTester histogram_tester;

  auto* controller = FocusModeController::Get();
  auto* sounds_controller = controller->focus_mode_sounds_controller();

  // 1. No playlist played during the session.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_TRUE(sounds_controller->selected_playlist().empty());

  controller->ToggleFocusMode();
  EXPECT_FALSE(controller->in_focus_session());
  histogram_tester.ExpectBucketCount(
      /*name=*/focus_mode_histogram_names::kCountPlaylistsPlayedDuringSession,
      /*sample=*/0, /*expected_count=*/1);

  // 2. Two playlists played during the session.
  focus_mode_util::SelectedPlaylist selected_playlist;
  selected_playlist.id = "id0";
  selected_playlist.type = focus_mode_util::SoundType::kYouTubeMusic;
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());

  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());

  selected_playlist.id = "id1";
  selected_playlist.type = focus_mode_util::SoundType::kSoundscape;
  sounds_controller->TogglePlaylist(selected_playlist);

  // De-select the playlist and the histogram will not record it.
  sounds_controller->TogglePlaylist(sounds_controller->selected_playlist());

  controller->ToggleFocusMode();
  EXPECT_FALSE(controller->in_focus_session());
  histogram_tester.ExpectBucketCount(
      /*name=*/focus_mode_histogram_names::kCountPlaylistsPlayedDuringSession,
      /*sample=*/2, /*expected_count=*/1);
}

// Tests that the source title shown in the media controls for the associated
// Focus Mode media widget is overridden and not empty.
IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest, MediaSourceTitle) {
  // Toggle on focus mode.
  auto* focus_mode_controller = FocusModeController::Get();
  focus_mode_controller->ToggleFocusMode();
  EXPECT_TRUE(focus_mode_controller->in_focus_session());

  // Select a playlist and verify that a media widget is created.
  focus_mode_util::SelectedPlaylist selected_playlist;
  selected_playlist.id = "id0";
  selected_playlist.title = "Playlist Title";
  selected_playlist.type = focus_mode_util::SoundType::kYouTubeMusic;
  auto* sounds_controller =
      focus_mode_controller->focus_mode_sounds_controller();
  sounds_controller->TogglePlaylist(selected_playlist);
  EXPECT_FALSE(sounds_controller->selected_playlist().empty());

  auto* widget = FindMediaWidget();
  EXPECT_TRUE(widget);

  // Verify that there is a source title.
  AshWebViewImpl* web_view_impl =
      static_cast<AshWebViewImpl*>(widget->GetContentsView());
  std::string source_title =
      web_view_impl->GetTitleForMediaControls(web_view_impl->web_contents());
  EXPECT_FALSE(source_title.empty());
}

// Tests that during the overvide mode, clicking on the focus panel will not end
// the overview mode.
IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest, ClickOnFocusPanelInOverviewMode) {
  // Enter overview mode and open the focus panel.
  ToggleOverview();
  WaitForOverviewEnterAnimation();

  auto* quick_settings = OpenQuickSettings();
  ClickOnFocusTile(quick_settings);
  EXPECT_TRUE(ash::OverviewController::Get()->InOverviewSession());

  // 1. Click the timer textfield on the focus panel and stay in overview mode.
  auto* timer_textfield = GetTimerTextfield(quick_settings);
  EXPECT_TRUE(timer_textfield->GetVisible());

  test::Click(timer_textfield);
  EXPECT_TRUE(timer_textfield->HasFocus());
  EXPECT_TRUE(ash::OverviewController::Get()->InOverviewSession());

  // 2. Click the `Start` button to start a focus session and stay in overview
  // mode.
  auto* toggle_button = GetToggleFocusButton(quick_settings);
  EXPECT_TRUE(toggle_button->GetVisible());

  auto* focus_mode_controller = ash::FocusModeController::Get();
  EXPECT_FALSE(focus_mode_controller->in_focus_session());
  test::Click(toggle_button);
  EXPECT_TRUE(focus_mode_controller->in_focus_session());
  EXPECT_TRUE(ash::OverviewController::Get()->InOverviewSession());
}

// Tests that clicking the ending moment nudge will show the focus tray bubble.
// Regression test for crbug.com/384586824
IN_PROC_BROWSER_TEST_F(FocusModeBrowserTest, EndingMomentNudgeClick) {
  FocusModeTray* focus_mode_tray =
      StatusAreaWidgetTestHelper::GetStatusAreaWidget()->focus_mode_tray();
  auto* controller = FocusModeController::Get();
  EXPECT_FALSE(controller->in_focus_session());

  // Open a browser window.
  CreateBrowser(ProfileManager::GetActiveUserProfile());

  // Start a focus session.
  controller->ToggleFocusMode();
  EXPECT_TRUE(controller->in_focus_session());
  EXPECT_TRUE(focus_mode_tray->GetVisible());

  // Trigger the ending moment and verify that the tray icon is still visible,
  // even though the focus session has ended.
  controller->TriggerEndingMomentImmediately();
  EXPECT_FALSE(controller->in_focus_session());
  EXPECT_TRUE(controller->in_ending_moment());
  EXPECT_TRUE(focus_mode_tray->GetVisible());
  EXPECT_FALSE(focus_mode_tray->GetBubbleView());

  // Force the ending moment nudge to show immediately since normally there is a
  // delay. Verify that it is showing.
  controller->MaybeShowEndingMomentNudge();
  EXPECT_TRUE(AnchoredNudgeManager::Get()->IsNudgeShown(
      focus_mode_util::kFocusModeEndingMomentNudgeId));

  // Simulate a "click" on the nudge.
  focus_mode_tray->ShowBubble();

  // Verify that the focus mode tray bubble is showing and we are still in the
  // focus session ending moment.
  EXPECT_TRUE(focus_mode_tray->GetBubbleView());
  EXPECT_TRUE(focus_mode_tray->GetVisible());
  EXPECT_TRUE(controller->in_ending_moment());
}

class FocusModeSpokenFeedbackTest : public LoggedInSpokenFeedbackTest {
 public:
  FocusModeSpokenFeedbackTest() = default;
  FocusModeSpokenFeedbackTest(const FocusModeSpokenFeedbackTest&) = delete;
  FocusModeSpokenFeedbackTest& operator=(const FocusModeSpokenFeedbackTest&) =
      delete;
  ~FocusModeSpokenFeedbackTest() override = default;
};

INSTANTIATE_TEST_SUITE_P(
    ManifestV2,
    FocusModeSpokenFeedbackTest,
    ::testing::Values(SpokenFeedbackTestConfig(ManifestVersion::kTwo)));

// Tests that when using `Search + Left/Right Arrow` key to navigate on the
// focus panel, the user update the timer texfield and start a focus session,
// which should also update the session duration for the controller.
IN_PROC_BROWSER_TEST_P(FocusModeSpokenFeedbackTest,
                       AfterA11yFocusRingOnTimerTextfield) {
  chromevox_test_utils()->EnableChromeVox();

  // Set a session duration with 25 min and let the timer textfield gain the
  // focus.
  sm()->Call([] {
    auto* focus_mode_controller = FocusModeController::Get();
    focus_mode_controller->SetInactiveSessionDuration(base::Minutes(25));
    EXPECT_EQ(base::Minutes(25), focus_mode_controller->session_duration());

    // Open the focus panel.
    auto* quick_settings = OpenQuickSettings();
    ClickOnFocusTile(quick_settings);
    auto* timer_textfield = GetTimerTextfield(quick_settings);
    timer_textfield->RequestFocus();
  });
  sm()->ExpectSpeechPattern("Edit timer*");

  // Update the session duration from 25 min to 250 min by appending a `0` key
  // to the end of the text.
  sm()->Call([this] { SendKeyPress(ui::VKEY_0); });

  // Press `Search + Left Arrow` keys to the `Start Focus` button..
  sm()->Call([this] {
    SendKeyPressWithSearch(ui::VKEY_LEFT);
    SendKeyPressWithSearch(ui::VKEY_LEFT);
  });
  sm()->ExpectSpeechPattern("Start Focus*");

  // Press `Enter` key to start a focus session and Verify the session
  // duration..
  sm()->Call([this] {
    SendKeyPress(ui::VKEY_RETURN);
    EXPECT_EQ(base::Minutes(250),
              FocusModeController::Get()->session_duration());
  });
  sm()->Replay();
}

}  // namespace ash