Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: chrome/browser/notifications/notification_platform_bridge_linux_unittest.cc

Issue 2857293002: Linux native notifications: Add notify and close tests (Closed)
Patch Set: Rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/notifications/notification_platform_bridge_linux.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/notification_platform_bridge_linux.h" 5 #include "chrome/browser/notifications/notification_platform_bridge_linux.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/notifications/notification.h" 15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_test_util.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "content/public/test/test_utils.h" 18 #include "content/public/test/test_utils.h"
17 #include "dbus/mock_bus.h" 19 #include "dbus/mock_bus.h"
18 #include "dbus/mock_object_proxy.h" 20 #include "dbus/mock_object_proxy.h"
19 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 23
24 using testing::_;
25 using testing::Return;
26 using testing::StrictMock;
27
22 namespace { 28 namespace {
23 29
24 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; 30 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications";
25 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; 31 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications";
26 32
27 ACTION_P(RegisterSignalCallback, callback_addr) { 33 ACTION_P(RegisterSignalCallback, callback_addr) {
28 *callback_addr = arg2; 34 *callback_addr = arg2;
29 arg3.Run("" /* interface_name */, "" /* signal_name */, true /* success */); 35 arg3.Run("" /* interface_name */, "" /* signal_name */, true /* success */);
30 } 36 }
31 37
32 ACTION_P(OnGetCapabilities, capabilities) { 38 ACTION_P(OnGetCapabilities, capabilities) {
33 // MockObjectProxy::CallMethodAndBlock will wrap the return value in 39 // MockObjectProxy::CallMethodAndBlock will wrap the return value in
34 // a unique_ptr. 40 // a unique_ptr.
35 dbus::Response* response = dbus::Response::CreateEmpty().release(); 41 dbus::Response* response = dbus::Response::CreateEmpty().release();
36 dbus::MessageWriter writer(response); 42 dbus::MessageWriter writer(response);
37 writer.AppendArrayOfStrings(capabilities); 43 writer.AppendArrayOfStrings(capabilities);
38 return response; 44 return response;
39 } 45 }
40 46
47 ACTION_P(OnNotify, id) {
48 // The "Notify" message must have type (susssasa{sv}i).
49 // https://developer.gnome.org/notification-spec/#command-notify
50 dbus::MethodCall* method_call = arg0;
51 dbus::MessageReader reader(method_call);
52 std::string str;
53 uint32_t uint32;
54 int32_t int32;
55 EXPECT_TRUE(reader.PopString(&str));
56 EXPECT_TRUE(reader.PopUint32(&uint32));
57 EXPECT_TRUE(reader.PopString(&str));
58 EXPECT_TRUE(reader.PopString(&str));
59 EXPECT_TRUE(reader.PopString(&str));
60
61 {
62 dbus::MessageReader actions_reader(nullptr);
63 EXPECT_TRUE(reader.PopArray(&actions_reader));
64 while (actions_reader.HasMoreData()) {
65 // Actions come in pairs.
66 EXPECT_TRUE(actions_reader.PopString(&str));
67 EXPECT_TRUE(actions_reader.PopString(&str));
68 }
69 }
70
71 {
72 dbus::MessageReader hints_reader(nullptr);
73 EXPECT_TRUE(reader.PopArray(&hints_reader));
74 while (hints_reader.HasMoreData()) {
75 dbus::MessageReader dict_entry_reader(nullptr);
76 EXPECT_TRUE(hints_reader.PopDictEntry(&dict_entry_reader));
77 EXPECT_TRUE(dict_entry_reader.PopString(&str));
78 dbus::MessageReader variant_reader(nullptr);
79 EXPECT_TRUE(dict_entry_reader.PopVariant(&variant_reader));
80 EXPECT_FALSE(dict_entry_reader.HasMoreData());
81 }
82 }
83
84 EXPECT_TRUE(reader.PopInt32(&int32));
85 EXPECT_FALSE(reader.HasMoreData());
86
87 dbus::Response* response = dbus::Response::CreateEmpty().release();
88 dbus::MessageWriter writer(response);
89 writer.AppendUint32(id);
90 return response;
91 }
92
93 ACTION(OnCloseNotification) {
94 // The "CloseNotification" message must have type (u).
95 // https://developer.gnome.org/notification-spec/#command-close-notification
96 dbus::MethodCall* method_call = arg0;
97 dbus::MessageReader reader(method_call);
98 uint32_t uint32;
99 EXPECT_TRUE(reader.PopUint32(&uint32));
100 EXPECT_FALSE(reader.HasMoreData());
101
102 return dbus::Response::CreateEmpty().release();
103 }
104
105 // Matches a method call to the specified dbus target.
106 MATCHER_P(Calls, member, "") {
107 return arg->GetMember() == member;
108 }
109
110 Notification CreateNotification(const char* id,
111 const char* title,
112 const char* body,
113 const char* origin) {
114 message_center::RichNotificationData optional_fields;
115 GURL url = GURL(origin);
116 return Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
117 base::UTF8ToUTF16(title), base::UTF8ToUTF16(body),
118 gfx::Image(), message_center::NotifierId(url),
119 base::UTF8ToUTF16("Notifier's Name"), url, id,
120 optional_fields, new MockNotificationDelegate(id));
121 }
122
41 } // namespace 123 } // namespace
42 124
43 class NotificationPlatformBridgeLinuxTest : public testing::Test { 125 class NotificationPlatformBridgeLinuxTest : public testing::Test {
44 public: 126 public:
45 NotificationPlatformBridgeLinuxTest() = default; 127 NotificationPlatformBridgeLinuxTest() = default;
46 ~NotificationPlatformBridgeLinuxTest() override = default; 128 ~NotificationPlatformBridgeLinuxTest() override = default;
47 129
48 void SetUp() override { 130 void SetUp() override {
49 mock_bus_ = new dbus::MockBus(dbus::Bus::Options()); 131 mock_bus_ = new dbus::MockBus(dbus::Bus::Options());
50 mock_notification_proxy_ = new testing::StrictMock<dbus::MockObjectProxy>( 132 mock_notification_proxy_ = new StrictMock<dbus::MockObjectProxy>(
51 mock_bus_.get(), kFreedesktopNotificationsName, 133 mock_bus_.get(), kFreedesktopNotificationsName,
52 dbus::ObjectPath(kFreedesktopNotificationsPath)); 134 dbus::ObjectPath(kFreedesktopNotificationsPath));
53 135
54 EXPECT_CALL(*mock_bus_.get(), 136 EXPECT_CALL(*mock_bus_.get(),
55 GetObjectProxy(kFreedesktopNotificationsName, 137 GetObjectProxy(kFreedesktopNotificationsName,
56 dbus::ObjectPath(kFreedesktopNotificationsPath))) 138 dbus::ObjectPath(kFreedesktopNotificationsPath)))
57 .WillOnce(testing::Return(mock_notification_proxy_.get())); 139 .WillOnce(Return(mock_notification_proxy_.get()));
58 140
59 EXPECT_CALL(*mock_notification_proxy_.get(), 141 EXPECT_CALL(*mock_notification_proxy_.get(),
60 MockCallMethodAndBlock(testing::_, testing::_)) 142 MockCallMethodAndBlock(Calls("GetCapabilities"), _))
61 .WillOnce(OnGetCapabilities(std::vector<std::string>())); 143 .WillOnce(OnGetCapabilities(std::vector<std::string>()));
62 144
63 EXPECT_CALL(*mock_notification_proxy_.get(), 145 EXPECT_CALL(
64 ConnectToSignal(kFreedesktopNotificationsName, "ActionInvoked", 146 *mock_notification_proxy_.get(),
65 testing::_, testing::_)) 147 ConnectToSignal(kFreedesktopNotificationsName, "ActionInvoked", _, _))
66 .WillOnce(RegisterSignalCallback(&action_invoked_callback_)); 148 .WillOnce(RegisterSignalCallback(&action_invoked_callback_));
67 149
68 EXPECT_CALL(*mock_notification_proxy_.get(), 150 EXPECT_CALL(*mock_notification_proxy_.get(),
69 ConnectToSignal(kFreedesktopNotificationsName, 151 ConnectToSignal(kFreedesktopNotificationsName,
70 "NotificationClosed", testing::_, testing::_)) 152 "NotificationClosed", _, _))
71 .WillOnce(RegisterSignalCallback(&notification_closed_callback_)); 153 .WillOnce(RegisterSignalCallback(&notification_closed_callback_));
72 154
73 notification_bridge_linux_ = 155 notification_bridge_linux_ =
74 base::WrapUnique(new NotificationPlatformBridgeLinux(mock_bus_)); 156 base::WrapUnique(new NotificationPlatformBridgeLinux(mock_bus_));
75 } 157 }
76 158
77 void TearDown() override { 159 void TearDown() override {
78 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()); 160 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock());
79 notification_bridge_linux_->CleanUp(); 161 notification_bridge_linux_->CleanUp();
80 content::RunAllBlockingPoolTasksUntilIdle(); 162 content::RunAllBlockingPoolTasksUntilIdle();
(...skipping 11 matching lines...) Expand all
92 base::Callback<void(dbus::Signal*)> action_invoked_callback_; 174 base::Callback<void(dbus::Signal*)> action_invoked_callback_;
93 base::Callback<void(dbus::Signal*)> notification_closed_callback_; 175 base::Callback<void(dbus::Signal*)> notification_closed_callback_;
94 176
95 std::unique_ptr<NotificationPlatformBridgeLinux> notification_bridge_linux_; 177 std::unique_ptr<NotificationPlatformBridgeLinux> notification_bridge_linux_;
96 178
97 private: 179 private:
98 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeLinuxTest); 180 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeLinuxTest);
99 }; 181 };
100 182
101 TEST_F(NotificationPlatformBridgeLinuxTest, SetUpAndTearDown) {} 183 TEST_F(NotificationPlatformBridgeLinuxTest, SetUpAndTearDown) {}
184
185 TEST_F(NotificationPlatformBridgeLinuxTest, NotifyAndCloseFormat) {
186 EXPECT_CALL(*mock_notification_proxy_.get(),
187 MockCallMethodAndBlock(Calls("Notify"), _))
188 .WillOnce(OnNotify(1));
189 notification_bridge_linux_->Display(NotificationCommon::PERSISTENT, "", "",
190 false,
191 CreateNotification("id1", "", "", ""));
192
193 EXPECT_CALL(*mock_notification_proxy_.get(),
194 MockCallMethodAndBlock(Calls("CloseNotification"), _))
195 .WillOnce(OnCloseNotification());
196 notification_bridge_linux_->Close("", "");
197 }
OLDNEW
« no previous file with comments | « chrome/browser/notifications/notification_platform_bridge_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698