File: data_sharing_live_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 (246 lines) | stat: -rw-r--r-- 9,029 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
// 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/command_line.h"
#include "base/scoped_observation.h"
#include "base/test/bind.h"
#include "chrome/browser/signin/e2e_tests/live_test.h"
#include "chrome/browser/signin/e2e_tests/signin_util.h"
#include "chrome/browser/tab_group_sync/tab_group_sync_service_factory.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/tab_group_action_context_desktop.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/data_sharing/data_sharing_bubble_controller.h"
#include "components/data_sharing/public/features.h"
#include "components/saved_tab_groups/internal/tab_group_sync_service_impl.h"
#include "components/saved_tab_groups/public/features.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/test_accounts.h"
#include "components/sync/service/sync_service.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tabs/public/tab_group.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"

namespace {

// A class to wait for a tab group with specific title to open
class SavedTabGroupServiceWaiter
    : public tab_groups::TabGroupSyncService::Observer {
 public:
  explicit SavedTabGroupServiceWaiter(
      tab_groups::TabGroupSyncService* tab_group_service,
      const std::u16string title)
      : tab_group_service_(tab_group_service), title_(title) {
    tab_group_sync_service_observation_.Observe(tab_group_service_);
    bool should_wait = true;
    for (const tab_groups::SavedTabGroup& group :
         tab_group_service_->GetAllGroups()) {
      if (group.title() == title_) {
        should_wait = false;
        break;
      }
    }
    if (should_wait) {
      run_loop_.Run();
    }
  }

  void OnTabGroupAdded(const tab_groups::SavedTabGroup& group,
                       tab_groups::TriggerSource source) override {
    if (run_loop_.running() && group.title() == title_) {
      run_loop_.Quit();
    }
  }

 private:
  base::ScopedObservation<tab_groups::TabGroupSyncService,
                          tab_groups::TabGroupSyncService::Observer>
      tab_group_sync_service_observation_{this};
  base::RunLoop run_loop_;
  const raw_ptr<tab_groups::TabGroupSyncService> tab_group_service_;
  const std::u16string title_;
};

// A class to wait for a javascript statement to return true.
class EvalScriptWaiter {
 public:
  EvalScriptWaiter(content::WebContents* web_contents,
                   const std::string& script)
      : web_contents_(web_contents), script_(script) {
    if (content::EvalJs(web_contents_, script_).ExtractBool()) {
      return;
    }
    timer_.Start(FROM_HERE, base::Seconds(0.1), this,
                 &EvalScriptWaiter::EvalScript);
    run_loop_.Run();
  }

 private:
  void EvalScript() {
    if (content::EvalJs(web_contents_, script_).ExtractBool()) {
      timer_.Stop();
      run_loop_.Quit();
    }
  }

  raw_ptr<content::WebContents> web_contents_;
  const std::string script_;
  base::RunLoop run_loop_;
  base::RepeatingTimer timer_;
};

// End to end test for shared tab group UI on desktop.
class DataSharingLiveTest : public signin::test::LiveTest {
 public:
  DataSharingLiveTest() = default;
  ~DataSharingLiveTest() override = default;

  void SetUp() override {
    scoped_feature_list_.InitWithFeatures(
        {data_sharing::features::kDataSharingFeature,
         tab_groups::kTabGroupSyncServiceDesktopMigration},
        {});
    constexpr char SYNC_URL[] =
        "https://chrome-sync.sandbox.google.com/chrome-sync/alpha";
    base::CommandLine::ForCurrentProcess()->AppendSwitchASCII("sync-url",
                                                              SYNC_URL);
    LiveTest::SetUp();
    // Always disable animation for stability.
    ui::ScopedAnimationDurationScaleMode disable_animation(
        ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
  }

  signin::IdentityManager* identity_manager() {
    return signin::test::identity_manager(browser());
  }

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

  tab_groups::TabGroupSyncService* tab_group_service() {
    return tab_groups::TabGroupSyncServiceFactory::GetForProfile(
        browser()->profile());
  }

  void SignInAndTurnOnSync() {
    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);
                }));

    std::optional<signin::TestAccountSigninCredentials> test_account =
        GetTestAccounts()->GetAccount("DATA_SHARING_1");
    CHECK(test_account);
    sign_in_functions.SignInFromSettings(*test_account, 0);
    sign_in_functions.TurnOnSync(*test_account, 0);

    const CoreAccountInfo& primary_account =
        identity_manager()->GetPrimaryAccountInfo(signin::ConsentLevel::kSync);
    EXPECT_FALSE(primary_account.IsEmpty());
    EXPECT_TRUE(gaia::AreEmailsSame(test_account->user, primary_account.email));
    EXPECT_TRUE(sync_service()->IsSyncFeatureEnabled());
  }

  std::optional<tab_groups::TabGroupId> OpenTabGroupByTitle(
      tab_groups::TabGroupSyncService* tab_group_service,
      std::u16string title) {
    bool open = false;
    for (const tab_groups::SavedTabGroup& group :
         tab_group_service->GetAllGroups()) {
      if (group.title() == title) {
        tab_group_service->OpenTabGroup(
            group.saved_guid(),
            std::make_unique<tab_groups::TabGroupActionContextDesktop>(
                browser(), tab_groups::OpeningSource::kUnknown));
        open = true;
      }
    }
    DCHECK(open);
    TabGroupModel* tab_group_model =
        browser()->tab_strip_model()->group_model();
    for (const tab_groups::TabGroupId& id : tab_group_model->ListTabGroups()) {
      const tab_groups::TabGroupVisualData* visual_data =
          tab_group_model->GetTabGroup(id)->visual_data();
      if (visual_data->title() == title) {
        return id;
      }
    }
    return std::nullopt;
  }

  void WaitForSDKToLoad() {
    content::WebContents* web_contents =
        DataSharingBubbleController::GetOrCreateForBrowser(browser())
            ->BubbleViewForTesting()
            ->get_contents_wrapper_for_testing()
            ->web_contents();
    EvalScriptWaiter script_waiter(
        web_contents,
        "!!document.querySelector('data-sharing-app') &&"
        "!!document.querySelector('data-sharing-app').shadowRoot &&"
        "document.querySelector('data-sharing-app').shadowRoot."
        "querySelectorAll('.peopleSharekitContainerComponents').length == 1;");
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Open the share dialog of a unshared the tab group.
IN_PROC_BROWSER_TEST_F(DataSharingLiveTest, ShareUnsharedTabGroup) {
  SignInAndTurnOnSync();

  const std::u16string unshared_group_title = u"TEST UNSHARED GROUP";
  SavedTabGroupServiceWaiter waiter(tab_group_service(), unshared_group_title);

  std::optional<tab_groups::TabGroupId> tab_group_id =
      OpenTabGroupByTitle(tab_group_service(), unshared_group_title);
  CHECK(tab_group_id.has_value());

  data_sharing::RequestInfo request_info(tab_group_id.value(),
                                         data_sharing::FlowType::kShare);
  DataSharingBubbleController::GetOrCreateForBrowser(browser())->Show(
      request_info);

  WaitForSDKToLoad();
}

// Open the manage dialog of a shared tab group.
IN_PROC_BROWSER_TEST_F(DataSharingLiveTest, ManageSharedTabGroup) {
  SignInAndTurnOnSync();

  const std::u16string shared_group_title = u"TEST SHARED GROUP";
  SavedTabGroupServiceWaiter waiter(tab_group_service(), shared_group_title);

  std::optional<tab_groups::TabGroupId> tab_group_id =
      OpenTabGroupByTitle(tab_group_service(), shared_group_title);
  CHECK(tab_group_id.has_value());

  // Share the group.
  data_sharing::RequestInfo request_info(tab_group_id.value(),
                                         data_sharing::FlowType::kShare);
  DataSharingBubbleController::GetOrCreateForBrowser(browser())->Show(
      request_info);

  // Manage the group.
  request_info.type = data_sharing::FlowType::kManage;
  DataSharingBubbleController::GetOrCreateForBrowser(browser())->Show(
      request_info);

  WaitForSDKToLoad();
}

// TODO(crbug.com/366058571) Add e2e test for join flow.

}  // namespace