File: lens_overlay_live_test.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 (760 lines) | stat: -rw-r--r-- 30,890 bytes parent folder | download | duplicates (5)
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
// 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 <optional>

#include "base/feature_list.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/statistics_recorder.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/run_until.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_timeouts.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/e2e_tests/account_capabilities_observer.h"
#include "chrome/browser/signin/e2e_tests/accounts_removed_waiter.h"
#include "chrome/browser/signin/e2e_tests/live_test.h"
#include "chrome/browser/signin/e2e_tests/sign_in_test_observer.h"
#include "chrome/browser/signin/e2e_tests/signin_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/lens/lens_overlay_controller.h"
#include "chrome/browser/ui/lens/lens_overlay_side_panel_coordinator.h"
#include "chrome/browser/ui/lens/lens_search_controller.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h"
#include "chrome/browser/ui/views/side_panel/side_panel_enums.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/lens/lens_features.h"
#include "components/lens/lens_overlay_invocation_source.h"
#include "components/lens/lens_overlay_permission_utils.h"
#include "components/signin/core/browser/account_reconcilor.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/signin/public/identity_manager/test_accounts.h"
#include "components/sync/service/sync_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/compositor/compositor_switches.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"

namespace lens {

namespace {
using State = LensOverlayController::State;
using LensOverlayInvocationSource = lens::LensOverlayInvocationSource;

constexpr char kResultsSearchBaseUrl[] = "https://www.google.com/search";

constexpr char kDivWordClass[] = "word";
constexpr char kDivObjectClass[] = "object";
constexpr char kDivTranslatedLineClass[] = "translated-line";

constexpr char kTranslateEnableButtonID[] = "translateEnableButton";

// Helper script to verify that the overlay WebUI has rendered divs with the CSS
// class provided.
constexpr char kFindAndClickDivWithClassScript[] = R"(
      function findAndClickDivWithClass(parentElement) {
        const div = parentElement.querySelector('div.' + $1);
        if (div) {
            const rect = div.getBoundingClientRect();
            const centerX = rect.left + rect.width / 2;
            const centerY = rect.top + rect.height / 2;

            div.dispatchEvent(new PointerEvent('pointerdown', {
              pointerId: 1,
              button: 0,
              clientX: centerX,
              clientY: centerY,
              isPrimary: true,
              bubbles: true,
              composed: true
            }));
            div.dispatchEvent(new PointerEvent('pointerup', {
              pointerId: 1,
              button: 0,
              clientX: centerX,
              clientY: centerY,
              isPrimary: true,
              bubbles: true,
              composed: true
            }));
            return true;
        }
        for (const child of parentElement.children) {
            if (findAndClickDivWithClass(child) ||
                (child.shadowRoot &&
                    findAndClickDivWithClass(child.shadowRoot))) {
                return true;
            }
        }
        return false;
      }
      findAndClickDivWithClass(document.body);
)";

// Helper script to fetch an element with a certain ID and click on it.
constexpr char kFindAndClickElementWithIDScript[] = R"(
  function findAndClickElementWithID(root, id) {
    const nodesToVisit = [root];
    while (nodesToVisit.length > 0) {
      const currentNode = nodesToVisit.shift();
      if (currentNode instanceof ShadowRoot) {
        const element = currentNode.getElementById(id);
        if (element) {
          element.click();
          return true;
        }
      }
      // Add all children (including those in shadowRoots) to the queue.
      for (const child of currentNode.children) {
        nodesToVisit.push(child);
        if (child.shadowRoot) {
          nodesToVisit.push(child.shadowRoot);
        }
      }
    }
    return false;
  }
  findAndClickElementWithID(document, $1);
)";

const char kNpsUrl[] = "https://www.nps.gov/articles/route-66-overview.htm";
const char kNpsObjectUrl[] =
    "https://www.nps.gov/common/commonspot/templates/images/graphics/404/"
    "04.jpg";
const char kNpsTranslateUrl[] =
    "https://www.nps.gov/subjects/historicpreservationfund/en-espanol.htm";
}  // namespace

// Live tests for Lens Overlay.
// These tests can be run with:
// browser_tests --gtest_filter=LensOverlayLiveTest.* --run-live-tests
class LensOverlayLiveTest : public signin::test::LiveTest {
 public:
  LensOverlayLiveTest() = default;
  ~LensOverlayLiveTest() override = default;

  void SetUp() override {
    SetUpFeatureList();
    LiveTest::SetUp();
    // Always disable animation for stability.
    ui::ScopedAnimationDurationScaleMode disable_animation(
        ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
  }

  void SetUpOnMainThread() override {
    LiveTest::SetUpOnMainThread();

    // Permits sharing the page screenshot by default. This disables the
    // permission dialog.
    PrefService* prefs = browser()->profile()->GetPrefs();
    prefs->SetBoolean(lens::prefs::kLensSharingPageScreenshotEnabled, true);

    // Set the default timeout for our run loops.
    base::test::ScopedRunLoopTimeout timeout(FROM_HERE,
                                             TestTimeouts::action_timeout());
  }

  void SetUpInProcessBrowserTestFixture() override {
    // Allowlists hosts.
    host_resolver()->AllowDirectLookup("*.nps.gov");

    LiveTest::SetUpInProcessBrowserTestFixture();
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    LiveTest::SetUpCommandLine(command_line);
    // Because we are taking a screenshot of a live page, we need to enable
    // pixel output in tests.
    command_line->AppendSwitch(::switches::kEnablePixelOutputInTests);
  }

  SidePanelCoordinator* side_panel_coordinator() {
    return browser()->GetFeatures().side_panel_coordinator();
  }

  syncer::SyncService* sync_service() {
    return signin::test::sync_service(browser());
  }

  signin::test::SignInFunctions sign_in_functions =
      signin::test::SignInFunctions(
          base::BindLambdaForTesting(
              [this]() -> Browser* { return this->browser(); }),
          base::BindLambdaForTesting(
              [this](int index,
                     const GURL& url,
                     ui::PageTransition transition) -> bool {
                return this->AddTabAtIndex(index, url, transition);
              }));

  content::WebContents* web_contents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  content::WebContents* GetOverlayWebContents() {
    auto* controller = browser()
                           ->tab_strip_model()
                           ->GetActiveTab()
                           ->GetTabFeatures()
                           ->lens_overlay_controller();
    return controller->GetOverlayWebViewForTesting()->GetWebContents();
  }

  content::EvalJsResult EvalJs(const std::string& code) {
    // Execute JS in Overlay WebUI.
    return content::EvalJs(GetOverlayWebContents()->GetPrimaryMainFrame(),
                           code);
  }

  void WaitForHistogram(const std::string& histogram_name) {
    // Continue if histogram was already recorded.
    if (base::StatisticsRecorder::FindHistogram(histogram_name)) {
      return;
    }

    // Else, wait until the histogram is recorded.
    base::RunLoop run_loop;
    auto histogram_observer = std::make_unique<
        base::StatisticsRecorder::ScopedHistogramSampleObserver>(
        histogram_name,
        base::BindLambdaForTesting(
            [&](std::string_view histogram_name, uint64_t name_hash,
                base::HistogramBase::Sample32 sample) { run_loop.Quit(); }));
    run_loop.Run();
  }

  // Lens overlay takes a screenshot of the tab. In order to take a screenshot
  // the tab must not be about:blank and must be painted. By default opens in
  // the current tab.
  void WaitForPaint(
      std::string_view url,
      WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB,
      int browser_test_flags = ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP) {
    ASSERT_TRUE(ui_test_utils::NavigateToURLWithDisposition(
        browser(), GURL(url), disposition, browser_test_flags));
    ASSERT_TRUE(base::test::RunUntil([&]() {
      return browser()
          ->tab_strip_model()
          ->GetActiveTab()
          ->GetContents()
          ->CompletedFirstVisuallyNonEmptyPaint();
    }));
  }

  // Verifies the side panel opened and loaded a search URL in its iframe.
  void VerifySidePanelLoaded() {
    auto* controller = browser()
                           ->tab_strip_model()
                           ->GetActiveTab()
                           ->GetTabFeatures()
                           ->lens_overlay_controller();

    // Expect the Lens Overlay results panel to open.
    ASSERT_TRUE(base::test::RunUntil(
        [&]() { return controller->state() == State::kOverlayAndResults; }));
    auto* coordinator = browser()->GetFeatures().side_panel_coordinator();
    ASSERT_TRUE(coordinator->IsSidePanelShowing());
    ASSERT_EQ(coordinator->GetCurrentEntryId(),
              SidePanelEntry::Id::kLensOverlayResults);

    // Wait for the panel to finish loading.
    EXPECT_TRUE(content::WaitForLoadStop(
        controller->GetSidePanelWebContentsForTesting()));

    // The results frame should be the only child frame of the side panel web
    // contents.
    content::RenderFrameHost* results_frame = content::ChildFrameAt(
        controller->GetSidePanelWebContentsForTesting()->GetPrimaryMainFrame(),
        0);
    EXPECT_TRUE(results_frame);
    EXPECT_TRUE(content::WaitForRenderFrameReady(results_frame));

    // Check the result frame URL matches a valid results URL.
    EXPECT_THAT(results_frame->GetLastCommittedURL().spec(),
                testing::MatchesRegex(
                    std::string(kResultsSearchBaseUrl) +
                    ".*source=chrome.cr.menu.*&gsc=2&hl=.*&biw=\\d+&bih=\\d+"));
  }

  virtual void SetUpFeatureList() {
    feature_list_.InitAndEnableFeatureWithParameters(
        lens::features::kLensOverlay,
        {{"enable-shimmer", "false"}, {"use-blur", "false"}});
  }

  void TearDown() override { LiveTest::TearDown(); }

 protected:
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(LensOverlayLiveTest, ClickText_SignedInAndSynced) {
  std::optional<signin::TestAccountSigninCredentials> test_account =
      GetTestAccounts()->GetAccount("INTELLIGENCE_ACCOUNT");
  // Sign in and sync to opted in test account.
  CHECK(test_account.has_value());
  sign_in_functions.TurnOnSync(*test_account, 0);
  EXPECT_TRUE(sync_service()->IsSyncFeatureEnabled());

  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  EXPECT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Verify that the page returns text that is selectable on the overlay.
  ASSERT_TRUE(base::test::RunUntil([&]() {
    return EvalJs(content::JsReplace(kFindAndClickDivWithClassScript,
                                     kDivWordClass))
        .ExtractBool();
  }));

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

IN_PROC_BROWSER_TEST_F(LensOverlayLiveTest, ClickText_SignedInNotSynced) {
  std::optional<signin::TestAccountSigninCredentials> test_account =
      GetTestAccounts()->GetAccount("INTELLIGENCE_ACCOUNT");
  // Sign in but do not sync to opted in test account.
  CHECK(test_account.has_value());
  sign_in_functions.SignInFromWeb(*test_account, 0);
  EXPECT_FALSE(sync_service()->IsSyncFeatureEnabled());

  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Verify that the page returns text that is selectable on the overlay.
  ASSERT_TRUE(base::test::RunUntil([&]() {
    return EvalJs(content::JsReplace(kFindAndClickDivWithClassScript,
                                     kDivWordClass))
        .ExtractBool();
  }));

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

IN_PROC_BROWSER_TEST_F(LensOverlayLiveTest, ClickText_SignedOut) {
  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Verify that the page returns text that is selectable on the overlay.
  ASSERT_TRUE(base::test::RunUntil([&]() {
    return EvalJs(content::JsReplace(kFindAndClickDivWithClassScript,
                                     kDivWordClass))
        .ExtractBool();
  }));

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

IN_PROC_BROWSER_TEST_F(LensOverlayLiveTest, ClickObject_SignedInAndSynced) {
  std::optional<signin::TestAccountSigninCredentials> test_account =
      GetTestAccounts()->GetAccount("INTELLIGENCE_ACCOUNT");
  // Sign in and sync to opted in test account.
  CHECK(test_account.has_value());
  sign_in_functions.TurnOnSync(*test_account, 0);
  EXPECT_TRUE(sync_service()->IsSyncFeatureEnabled());

  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsObjectUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Verify that the page returns objects that is selectable on the overlay.
  ASSERT_TRUE(base::test::RunUntil([&]() {
    return EvalJs(content::JsReplace(kFindAndClickDivWithClassScript,
                                     kDivObjectClass))
        .ExtractBool();
  }));

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

IN_PROC_BROWSER_TEST_F(LensOverlayLiveTest, ClickObject_SignedInNotSynced) {
  std::optional<signin::TestAccountSigninCredentials> test_account =
      GetTestAccounts()->GetAccount("INTELLIGENCE_ACCOUNT");
  // Sign in but do not sync to opted in test account.
  CHECK(test_account.has_value());
  sign_in_functions.SignInFromWeb(*test_account, 0);
  EXPECT_FALSE(sync_service()->IsSyncFeatureEnabled());

  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsObjectUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Verify that the page returns objects that is selectable on the overlay.
  ASSERT_TRUE(base::test::RunUntil([&]() {
    return EvalJs(content::JsReplace(kFindAndClickDivWithClassScript,
                                     kDivObjectClass))
        .ExtractBool();
  }));

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

IN_PROC_BROWSER_TEST_F(LensOverlayLiveTest, ClickObject_SignedOut) {
  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsObjectUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Verify that the page returns objects that is selectable on the overlay.
  ASSERT_TRUE(base::test::RunUntil([&]() {
    return EvalJs(content::JsReplace(kFindAndClickDivWithClassScript,
                                     kDivObjectClass))
        .ExtractBool();
  }));

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

// Live tests for LensOverlayTranslateButton.
class LensOverlayTranslateLiveTest : public LensOverlayLiveTest {
 public:
  void ClickTranslateButtonAndThenText() {
    // Find and click the translate enable button when it appears on the
    // overlay.
    ASSERT_TRUE(base::test::RunUntil([&]() {
      return EvalJs(content::JsReplace(kFindAndClickElementWithIDScript,
                                       kTranslateEnableButtonID))
          .ExtractBool();
    }));

    // The translated lines render and need some time in order
    // for the overlay to compute their bounding boxes for highlighted lines.
    // For this reason, keep clicking on the line until the side panel actually
    // opens.
    auto* controller = browser()
                           ->tab_strip_model()
                           ->GetActiveTab()
                           ->GetTabFeatures()
                           ->lens_overlay_controller();
    ASSERT_TRUE(base::test::RunUntil([&]() {
      EvalJs(content::JsReplace(kFindAndClickDivWithClassScript,
                                kDivTranslatedLineClass));
      return controller->state() == State::kOverlayAndResults;
    }));
  }

  void SetUpFeatureList() override {
    feature_list_.InitWithFeaturesAndParameters(
        {{lens::features::kLensOverlay,
          {{"enable-shimmer", "false"}, {"use-blur", "false"}}},
         {features::kLensOverlayTranslateButton, {}}},
        {});
  }
};

IN_PROC_BROWSER_TEST_F(LensOverlayTranslateLiveTest,
                       TranslateScreen_SignedInAndSynced) {
  std::optional<signin::TestAccountSigninCredentials> test_account =
      GetTestAccounts()->GetAccount("INTELLIGENCE_ACCOUNT");
  // Sign in and sync to opted in test account.
  CHECK(test_account.has_value());
  sign_in_functions.TurnOnSync(*test_account, 0);
  EXPECT_TRUE(sync_service()->IsSyncFeatureEnabled());

  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsTranslateUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Check if the translate button exits and then click on a translated line.
  ClickTranslateButtonAndThenText();

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

IN_PROC_BROWSER_TEST_F(LensOverlayTranslateLiveTest,
                       TranslateScreen_SignedInNotSynced) {
  std::optional<signin::TestAccountSigninCredentials> test_account =
      GetTestAccounts()->GetAccount("INTELLIGENCE_ACCOUNT");
  // Sign in but do not sync to opted in test account.
  CHECK(test_account.has_value());
  sign_in_functions.SignInFromWeb(*test_account, 0);
  EXPECT_FALSE(sync_service()->IsSyncFeatureEnabled());

  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsTranslateUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that it is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Check if the translate button exits and then click on a translated line.
  ClickTranslateButtonAndThenText();

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

IN_PROC_BROWSER_TEST_F(LensOverlayTranslateLiveTest,
                       TranslateScreen_SignedOut) {
  // Navigate to a website and wait for paint before starting controller.
  WaitForPaint(kNpsTranslateUrl);
  EXPECT_TRUE(content::WaitForLoadStop(web_contents()));

  // State should start in off.
  auto* controller = browser()
                         ->tab_strip_model()
                         ->GetActiveTab()
                         ->GetTabFeatures()
                         ->lens_overlay_controller();
  ASSERT_EQ(controller->state(), State::kOff);

  auto* search_controller = browser()
                                ->tab_strip_model()
                                ->GetActiveTab()
                                ->GetTabFeatures()
                                ->lens_search_controller();

  // Showing UI should change the state to screenshot and eventually to overlay.
  search_controller->OpenLensOverlay(LensOverlayInvocationSource::kAppMenu);
  ASSERT_EQ(controller->state(), State::kScreenshot);
  ASSERT_TRUE(base::test::RunUntil(
      [&]() { return controller->state() == State::kOverlay; }));
  ASSERT_EQ(side_panel_coordinator()->GetCurrentEntryId(), std::nullopt);
  ASSERT_TRUE(content::WaitForLoadStop(GetOverlayWebContents()));

  // Confirm that the WebUI has reported that i1t is ready. This means the local
  // DOM should be initialized on our WebUI.
  WaitForHistogram("Lens.Overlay.TimeToWebUIReady");

  // Check if the translate button exits and then click on a translated line.
  ClickTranslateButtonAndThenText();

  // After finding and clicking the div, make sure the side panel opens and
  // loaded a result.
  VerifySidePanelLoaded();
}

}  // namespace lens