File: notification_ui_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (120 lines) | stat: -rw-r--r-- 4,526 bytes parent folder | download | duplicates (3)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_timeouts.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification_test_util.h"
#include "chrome/browser/notifications/notification_ui_manager_impl.h"
#include "chrome/browser/notifications/profile_notification.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_types.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/message_center/public/cpp/notifier_id.h"

namespace message_center {

class NotificationUIManagerTest : public BrowserWithTestWindowTest {
 public:
  NotificationUIManagerTest() = default;

 protected:
  void SetUp() override {
    MessageCenter::Initialize();

    BrowserWithTestWindowTest::SetUp();
    message_center_ = MessageCenter::Get();
    notification_manager()->ResetUiControllerForTest();
  }

  void TearDown() override {
    BrowserWithTestWindowTest::TearDown();
    MessageCenter::Shutdown();
  }

  NotificationUIManagerImpl* notification_manager() {
    return (NotificationUIManagerImpl*)
        g_browser_process->notification_ui_manager();
  }

  MessageCenter* message_center() { return message_center_; }

  const Notification GetANotification(const std::string& id) {
    return Notification(
        message_center::NOTIFICATION_TYPE_SIMPLE, id, std::u16string(),
        std::u16string(), ui::ImageModel(), std::u16string(),
        GURL("chrome-extension://adflkjsdflkdsfdsflkjdsflkdjfs"),
        NotifierId(NotifierType::APPLICATION, "adflkjsdflkdsfdsflkjdsflkdjfs"),
        message_center::RichNotificationData(),
        new message_center::NotificationDelegate());
  }

 private:
  raw_ptr<MessageCenter, DanglingUntriaged> message_center_;
};

TEST_F(NotificationUIManagerTest, SetupNotificationManager) {
  notification_manager()->Add(GetANotification("test"), profile());
}

TEST_F(NotificationUIManagerTest, AddNotificationOnShutdown) {
  EXPECT_TRUE(message_center()->NotificationCount() == 0);
  notification_manager()->Add(GetANotification("test"), profile());
  EXPECT_TRUE(message_center()->NotificationCount() == 1);

  // Verify the number of notifications does not increase when trying to add a
  // notifcation on shutdown.
  notification_manager()->StartShutdown();
  EXPECT_TRUE(message_center()->NotificationCount() == 0);
  notification_manager()->Add(GetANotification("test2"), profile());
  EXPECT_TRUE(message_center()->NotificationCount() == 0);

  base::RunLoop().RunUntilIdle();
}

TEST_F(NotificationUIManagerTest, UpdateNotification) {
  EXPECT_TRUE(message_center()->NotificationCount() == 0);
  notification_manager()->Add(GetANotification("test"), profile());
  EXPECT_TRUE(message_center()->NotificationCount() == 1);
  ASSERT_TRUE(
      notification_manager()->Update(GetANotification("test"), profile()));
  EXPECT_TRUE(message_center()->NotificationCount() == 1);

  base::RunLoop().RunUntilIdle();
}

// Regression test for crbug.com/767868
TEST_F(NotificationUIManagerTest, GetAllIdsReturnsOriginalId) {
  EXPECT_TRUE(message_center()->NotificationCount() == 0);
  notification_manager()->Add(GetANotification("test"), profile());
  std::set<std::string> ids = notification_manager()->GetAllIdsByProfile(
      ProfileNotification::GetProfileID(profile()));
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(*ids.begin(), "test");
}

TEST_F(NotificationUIManagerTest, GetAllIdsByOriginReturnsOriginalId) {
  EXPECT_TRUE(message_center()->NotificationCount() == 0);
  notification_manager()->Add(GetANotification("test"), profile());
  std::set<std::string> ids =
      notification_manager()->GetAllIdsByProfileAndOrigin(
          ProfileNotification::GetProfileID(profile()),
          GURL("chrome-extension://adflkjsdflkdsfdsflkjdsflkdjfs"));
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(*ids.begin(), "test");
}

}  // namespace message_center