File: data_sharing_page_handler_unittest.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 (130 lines) | stat: -rw-r--r-- 4,918 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
// 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 "chrome/browser/ui/webui/data_sharing/data_sharing_page_handler.h"

#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/webui/data_sharing/data_sharing_ui.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "components/data_sharing/public/features.h"
#include "components/saved_tab_groups/public/features.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace {

class MockPage : public data_sharing::mojom::Page {
 public:
  MockPage() = default;
  ~MockPage() override = default;

  mojo::PendingRemote<data_sharing::mojom::Page> BindAndGetRemote() {
    DCHECK(!receiver_.is_bound());
    return receiver_.BindNewPipeAndPassRemote();
  }

  MOCK_METHOD(void, OnAccessTokenFetched, (const std::string& access_token));
  MOCK_METHOD(void,
              ReadGroups,
              (data_sharing::mojom::ReadGroupsParamsPtr read_groups_params,
               ReadGroupsCallback callback));
  MOCK_METHOD(void,
              ReadGroupWithToken,
              (data_sharing::mojom::ReadGroupWithTokenParamPtr param,
               ReadGroupWithTokenCallback callback));
  MOCK_METHOD(void,
              DeleteGroup,
              (const std::string& group_id, DeleteGroupCallback callback));
  MOCK_METHOD(void,
              LeaveGroup,
              (const std::string& group_id, LeaveGroupCallback callback));

  mojo::Receiver<data_sharing::mojom::Page> receiver_{this};
};

class TestDataSharingPageHandler : public DataSharingPageHandler {
 public:
  TestDataSharingPageHandler(
      DataSharingUI* webui_controller,
      mojo::PendingRemote<data_sharing::mojom::Page> page)
      : DataSharingPageHandler(
            webui_controller,
            mojo::PendingReceiver<data_sharing::mojom::PageHandler>(),
            std::move(page)) {}
};

}  // namespace

class DataSharingPageHandlerUnitTest : public BrowserWithTestWindowTest {
 public:
  DataSharingPageHandlerUnitTest()
      : BrowserWithTestWindowTest(
            base::test::SingleThreadTaskEnvironment::TimeSource::MOCK_TIME) {}
  void SetUp() override {
    scoped_feature_list_.InitWithFeatures(
        {data_sharing::features::kDataSharingFeature,
         tab_groups::kTabGroupSyncServiceDesktopMigration},
        {});
    BrowserWithTestWindowTest::SetUp();
    web_contents_ = content::WebContents::Create(
        content::WebContents::CreateParams(profile()));
    web_ui_.set_web_contents(web_contents_.get());
    webui_controller_ = std::make_unique<DataSharingUI>(&web_ui_);
    handler_ = std::make_unique<TestDataSharingPageHandler>(
        webui_controller_.get(), page_.BindAndGetRemote());
  }

  void TearDown() override {
    web_contents_.reset();
    handler_.reset();
    BrowserWithTestWindowTest::TearDown();
  }

  TestDataSharingPageHandler* handler() { return handler_.get(); }

 protected:
  testing::StrictMock<MockPage> page_;

 private:
  std::unique_ptr<content::WebContents> web_contents_;
  content::TestWebUI web_ui_;
  std::unique_ptr<TestDataSharingPageHandler> handler_;
  std::unique_ptr<DataSharingUI> webui_controller_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(DataSharingPageHandlerUnitTest, GetShareLink) {
  data_sharing::mojom::PageHandler::GetShareLinkCallback callback =
      base::BindLambdaForTesting(
          [&](const GURL& url) { ASSERT_TRUE(url.is_valid()); });
  handler()->GetShareLink("GROUP_ID", "ACCESS_TOKEN", std::move(callback));
}

TEST_F(DataSharingPageHandlerUnitTest, GetTabGroupPreview) {
  data_sharing::mojom::PageHandler::GetTabGroupPreviewCallback callback =
      base::BindLambdaForTesting(
          [&](data_sharing::mojom::GroupPreviewPtr preview) {
            EXPECT_EQ(preview->title, "");
            EXPECT_EQ(preview->shared_tabs.size(), size_t(0));
            EXPECT_EQ(preview->status_code,
                      mojo_base::mojom::AbslStatusCode::kUnknown);
          });
  handler()->GetTabGroupPreview("GROUP_ID", "ACCESS_TOKEN",
                                std::move(callback));
}

TEST_F(DataSharingPageHandlerUnitTest, OnAccessTokenFetched) {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  GTEST_SKIP() << "N/A for Google Chrome Branding Build";
#else
  // For non-branded build, the access token is set to expire 1 minute later.
  // (See kDummyTokenExpirationDuration in data_sharing_page_handler.cc)
  // Fast forward 1 minute to make sure the access token is refetched once.
  EXPECT_CALL(page_, OnAccessTokenFetched(testing::_)).Times(2);
  task_environment()->FastForwardBy(base::Minutes(1));
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)
}