File: glic_fre_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 (264 lines) | stat: -rw-r--r-- 10,254 bytes parent folder | download | duplicates (2)
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
// Copyright 2025 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/glic/fre/glic_fre_controller.h"

#include "base/test/run_until.h"
#include "base/time/time.h"
#include "chrome/browser/glic/glic_keyed_service.h"
#include "chrome/browser/glic/glic_pref_names.h"
#include "chrome/browser/glic/test_support/non_interactive_glic_test.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace glic {
namespace {

class GlicFreControllerBrowserTest : public NonInteractiveGlicTest {
 public:
  GlicFreControllerBrowserTest() = default;
  ~GlicFreControllerBrowserTest() override = default;

  void SetUpOnMainThread() override {
    glic::test::InteractiveGlicTest::SetUpOnMainThread();
    glic_test_environment().SetFRECompletion(prefs::FreStatus::kNotStarted);
  }

  GlicFreController* glic_fre_controller() {
    return glic_test_environment()
        .GetService()
        ->window_controller()
        .fre_controller();
  }

  tabs::TabInterface* GetTabInterfaceForActiveWebContents(Browser* browser) {
    content::WebContents* tab_web_contents =
        browser->tab_strip_model()->GetActiveWebContents();
    return tabs::TabInterface::MaybeGetFromContents(tab_web_contents);
  }

  void WaitForFreShow() {
    ASSERT_TRUE(base::test::RunUntil([&]() {
      return glic_fre_controller()->IsShowingDialog();
    })) << "FRE dialog should have been shown";
  }

  void WaitForFreClose() {
    ASSERT_TRUE(base::test::RunUntil([&]() {
      return !glic_fre_controller()->IsShowingDialog();
    })) << "FRE dialog should have been closed";
  }

  void WaitForGlicPanelShow() {
    ASSERT_TRUE(base::test::RunUntil([&]() {
      return glic_test_environment().GetService()->IsWindowShowing();
    })) << "Glic panel should have been shown";
  }

  void EnsureFreDoesNotShow() {
    auto end_time = base::TimeTicks::Now() + base::Milliseconds(500);
    ASSERT_TRUE(base::test::RunUntil(
        [&]() { return end_time < base::TimeTicks::Now(); }));
    ASSERT_FALSE(glic_fre_controller()->IsShowingDialog())
        << "FRE dialog should not have been shown";
  }
};

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       FreDialogShowBlockedByModalUI) {
  tabs::TabInterface* tab = GetTabInterfaceForActiveWebContents(browser());

  // FRE dialog should be blocked from showing if another modal dialog is
  // already open.
  auto scoped_tab_modal_ui = tab->ShowModalUI();
  EXPECT_FALSE(glic_fre_controller()->CanShowFreDialog(browser()));

  // The FRE dialog should be able to open after the existing modal dialog
  // is closed.
  scoped_tab_modal_ui.reset();
  EXPECT_TRUE(glic_fre_controller()->CanShowFreDialog(browser()));
  glic_fre_controller()->ShowFreDialog(browser());

  // Verify the FRE dialog is shown.
  WaitForFreShow();
}

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       FreDialogBlocksOtherModalUI) {
  tabs::TabInterface* tab = GetTabInterfaceForActiveWebContents(browser());

  // The FRE dialog should be able to open with no other modal dialogs open.
  EXPECT_TRUE(glic_fre_controller()->CanShowFreDialog(browser()));
  glic_fre_controller()->ShowFreDialog(browser());

  // Verify the FRE dialog is shown.
  WaitForFreShow();

  // Verify that another modal dialog cannot be shown now that the FRE is open.
  EXPECT_FALSE(tab->CanShowModalUI());

  // Once the FRE is closed, other modal dialogs can be shown again.
  glic_fre_controller()->DismissFre();
  EXPECT_TRUE(tab->CanShowModalUI());
}

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       FreDialogShowBlockedByItself) {
  // Open the FRE dialog in a tab.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(0);
  EXPECT_TRUE(glic_fre_controller()->CanShowFreDialog(browser()));
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();

  // Showing the FRE should be blocked as it is already open in the same tab.
  EXPECT_FALSE(glic_fre_controller()->CanShowFreDialog(browser()));
}

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       DismissFreDialogOnActiveTab) {
  // Open the FRE dialog in a tab.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(0);
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();

  // Close the FRE on the active tab.
  glic_fre_controller()->DismissFreIfOpenOnActiveTab(browser());
  EXPECT_FALSE(glic_fre_controller()->IsShowingDialog());
}

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       ShowFreDialogOnFailedCookieSync) {
  glic_test_environment().SetResultForFutureCookieSyncInFre(false);
  // Open the FRE dialog in a tab.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(0);
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();
}

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       DontDismissFreDialogOnInactiveTab) {
  // Open the FRE dialog in a tab.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(0);
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();

  // Open a new tab at the end of the tab strip and activate it.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(1);

  // Attempting to close the FRE on the active tab should do nothing.
  glic_fre_controller()->DismissFreIfOpenOnActiveTab(browser());
  EXPECT_TRUE(glic_fre_controller()->IsShowingDialog());
}

// TODO(crbug.com/402310277): Re-enable this test.
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_FreDialogCloseAndReopenForDifferentTab \
  DISABLED_FreDialogCloseAndReopenForDifferentTab
#else
#define MAYBE_FreDialogCloseAndReopenForDifferentTab \
  FreDialogCloseAndReopenForDifferentTab
#endif  // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
// Tests that, when the FRE dialog is already open in an inactive tab, trying to
// show it in the active tab closes the existing dialog and opens a new one.
IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       MAYBE_FreDialogCloseAndReopenForDifferentTab) {
  // Open the FRE dialog in a tab.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(0);
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();
  tabs::TabInterface* original_tab =
      GetTabInterfaceForActiveWebContents(browser());

  // Open a new tab at the end of the tab strip and activate it.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(1);
  tabs::TabInterface* new_tab = GetTabInterfaceForActiveWebContents(browser());

  // Opening the FRE dialog should close the existing dialog.
  EXPECT_TRUE(glic_fre_controller()->CanShowFreDialog(browser()));
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();
  // The original tab no longer has a modal, while the active one does.
  EXPECT_TRUE(original_tab->CanShowModalUI());
  EXPECT_FALSE(new_tab->CanShowModalUI());
}

// Tests that, when the FRE dialog is already open in an inactive tab and some
// other modal is open in the active tab, trying to show the FRE does nothing.
IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       InactiveTabFreDialogNotClosedWhenBlockedByModalUI) {
  // Open the FRE dialog in a tab.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(0);
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();
  tabs::TabInterface* original_tab =
      GetTabInterfaceForActiveWebContents(browser());

  // Open a new tab at the end of the tab strip and activate it.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(1);
  tabs::TabInterface* new_tab = GetTabInterfaceForActiveWebContents(browser());

  // Show some other modal in the active tab.
  auto scoped_tab_modal_ui = new_tab->ShowModalUI();
  // The FRE should be blocked from showing, the existing FRE should not close,
  // and a new FRE should not be opened in the active tab.
  EXPECT_FALSE(glic_fre_controller()->CanShowFreDialog(browser()));
  glic_fre_controller()->DismissFreIfOpenOnActiveTab(browser());
  WaitForFreShow();
  EXPECT_FALSE(original_tab->CanShowModalUI());
}

// Test proper destruction of the FRE controller when the WebContents is
// destroyed.
IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest,
                       FreControllerWithWebContentsDestruction) {
  // Open the FRE dialog in a tab.
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();

  // Open a new tab at the end of the tab strip and activate it.
  chrome::AddTabAt(browser(), GURL("about:blank"), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(1);

  // Destroy the WebContents that the dialog is being shown on.
  browser()->tab_strip_model()->CloseWebContentsAt(
      0, TabCloseTypes::CLOSE_USER_GESTURE);
}

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest, FreAcceptance) {
  // Open the FRE dialog in a tab.
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();

  // Accept the FRE and confirm it closed and the glic panel opened.
  glic_fre_controller()->AcceptFre();
  WaitForFreClose();
  WaitForGlicPanelShow();
}

IN_PROC_BROWSER_TEST_F(GlicFreControllerBrowserTest, DoNotCrashOnBrowserClose) {
  // Open the FRE dialog in a tab.
  glic_fre_controller()->ShowFreDialog(browser());
  WaitForFreShow();

  chrome::CloseAllBrowsers();
}

}  // namespace
}  // namespace glic