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

Side by Side Diff: ash/common/system/chromeos/network/sms_observer_unittest.cc

Issue 2583393002: Send notification to users upon receiving sms messages (Closed)
Patch Set: address comments Created 3 years, 10 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
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ash/common/system/chromeos/network/sms_observer.h"
6 #include "ash/common/shelf/shelf_widget.h"
7 #include "ash/common/shelf/wm_shelf.h"
8 #include "ash/public/interfaces/vpn_list.mojom.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/macros.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/message_center/message_center.h"
15 #include "ui/message_center/notification.h"
16 #include "ui/message_center/notification_list.h"
17
18 namespace ash {
19 namespace test {
20
21 class SmsObserverTest : public AshTestBase {
22 public:
23 SmsObserverTest() {}
24 ~SmsObserverTest() override {}
25
26 SmsObserver* GetSmsObserver(ShelfWidget* shelf_widget) {
27 return shelf_widget->sms_observer_.get();
28 }
29
30 private:
31 DISALLOW_COPY_AND_ASSIGN(SmsObserverTest);
32 };
33
34 base::DictionaryValue* CreateMessage(const char* kDefaultNumber,
stevenjb 2017/02/09 19:12:37 This should return a unique_ptr<> to ensure owners
35 const char* kDefaultMessage,
36 const char* kDefaultTimestamp) {
37 base::DictionaryValue* sms = new base::DictionaryValue();
38 if (kDefaultNumber)
39 sms->SetString("number", kDefaultNumber);
40 if (kDefaultMessage)
41 sms->SetString("text", kDefaultMessage);
42 if (kDefaultTimestamp)
43 sms->SetString("timestamp", kDefaultMessage);
44 return sms;
45 }
46 // Verify if notification is received after receiving a sms message with
47 // number and content.
48 TEST_F(SmsObserverTest, SendTextMessage) {
49 SmsObserver* sms_observer = GetSmsObserver(GetPrimaryShelf()->shelf_widget());
50 EXPECT_EQ(
51 0u,
52 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
53 std::unique_ptr<base::DictionaryValue> sms(
54 CreateMessage("000-000-0000", "FakeSMSClient: Test Message.",
55 "Fri Jun 8 13:26:04 EDT 2016"));
56 EXPECT_EQ(
57 0u,
58 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
59
60 sms_observer->MessageReceived(*sms);
61
62 const message_center::NotificationList::Notifications notifications =
63 message_center::MessageCenter::Get()->GetVisibleNotifications();
64 EXPECT_EQ(1u, notifications.size());
65
66 EXPECT_EQ((*notifications.begin())->title(),
67 base::ASCIIToUTF16("000-000-0000"));
68 EXPECT_EQ((*notifications.begin())->message(),
69 base::ASCIIToUTF16("FakeSMSClient: Test Message."));
70 message_center::MessageCenter::Get()->RemoveAllNotifications(
71 false /* by_user */, message_center::MessageCenter::RemoveType::ALL);
72 EXPECT_EQ(
73 0u,
74 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
75 }
76
77 // Verify if no notification is received if phone number is missing in sms
78 // message.
79 TEST_F(SmsObserverTest, TextMessageMissingNumber) {
80 SmsObserver* sms_observer = GetSmsObserver(GetPrimaryShelf()->shelf_widget());
81 EXPECT_EQ(
82 0u,
83 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
84 std::unique_ptr<base::DictionaryValue> sms(CreateMessage(
85 nullptr, "FakeSMSClient: Test Message.", "Fri Jun 8 13:26:04 EDT 2016"));
86 sms_observer->MessageReceived(*sms);
87 EXPECT_EQ(
88 0u,
89 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
90 }
91
92 // Verify if no notification is received if text body is empty in sms message.
93 TEST_F(SmsObserverTest, TextMessageEmptyText) {
94 SmsObserver* sms_observer = GetSmsObserver(GetPrimaryShelf()->shelf_widget());
95 EXPECT_EQ(
96 0u,
97 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
98 std::unique_ptr<base::DictionaryValue> sms(
99 CreateMessage("000-000-0000", "", "Fri Jun 8 13:26:04 EDT 2016"));
100 sms_observer->MessageReceived(*sms);
101 EXPECT_EQ(
102 0u,
103 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
104 }
105
106 // Verify if no notification is received if the text is missing in sms message.
107 TEST_F(SmsObserverTest, TextMessageMissingText) {
108 SmsObserver* sms_observer = GetSmsObserver(GetPrimaryShelf()->shelf_widget());
109 EXPECT_EQ(
110 0u,
111 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
112 std::unique_ptr<base::DictionaryValue> sms(
113 CreateMessage("000-000-0000", nullptr, "Fri Jun 8 13:26:04 EDT 2016"));
114 ;
115 sms_observer->MessageReceived(*sms);
116 EXPECT_EQ(
117 0u,
118 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
119 }
120
121 // Verify if 2 notification received after receiving 2 sms messages from the
122 // same number.
123 TEST_F(SmsObserverTest, MultipleTextMessages) {
124 SmsObserver* sms_observer = GetSmsObserver(GetPrimaryShelf()->shelf_widget());
125 EXPECT_EQ(
126 0u,
127 message_center::MessageCenter::Get()->GetVisibleNotifications().size());
128 std::unique_ptr<base::DictionaryValue> sms(CreateMessage(
129 "000-000-0000", "first message", "Fri Jun 8 13:26:04 EDT 2016"));
130
131 sms_observer->MessageReceived(*sms);
132
133 sms.reset(CreateMessage("000-000-0000", "second message",
134 "Fri Jun 8 13:26:04 EDT 2016"));
135 sms_observer->MessageReceived(*sms);
136 const message_center::NotificationList::Notifications notifications =
137 message_center::MessageCenter::Get()->GetVisibleNotifications();
138 EXPECT_EQ(2u, notifications.size());
139
140 for (message_center::Notification* iter : notifications) {
141 if (iter->id().find("chrome://network/sms1") != std::string::npos) {
142 EXPECT_EQ(iter->title(), base::ASCIIToUTF16("000-000-0000"));
143 EXPECT_EQ(iter->message(), base::ASCIIToUTF16("first message"));
144 } else if (iter->id().find("chrome://network/sms2") != std::string::npos) {
145 EXPECT_EQ(iter->title(), base::ASCIIToUTF16("000-000-0000"));
146 EXPECT_EQ(iter->message(), base::ASCIIToUTF16("second message"));
147 } else {
148 ASSERT_TRUE(false);
149 }
150 }
151 }
152
153 } // namespace test
154 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/network/sms_observer.cc ('k') | ash/common/system/chromeos/network/tray_sms.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698