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

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: 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
22 namespace { 24 namespace {
23 25
26 using testing::_;
27 using testing::Return;
28 using testing::StrictMock;
Peter Beverloo 2017/05/04 14:43:06 nit: move outside of the anonymous namespace
Tom (Use chromium acct) 2017/05/04 18:15:36 Done.
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 dbus::MethodCall* method_call = arg0;
49 dbus::MessageReader reader(method_call);
50 std::string str;
51 uint32_t uint32;
52 int32_t int32;
53 EXPECT_TRUE(reader.PopString(&str));
Peter Beverloo 2017/05/04 14:43:06 nit: this method expects data in a very particular
Tom (Use chromium acct) 2017/05/04 18:15:36 Done.
54 EXPECT_TRUE(reader.PopUint32(&uint32));
55 EXPECT_TRUE(reader.PopString(&str));
56 EXPECT_TRUE(reader.PopString(&str));
57 EXPECT_TRUE(reader.PopString(&str));
58
59 {
60 dbus::MessageReader actions_reader(nullptr);
61 EXPECT_TRUE(reader.PopArray(&actions_reader));
62 while (actions_reader.HasMoreData()) {
63 // Actions come in pairs.
64 EXPECT_TRUE(actions_reader.PopString(&str));
65 EXPECT_TRUE(actions_reader.PopString(&str));
66 }
67 }
68
69 {
70 dbus::MessageReader hints_reader(nullptr);
71 EXPECT_TRUE(reader.PopArray(&hints_reader));
72 while (hints_reader.HasMoreData()) {
73 dbus::MessageReader dict_entry_reader(nullptr);
74 EXPECT_TRUE(hints_reader.PopDictEntry(&dict_entry_reader));
75 EXPECT_TRUE(dict_entry_reader.PopString(&str));
76 dbus::MessageReader variant_reader(nullptr);
77 EXPECT_TRUE(dict_entry_reader.PopVariant(&variant_reader));
78 EXPECT_FALSE(dict_entry_reader.HasMoreData());
79 }
80 }
81
82 EXPECT_TRUE(reader.PopInt32(&int32));
83 EXPECT_FALSE(reader.HasMoreData());
84
85 dbus::Response* response = dbus::Response::CreateEmpty().release();
86 dbus::MessageWriter writer(response);
87 writer.AppendUint32(id);
88 return response;
89 }
90
91 ACTION(OnCloseNotification) {
92 dbus::MethodCall* method_call = arg0;
93 dbus::MessageReader reader(method_call);
94 uint32_t uint32;
95 EXPECT_TRUE(reader.PopUint32(&uint32));
96 EXPECT_FALSE(reader.HasMoreData());
97
98 return dbus::Response::CreateEmpty().release();
99 }
100
101 // Matches a method call to the specified dbus target.
102 MATCHER_P(Calls, member, "") {
103 return arg->GetMember() == member;
104 }
105
106 Notification CreateNotification(const char* title,
107 const char* body,
108 const char* origin) {
109 message_center::RichNotificationData optional_fields;
110 GURL url = GURL(origin);
111 return Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
112 base::UTF8ToUTF16(title), base::UTF8ToUTF16(body),
113 gfx::Image(), message_center::NotifierId(url),
114 base::UTF8ToUTF16("Notifier's Name"), url, "id1",
Peter Beverloo 2017/05/04 14:43:06 Note that including "id1" (or any static value) me
Tom (Use chromium acct) 2017/05/04 18:15:36 Done.
115 optional_fields, new MockNotificationDelegate("id1"));
116 }
117
41 } // namespace 118 } // namespace
42 119
43 class NotificationPlatformBridgeLinuxTest : public testing::Test { 120 class NotificationPlatformBridgeLinuxTest : public testing::Test {
44 public: 121 public:
45 NotificationPlatformBridgeLinuxTest() = default; 122 NotificationPlatformBridgeLinuxTest() = default;
46 ~NotificationPlatformBridgeLinuxTest() override = default; 123 ~NotificationPlatformBridgeLinuxTest() override = default;
47 124
48 void SetUp() override { 125 void SetUp() override {
49 mock_bus_ = new dbus::MockBus(dbus::Bus::Options()); 126 mock_bus_ = new dbus::MockBus(dbus::Bus::Options());
50 mock_notification_proxy_ = new testing::StrictMock<dbus::MockObjectProxy>( 127 mock_notification_proxy_ = new StrictMock<dbus::MockObjectProxy>(
51 mock_bus_.get(), kFreedesktopNotificationsName, 128 mock_bus_.get(), kFreedesktopNotificationsName,
52 dbus::ObjectPath(kFreedesktopNotificationsPath)); 129 dbus::ObjectPath(kFreedesktopNotificationsPath));
53 130
54 EXPECT_CALL(*mock_bus_.get(), 131 EXPECT_CALL(*mock_bus_.get(),
55 GetObjectProxy(kFreedesktopNotificationsName, 132 GetObjectProxy(kFreedesktopNotificationsName,
56 dbus::ObjectPath(kFreedesktopNotificationsPath))) 133 dbus::ObjectPath(kFreedesktopNotificationsPath)))
57 .WillOnce(testing::Return(mock_notification_proxy_.get())); 134 .WillOnce(Return(mock_notification_proxy_.get()));
58 135
59 EXPECT_CALL(*mock_notification_proxy_.get(), 136 EXPECT_CALL(*mock_notification_proxy_.get(),
60 MockCallMethodAndBlock(testing::_, testing::_)) 137 MockCallMethodAndBlock(Calls("GetCapabilities"), _))
61 .WillOnce(OnGetCapabilities(std::vector<std::string>())); 138 .WillOnce(OnGetCapabilities(std::vector<std::string>()));
62 139
63 EXPECT_CALL(*mock_notification_proxy_.get(), 140 EXPECT_CALL(
64 ConnectToSignal(kFreedesktopNotificationsName, "ActionInvoked", 141 *mock_notification_proxy_.get(),
65 testing::_, testing::_)) 142 ConnectToSignal(kFreedesktopNotificationsName, "ActionInvoked", _, _))
66 .WillOnce(RegisterSignalCallback(&action_invoked_callback_)); 143 .WillOnce(RegisterSignalCallback(&action_invoked_callback_));
67 144
68 EXPECT_CALL(*mock_notification_proxy_.get(), 145 EXPECT_CALL(*mock_notification_proxy_.get(),
69 ConnectToSignal(kFreedesktopNotificationsName, 146 ConnectToSignal(kFreedesktopNotificationsName,
70 "NotificationClosed", testing::_, testing::_)) 147 "NotificationClosed", _, _))
71 .WillOnce(RegisterSignalCallback(&notification_closed_callback_)); 148 .WillOnce(RegisterSignalCallback(&notification_closed_callback_));
72 149
73 notification_bridge_linux_ = 150 notification_bridge_linux_ =
74 base::WrapUnique(new NotificationPlatformBridgeLinux(mock_bus_)); 151 base::WrapUnique(new NotificationPlatformBridgeLinux(mock_bus_));
75 } 152 }
76 153
77 void TearDown() override { 154 void TearDown() override {
78 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()); 155 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock());
79 notification_bridge_linux_->CleanUp(); 156 notification_bridge_linux_->CleanUp();
80 content::RunAllBlockingPoolTasksUntilIdle(); 157 content::RunAllBlockingPoolTasksUntilIdle();
(...skipping 11 matching lines...) Expand all
92 base::Callback<void(dbus::Signal*)> action_invoked_callback_; 169 base::Callback<void(dbus::Signal*)> action_invoked_callback_;
93 base::Callback<void(dbus::Signal*)> notification_closed_callback_; 170 base::Callback<void(dbus::Signal*)> notification_closed_callback_;
94 171
95 std::unique_ptr<NotificationPlatformBridgeLinux> notification_bridge_linux_; 172 std::unique_ptr<NotificationPlatformBridgeLinux> notification_bridge_linux_;
96 173
97 private: 174 private:
98 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeLinuxTest); 175 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeLinuxTest);
99 }; 176 };
100 177
101 TEST_F(NotificationPlatformBridgeLinuxTest, SetUpAndTearDown) {} 178 TEST_F(NotificationPlatformBridgeLinuxTest, SetUpAndTearDown) {}
179
180 TEST_F(NotificationPlatformBridgeLinuxTest, NotifyAndCloseFormat) {
181 EXPECT_CALL(*mock_notification_proxy_.get(),
182 MockCallMethodAndBlock(Calls("Notify"), _))
183 .WillOnce(OnNotify(1));
184 notification_bridge_linux_->Display(NotificationCommon::PERSISTENT, "", "",
185 false, CreateNotification("", "", ""));
186
187 EXPECT_CALL(*mock_notification_proxy_.get(),
188 MockCallMethodAndBlock(Calls("CloseNotification"), _))
189 .WillOnce(OnCloseNotification());
190 notification_bridge_linux_->Close("", "");
191 }
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