File: desktop_notification_handler_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 (228 lines) | stat: -rw-r--r-- 9,014 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
// Copyright 2019 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/send_tab_to_self/desktop_notification_handler.h"

#include <vector>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/send_tab_to_self_sync_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/send_tab_to_self/send_tab_to_self_entry.h"
#include "components/send_tab_to_self/send_tab_to_self_sync_service.h"
#include "components/send_tab_to_self/test_send_tab_to_self_model.h"
#include "components/sync/base/data_type.h"
#include "components/sync/test/fake_data_type_controller_delegate.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/strings/grit/ui_strings.h"

namespace send_tab_to_self {

namespace {

const char kDesktopNotificationOrigin[] = "https://www.google.com";
const char kDesktopNotificationId[] = "notification_id";
const char kDesktopNotificationGuid[] = "guid";
const char kDesktopNotificationTitle[] = "title";
const char16_t kDesktopNotificationTitle16[] = u"title";
const char kDesktopNotificationDeviceInfo[] = "device_info";
const char kDesktopNotificationTargetDeviceSyncCacheGuid[] =
    "target_device_sync_cache_guid";
const char16_t kDesktopNotificationDeviceInfoWithPrefix[] =
    u"Shared from device_info";

class SendTabToSelfModelMock : public TestSendTabToSelfModel {
 public:
  SendTabToSelfModelMock() = default;
  ~SendTabToSelfModelMock() override = default;

  MOCK_METHOD1(DeleteEntry, void(const std::string&));
  MOCK_METHOD1(DismissEntry, void(const std::string&));
  MOCK_METHOD1(MarkEntryOpened, void(const std::string&));
};

class TestSendTabToSelfSyncService : public SendTabToSelfSyncService {
 public:
  TestSendTabToSelfSyncService() : fake_delegate_(syncer::SEND_TAB_TO_SELF) {}

  ~TestSendTabToSelfSyncService() override = default;

  SendTabToSelfModel* GetSendTabToSelfModel() override { return &model_mock_; }

  base::WeakPtr<syncer::DataTypeControllerDelegate> GetControllerDelegate()
      override {
    return fake_delegate_.GetWeakPtr();
  }

 protected:
  syncer::FakeDataTypeControllerDelegate fake_delegate_;
  SendTabToSelfModelMock model_mock_;
};

// Matcher to compare Notification object
MATCHER_P(EqualNotification, e, "") {
  return arg.type() == e.type() && arg.id() == e.id() &&
         arg.title() == e.title() && arg.message() == e.message() &&
         arg.origin_url() == e.origin_url();
}

std::unique_ptr<KeyedService> BuildTestSendTabToSelfSyncService(
    content::BrowserContext* context) {
  return std::make_unique<TestSendTabToSelfSyncService>();
}

class NotificationDisplayServiceMock : public NotificationDisplayService {
 public:
  NotificationDisplayServiceMock() = default;
  ~NotificationDisplayServiceMock() override = default;

  using NotificationDisplayService::DisplayedNotificationsCallback;

  MOCK_METHOD3(DisplayMockImpl,
               void(NotificationHandler::Type,
                    const message_center::Notification&,
                    NotificationCommon::Metadata*));
  void Display(
      NotificationHandler::Type notification_type,
      const message_center::Notification& notification,
      std::unique_ptr<NotificationCommon::Metadata> metadata) override {
    DisplayMockImpl(notification_type, notification, metadata.get());
  }

  MOCK_METHOD2(Close, void(NotificationHandler::Type, const std::string&));
  MOCK_METHOD1(GetDisplayed, void(DisplayedNotificationsCallback));
  MOCK_METHOD2(GetDisplayedForOrigin,
               void(const GURL& origin, DisplayedNotificationsCallback));
  MOCK_METHOD1(AddObserver, void(Observer* observer));
  MOCK_METHOD1(RemoveObserver, void(Observer* observer));
};

std::unique_ptr<KeyedService> BuildTestNotificationDisplayService(
    content::BrowserContext* context) {
  return std::make_unique<NotificationDisplayServiceMock>();
}

class DesktopNotificationHandlerBrowserTest : public InProcessBrowserTest {
 public:
  DesktopNotificationHandlerBrowserTest() = default;
  ~DesktopNotificationHandlerBrowserTest() override = default;

  // InProcessBrowserTest:
  void SetUpBrowserContextKeyedServices(
      content::BrowserContext* context) override {
    InProcessBrowserTest::SetUpBrowserContextKeyedServices(context);
    NotificationDisplayServiceFactory::GetInstance()->SetTestingFactory(
        context, base::BindRepeating(&BuildTestNotificationDisplayService));
    SendTabToSelfSyncServiceFactory::GetInstance()->SetTestingFactory(
        context, base::BindRepeating(&BuildTestSendTabToSelfSyncService));
  }

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    display_service_mock_ = static_cast<NotificationDisplayServiceMock*>(
        NotificationDisplayServiceFactory::GetForProfile(profile()));
    model_mock_ = static_cast<SendTabToSelfModelMock*>(
        SendTabToSelfSyncServiceFactory::GetForProfile(profile())
            ->GetSendTabToSelfModel());
  }

  void TearDownOnMainThread() override {
    model_mock_ = nullptr;
    display_service_mock_ = nullptr;
    InProcessBrowserTest::TearDownOnMainThread();
  }

  Profile* profile() { return browser()->profile(); }

 protected:
  raw_ptr<SendTabToSelfModelMock> model_mock_;
  raw_ptr<NotificationDisplayServiceMock> display_service_mock_;
};

IN_PROC_BROWSER_TEST_F(DesktopNotificationHandlerBrowserTest,
                       DisplayNewEntries) {
  const GURL& url = GURL(kDesktopNotificationOrigin);
  message_center::RichNotificationData optional_fields;
  optional_fields.never_timeout = true;
  message_center::Notification notification(
      message_center::NOTIFICATION_TYPE_SIMPLE, kDesktopNotificationGuid,
      kDesktopNotificationTitle16, kDesktopNotificationDeviceInfoWithPrefix,
      ui::ImageModel(), base::UTF8ToUTF16(url.host()), url,
      message_center::NotifierId(url), optional_fields, /*delegate=*/nullptr);

  SendTabToSelfEntry entry(kDesktopNotificationGuid, url,
                           kDesktopNotificationTitle, base::Time::Now(),
                           kDesktopNotificationDeviceInfo,
                           kDesktopNotificationTargetDeviceSyncCacheGuid);
  std::vector<const SendTabToSelfEntry*> entries;
  entries.push_back(&entry);

  DesktopNotificationHandler handler(profile());
  EXPECT_CALL(*display_service_mock_,
              DisplayMockImpl(NotificationHandler::Type::SEND_TAB_TO_SELF,
                              EqualNotification(notification), nullptr))
      .WillOnce(::testing::Return());

  handler.DisplayNewEntries(entries);
}

IN_PROC_BROWSER_TEST_F(DesktopNotificationHandlerBrowserTest, DismissEntries) {
  DesktopNotificationHandler handler(profile());
  EXPECT_CALL(*display_service_mock_,
              Close(NotificationHandler::Type::SEND_TAB_TO_SELF,
                    kDesktopNotificationGuid))
      .WillOnce(::testing::Return());

  std::vector<std::string> guids;
  guids.push_back(kDesktopNotificationGuid);
  handler.DismissEntries(guids);
}

IN_PROC_BROWSER_TEST_F(DesktopNotificationHandlerBrowserTest, CloseHandler) {
  DesktopNotificationHandler handler(profile());

  EXPECT_CALL(*model_mock_, DismissEntry(kDesktopNotificationId))
      .WillOnce(::testing::Return());

  handler.OnClose(profile(), GURL(kDesktopNotificationOrigin),
                  kDesktopNotificationId, /*by_user=*/false, base::DoNothing());

  EXPECT_CALL(*model_mock_, DismissEntry(kDesktopNotificationId))
      .WillOnce(::testing::Return());

  handler.OnClose(profile(), GURL(kDesktopNotificationOrigin),
                  kDesktopNotificationId, /*by_user=*/true, base::DoNothing());
}

IN_PROC_BROWSER_TEST_F(DesktopNotificationHandlerBrowserTest, ClickHandler) {
  DesktopNotificationHandler handler(profile());

  EXPECT_CALL(*display_service_mock_,
              Close(NotificationHandler::Type::SEND_TAB_TO_SELF,
                    kDesktopNotificationId))
      .WillOnce(::testing::Return());
  EXPECT_CALL(*model_mock_, MarkEntryOpened(kDesktopNotificationId))
      .WillOnce(::testing::Return());

  handler.OnClick(profile(), GURL(kDesktopNotificationOrigin),
                  kDesktopNotificationId, /*action_index=*/1,
                  /*reply=*/std::nullopt, base::DoNothing());
}

}  // namespace

}  // namespace send_tab_to_self