File: shortcut_integration_interactive_uitest.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 (262 lines) | stat: -rw-r--r-- 11,154 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
// 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 "base/test/bind.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/shortcuts/shortcut_creation_test_support.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#include "chrome/browser/ui/views/shortcuts/shortcut_integration_interaction_test_base.h"
#include "chrome/grit/branded_strings.h"
#include "content/public/test/browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/view_class_properties.h"
#include "url/gurl.h"

namespace shortcuts {

namespace {

// Identifier for the initially created tab.
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kInitialTabId);
// Identifier for the tab opened as a result of launching a shortcut.
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kNewTabId);
// Identifier for the shortcut created in these tests.
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kNewShortcutId);

}  // namespace

using ShortcutIntegrationInteractiveUiTest =
    ShortcutIntegrationInteractionTestBase;

IN_PROC_BROWSER_TEST_F(ShortcutIntegrationInteractiveUiTest, CreateAndLaunch) {
  const GURL kPageWithIconsUrl =
      embedded_https_test_server().GetURL("/shortcuts/page_icons.html");
  RunTestSequence(
      InstrumentTab(kInitialTabId),
      NavigateWebContents(kInitialTabId, kPageWithIconsUrl),
      InstrumentNextShortcut(kNewShortcutId),
      ShowAndAcceptCreateShortcutDialog(),
      CheckShortcut(kNewShortcutId, IsShortcutForUrl(kPageWithIconsUrl)),
      InstrumentNextTab(kNewTabId), LaunchShortcut(kNewShortcutId),
      WaitForWebContentsNavigation(kNewTabId, kPageWithIconsUrl));
}

IN_PROC_BROWSER_TEST_F(ShortcutIntegrationInteractiveUiTest, CustomTitle) {
  const GURL kPageWithIconsUrl =
      embedded_https_test_server().GetURL("/shortcuts/page_icons.html");
  RunTestSequence(
      InstrumentTab(kInitialTabId),
      NavigateWebContents(kInitialTabId, kPageWithIconsUrl),
      InstrumentNextShortcut(kNewShortcutId),
      ShowCreateShortcutDialogSetTitleAndAccept(u"Hello World!"),
      CheckShortcut(kNewShortcutId, IsShortcutWithTitle(u"Hello World!")));
}

IN_PROC_BROWSER_TEST_F(ShortcutIntegrationInteractiveUiTest,
                       MultipleShortcuts) {
  const GURL kPageWithIconsUrl =
      embedded_https_test_server().GetURL("/shortcuts/page_icons.html");
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kSecondShortcutId);

  base::FilePath shortcut_path;
  RunTestSequence(
      InstrumentTab(kInitialTabId),
      NavigateWebContents(kInitialTabId, kPageWithIconsUrl),

      InstrumentNextShortcut(kNewShortcutId),
      ShowAndAcceptCreateShortcutDialog(),
      CheckShortcut(kNewShortcutId, IsShortcutForUrl(kPageWithIconsUrl)),

      InstrumentNextShortcut(kSecondShortcutId),
      ShowAndAcceptCreateShortcutDialog(),
      CheckShortcut(kSecondShortcutId, IsShortcutForUrl(kPageWithIconsUrl)),

      CheckShortcut(kNewShortcutId,
                    IsShortcutWithTitle(u"Page with icon links")),
      CheckShortcut(kSecondShortcutId,
                    IsShortcutWithTitle(u"Page with icon links")),

      // Sanity check that the two created shortcuts are in fact separate
      // shortcuts, despite being for the same page and profile.
      InAnyContext(WithElement(kNewShortcutId,
                               [&](ui::TrackedElement* shortcut) {
                                 shortcut_path = GetShortcutPath(shortcut);
                               })),
      CheckShortcut(kSecondShortcutId, testing::Ne(std::cref(shortcut_path))));
}

namespace {
// Initial tab in profile 1.
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kProfile1TabId);
// Initial tab in profile 2.
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kProfile2TabId);
// Shortcut created in profile 1.
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kProfile1ShortcutId);
// Shortcut created in profile 2.
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kProfile2ShortcutId);
}  // namespace

class ShortcutIntegrationMultiProfileInteractiveUiTest
    : public ShortcutIntegrationInteractiveUiTest {
 public:
  void SetUpOnMainThread() override {
    ShortcutIntegrationInteractiveUiTest::SetUpOnMainThread();

    ProfileManager* profile_manager = g_browser_process->profile_manager();
    base::FilePath new_path =
        profile_manager->GenerateNextProfileDirectoryPath();
    profile2_ = profiles::testing::CreateProfileSync(profile_manager, new_path)
                    .GetWeakPtr();
    profile2_browser_ =
        chrome::OpenEmptyWindow(profile2(),
                                /*should_trigger_session_restore=*/false)
            ->AsWeakPtr();
  }

  Profile* profile1() { return browser()->profile(); }
  Profile* profile2() { return profile2_.get(); }
  Browser* profile1_browser() { return browser(); }
  Browser* profile2_browser() { return profile2_browser_.get(); }

  GURL profile1_shortcut_url() {
    return embedded_https_test_server().GetURL("/shortcuts/page_icons.html");
  }
  GURL profile2_shortcut_url() {
    return embedded_https_test_server().GetURL("/shortcuts/no_icons_page.html");
  }

  // Returns a step that behaves as if the user deleted the given profile.
  [[nodiscard]] static MultiStep DeleteProfile(Profile* profile) {
    base::FilePath profile_path = profile->GetPath();
    return Steps(Do([profile_path] {
      ProfileManager* profile_manager = g_browser_process->profile_manager();
      profile_manager->GetDeleteProfileHelper().MaybeScheduleProfileForDeletion(
          profile_path, base::DoNothing(),
          ProfileMetrics::DELETE_PROFILE_USER_MANAGER);
    }));
  }

  // Returns a step that locks the given profile.
  [[nodiscard]] static MultiStep LockProfile(Profile* profile) {
    base::FilePath profile_path = profile->GetPath();
    return Steps(Do([profile_path] {
      ProfileAttributesEntry* entry =
          g_browser_process->profile_manager()
              ->GetProfileAttributesStorage()
              .GetProfileAttributesWithPath(profile_path);
      ASSERT_NE(entry, nullptr);
      entry->LockForceSigninProfile(true);
      EXPECT_TRUE(entry->IsSigninRequired());
    }));
  }

  // Creates shortcuts in both profiles.
  [[nodiscard]] MultiStep CreateShortcuts() {
    return Steps(InstrumentTab(kProfile1TabId, /*tab_index=*/std::nullopt,
                               profile1_browser()),
                 InstrumentTab(kProfile2TabId, /*tab_index=*/std::nullopt,
                               profile2_browser()),
                 NavigateWebContents(kProfile1TabId, profile1_shortcut_url()),
                 NavigateWebContents(kProfile2TabId, profile2_shortcut_url()),

                 InstrumentNextShortcut(kProfile2ShortcutId),
                 InContext(profile2_browser()->window()->GetElementContext(),
                           ShowAndAcceptCreateShortcutDialog()),
                 InAnyContext(WaitForShow(kProfile2ShortcutId)),

                 InstrumentNextShortcut(kProfile1ShortcutId),
                 InContext(profile1_browser()->window()->GetElementContext(),
                           ShowAndAcceptCreateShortcutDialog()),
                 InAnyContext(WaitForShow(kProfile1ShortcutId)));
  }

  static base::FilePath ProfilePathFromWebContents(ui::TrackedElement* te) {
    auto* const wc =
        ShortcutIntegrationInteractiveUiTest::AsInstrumentedWebContents(te);
    return wc->web_contents()->GetBrowserContext()->GetPath();
  }

 private:
  base::WeakPtr<Profile> profile2_;
  base::WeakPtr<Browser> profile2_browser_;
};

IN_PROC_BROWSER_TEST_F(ShortcutIntegrationMultiProfileInteractiveUiTest,
                       CreatedForCorrectProfile) {
  const std::u16string shortcut_title_1 =
      u"Page with icon links (" +
      // "Your Chrome"
      l10n_util::GetStringUTF16(IDS_PROFILE_MENU_PLACEHOLDER_PROFILE_NAME) +
      u")";
  const std::u16string shortcut_title_2 = u"Page without icons (Person 1)";
  RunTestSequence(
      CreateShortcuts(),
      CheckShortcut(kProfile1ShortcutId, IsShortcutWithTitle(shortcut_title_1)),
      CheckShortcut(kProfile1ShortcutId,
                    IsShortcutForProfile(profile1()->GetPath())),
      CheckShortcut(kProfile2ShortcutId, IsShortcutWithTitle(shortcut_title_2)),
      CheckShortcut(kProfile2ShortcutId,
                    IsShortcutForProfile(profile2()->GetPath())));
}

IN_PROC_BROWSER_TEST_F(ShortcutIntegrationMultiProfileInteractiveUiTest,
                       LaunchInCorrectProfile) {
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kProfile1NewTabId);
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kProfile2NewTabId);

  RunTestSequence(
      CreateShortcuts(),

      // Shortcut from profile 1 should open in profile 1.
      InstrumentNextTab(kProfile1NewTabId, AnyBrowser()),
      LaunchShortcut(kProfile1ShortcutId),
      WaitForWebContentsNavigation(kProfile1NewTabId, profile1_shortcut_url()),
      InAnyContext(CheckElement(kProfile1NewTabId, &ProfilePathFromWebContents,
                                ::testing::Eq(profile1()->GetPath()))),

      // Similarly, shortcut from profile 2 should open in profile 2.
      InstrumentNextTab(kProfile2NewTabId, AnyBrowser()),
      LaunchShortcut(kProfile2ShortcutId),
      WaitForWebContentsNavigation(kProfile2NewTabId, profile2_shortcut_url()),
      InAnyContext(CheckElement(kProfile2NewTabId, &ProfilePathFromWebContents,
                                ::testing::Eq(profile2()->GetPath()))),

      // Now delete profile 2; launching shortcut from profile 2 should now open
      // in profile 1.
      DeleteProfile(profile2()), InstrumentNextTab(kNewTabId, AnyBrowser()),
      LaunchShortcut(kProfile2ShortcutId),
      WaitForWebContentsNavigation(kNewTabId, profile2_shortcut_url()),
      InAnyContext(CheckElement(kNewTabId, &ProfilePathFromWebContents,
                                ::testing::Eq(profile1()->GetPath()))));
}

IN_PROC_BROWSER_TEST_F(ShortcutIntegrationMultiProfileInteractiveUiTest,
                       DontLaunchInLockedProfile) {
  signin_util::ScopedForceSigninSetterForTesting signin_setter(true);

  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kProfilePickerViewId);
  ProfilePicker::AddOnProfilePickerOpenedCallbackForTesting(
      base::BindLambdaForTesting([kProfilePickerViewId] {
        ProfilePicker::GetViewForTesting()->SetProperty(
            views::kElementIdentifierKey, kProfilePickerViewId);
      }));

  RunTestSequence(
      CreateShortcuts(),

      // Shortcut from locked profile 2 should cause profile picker to open.
      LockProfile(profile2()), LaunchShortcut(kProfile2ShortcutId),
      InAnyContext(WaitForShow(kProfilePickerViewId)));
}

}  // namespace shortcuts