File: web_app_relaunch_notification_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (203 lines) | stat: -rw-r--r-- 7,894 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 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/web_applications/web_app_relaunch_notification.h"

#include <set>

#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/run_until.h"
#include "base/test/test_future.h"
#include "base/test/test_mock_time_task_runner.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/message_center/public/cpp/notification.h"

using testing::_;
using testing::AllOf;
using testing::Eq;
using testing::Field;
using testing::Property;

namespace web_app {

class WebAppRelaunchNotificationBrowserTest
    : public InProcessBrowserTest,
      public NotificationDisplayService::Observer {
 public:
  WebAppRelaunchNotificationBrowserTest()
      : task_runner_(base::MakeRefCounted<base::TestMockTimeTaskRunner>()) {
    GetTaskRunnerForTesting() = task_runner_;
  }

  // NotificationDisplayService::Observer:
  MOCK_METHOD(void,
              OnNotificationDisplayed,
              (const message_center::Notification&,
               const NotificationCommon::Metadata* const),
              (override));
  MOCK_METHOD(void,
              OnNotificationClosed,
              (const std::string& notification_id),
              (override));

  void OnNotificationDisplayServiceDestroyed(
      NotificationDisplayService* service) override {
    notification_observation_.Reset();
  }

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

  auto GetAllNotifications() {
    base::test::TestFuture<std::set<std::string>, bool> get_displayed_future;
    NotificationDisplayServiceFactory::GetForProfile(profile())->GetDisplayed(
        get_displayed_future.GetCallback());

    const auto& notification_ids = get_displayed_future.Get<0>();
    EXPECT_TRUE(get_displayed_future.Wait());
    return notification_ids;
  }

  void ClearAllNotifications() {
    NotificationDisplayService* service =
        NotificationDisplayServiceFactory::GetForProfile(profile());

    for (const std::string& notification_id : GetAllNotifications()) {
      service->Close(NotificationHandler::Type::TRANSIENT, notification_id);
    }
  }

  size_t GetDisplayedNotificationsCount() {
    return GetAllNotifications().size();
  }

 protected:
  scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;

  base::ScopedObservation<NotificationDisplayService,
                          WebAppRelaunchNotificationBrowserTest>
      notification_observation_{this};
};

IN_PROC_BROWSER_TEST_F(WebAppRelaunchNotificationBrowserTest,
                       ShowNotificationOnRelaunch) {
  ClearAllNotifications();
  notification_observation_.Observe(
      NotificationDisplayServiceFactory::GetForProfile(profile()));

  EXPECT_CALL(
      *this,
      OnNotificationDisplayed(
          AllOf(
              Property(&message_center::Notification::id,
                       Eq("web_app_relaunch_notifier:placeholder_app_id")),
              Property(&message_center::Notification::notifier_id,
                       Field(&message_center::NotifierId::id,
                             Eq("web_app_relaunch"))),
              Property(&message_center::Notification::title,
                       Eq(u"Restarting and updating final app title")),
              Property(
                  &message_center::Notification::message,
                  Eq(u"Please wait while this application is being updated"))),
          _))
      .Times(1);

  NotifyAppRelaunchState("placeholder_app_id", "final_app_id",
                         u"final app title", profile()->GetWeakPtr(),
                         AppRelaunchState::kAppClosingForRelaunch);
  EXPECT_EQ(1u, GetDisplayedNotificationsCount());

  NotifyAppRelaunchState("placeholder_app_id", "final_app_id",
                         u"finall app title", profile()->GetWeakPtr(),
                         AppRelaunchState::kAppRelaunched);

  // The notification is still shown due to minimal visibility time after the
  // app was relaunched.
  EXPECT_EQ(1u, GetDisplayedNotificationsCount());

  // After two seconds the notification should be gone.
  task_runner_->FastForwardBy(
      base::Seconds(kSecondsToShowNotificationPostAppRelaunch));
  EXPECT_EQ(0u, GetDisplayedNotificationsCount());
}

IN_PROC_BROWSER_TEST_F(WebAppRelaunchNotificationBrowserTest,
                       TwoAppsInParallelShowNotificationsOnRelaunch) {
  ClearAllNotifications();
  notification_observation_.Observe(
      NotificationDisplayServiceFactory::GetForProfile(profile()));

  EXPECT_CALL(
      *this,
      OnNotificationDisplayed(
          AllOf(
              Property(&message_center::Notification::id,
                       Eq("web_app_relaunch_notifier:placeholder_app_id_1")),
              Property(&message_center::Notification::notifier_id,
                       Field(&message_center::NotifierId::id,
                             Eq("web_app_relaunch"))),
              Property(&message_center::Notification::title,
                       Eq(u"Restarting and updating final app title 1")),
              Property(
                  &message_center::Notification::message,
                  Eq(u"Please wait while this application is being updated"))),
          _))
      .Times(1);

  EXPECT_CALL(
      *this,
      OnNotificationDisplayed(
          AllOf(
              Property(&message_center::Notification::id,
                       Eq("web_app_relaunch_notifier:placeholder_app_id_2")),
              Property(&message_center::Notification::notifier_id,
                       Field(&message_center::NotifierId::id,
                             Eq("web_app_relaunch"))),
              Property(&message_center::Notification::title,
                       Eq(u"Restarting and updating final app title 2")),
              Property(
                  &message_center::Notification::message,
                  Eq(u"Please wait while this application is being updated"))),
          _))
      .Times(1);

  NotifyAppRelaunchState("placeholder_app_id_1", "final_app_id_1",
                         u"final app title 1", profile()->GetWeakPtr(),
                         AppRelaunchState::kAppClosingForRelaunch);
  EXPECT_EQ(1u, GetDisplayedNotificationsCount());

  NotifyAppRelaunchState("placeholder_app_id_2", "final_app_id_2",
                         u"final app title 2", profile()->GetWeakPtr(),
                         AppRelaunchState::kAppClosingForRelaunch);
  EXPECT_EQ(2u, GetDisplayedNotificationsCount());

  NotifyAppRelaunchState("placeholder_app_id_1", "final_app_id_1",
                         u"finall app title 1", profile()->GetWeakPtr(),
                         AppRelaunchState::kAppRelaunched);
  EXPECT_EQ(2u, GetDisplayedNotificationsCount());

  // After two seconds the notification should be gone.
  task_runner_->FastForwardBy(
      base::Seconds(kSecondsToShowNotificationPostAppRelaunch));
  EXPECT_EQ(1u, GetDisplayedNotificationsCount());

  NotifyAppRelaunchState("placeholder_app_id_2", "final_app_id_2",
                         u"finall app title 2", profile()->GetWeakPtr(),
                         AppRelaunchState::kAppRelaunched);
  EXPECT_EQ(1u, GetDisplayedNotificationsCount());

  // After two seconds the notification should be gone.
  task_runner_->FastForwardBy(
      base::Seconds(kSecondsToShowNotificationPostAppRelaunch));
  EXPECT_EQ(0u, GetDisplayedNotificationsCount());
}

}  // namespace web_app