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

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

Issue 2690543003: 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 2017 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
7 #include "ash/common/shelf/shelf_widget.h"
8 #include "ash/common/shelf/wm_shelf.h"
9 #include "ash/public/interfaces/vpn_list.mojom.h"
10 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h"
12 #include "base/macros.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/message_center/message_center.h"
17 #include "ui/message_center/notification.h"
18 #include "ui/message_center/notification_list.h"
19
20 using message_center::MessageCenter;
21
22 std::unique_ptr<base::DictionaryValue> CreateMessage(
James Cook 2017/02/14 02:04:38 anonymous namespace again
yiyix 2017/02/15 19:41:39 Done.
23 const char* kDefaultMessage = "FakeSMSClient: Test Message.",
24 const char* kDefaultNumber = "000-000-0000",
25 const char* kDefaultTimestamp = "Fri Jun 8 13:26:04 EDT 2016") {
26 std::unique_ptr<base::DictionaryValue> sms =
27 base::MakeUnique<base::DictionaryValue>();
28 if (kDefaultNumber)
29 sms->SetString("number", kDefaultNumber);
30 if (kDefaultMessage)
31 sms->SetString("text", kDefaultMessage);
32 if (kDefaultTimestamp)
33 sms->SetString("timestamp", kDefaultMessage);
34 return sms;
35 }
36
37 namespace ash {
38 namespace test {
James Cook 2017/02/14 02:04:38 super nit: Don't use this namespace. "namespace te
yiyix 2017/02/15 19:41:39 Done.
39
40 class SmsObserverTest : public AshTestBase {
41 public:
42 SmsObserverTest() {}
43 ~SmsObserverTest() override {}
44
45 SmsObserver* GetSmsObserver() {
46 Shell* shell = Shell::GetInstance();
47 return shell->sms_observer_.get();
James Cook 2017/02/14 02:04:38 nit: just inline Shell::GetInstance()->sms_observe
yiyix 2017/02/15 19:41:39 sorry, i was experimenting different ways to get s
48 }
49
50 private:
51 DISALLOW_COPY_AND_ASSIGN(SmsObserverTest);
52 };
53
54 // Verify if notification is received after receiving a sms message with
55 // number and content.
56 TEST_F(SmsObserverTest, SendTextMessage) {
57 SmsObserver* sms_observer = GetSmsObserver();
58 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
59
60 std::unique_ptr<base::DictionaryValue> sms(CreateMessage());
61 sms_observer->MessageReceived(*sms);
62
63 const message_center::NotificationList::Notifications notifications =
64 MessageCenter::Get()->GetVisibleNotifications();
65 EXPECT_EQ(1u, notifications.size());
66
67 EXPECT_EQ(base::ASCIIToUTF16("000-000-0000"),
68 (*notifications.begin())->title());
69 EXPECT_EQ(base::ASCIIToUTF16("FakeSMSClient: Test Message."),
70 (*notifications.begin())->message());
71 MessageCenter::Get()->RemoveAllNotifications(false /* by_user */,
72 MessageCenter::RemoveType::ALL);
73 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
74 }
75
76 // Verify if no notification is received if phone number is missing in sms
77 // message.
78 TEST_F(SmsObserverTest, TextMessageMissingNumber) {
79 SmsObserver* sms_observer = GetSmsObserver();
80 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
81
82 std::unique_ptr<base::DictionaryValue> sms(
83 CreateMessage("FakeSMSClient: Test Message.", nullptr));
84 sms_observer->MessageReceived(*sms);
85 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
86 }
87
88 // Verify if no notification is received if text body is empty in sms message.
89 TEST_F(SmsObserverTest, TextMessageEmptyText) {
90 SmsObserver* sms_observer = GetSmsObserver();
91 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
92
93 std::unique_ptr<base::DictionaryValue> sms(CreateMessage(""));
94 sms_observer->MessageReceived(*sms);
95 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
96 }
97
98 // Verify if no notification is received if the text is missing in sms message.
99 TEST_F(SmsObserverTest, TextMessageMissingText) {
100 SmsObserver* sms_observer = GetSmsObserver();
101 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
102 std::unique_ptr<base::DictionaryValue> sms(CreateMessage(nullptr));
103 sms_observer->MessageReceived(*sms);
104 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
105 }
106
107 // Verify if 2 notification received after receiving 2 sms messages from the
108 // same number.
109 TEST_F(SmsObserverTest, MultipleTextMessages) {
110 SmsObserver* sms_observer = GetSmsObserver();
111 EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
112
113 std::unique_ptr<base::DictionaryValue> sms(CreateMessage("first message"));
114 sms_observer->MessageReceived(*sms);
115 std::unique_ptr<base::DictionaryValue> sms2(CreateMessage("second message"));
116 sms_observer->MessageReceived(*sms2);
117 const message_center::NotificationList::Notifications notifications =
118 MessageCenter::Get()->GetVisibleNotifications();
119 EXPECT_EQ(2u, notifications.size());
120
121 for (message_center::Notification* iter : notifications) {
122 if (iter->id().find("chrome://network/sms1") != std::string::npos) {
123 EXPECT_EQ(base::ASCIIToUTF16("000-000-0000"), iter->title());
124 EXPECT_EQ(base::ASCIIToUTF16("first message"), iter->message());
125 } else if (iter->id().find("chrome://network/sms2") != std::string::npos) {
126 EXPECT_EQ(base::ASCIIToUTF16("000-000-0000"), iter->title());
127 EXPECT_EQ(base::ASCIIToUTF16("second message"), iter->message());
128 } else {
129 ASSERT_TRUE(false);
130 }
131 }
132 }
133
134 } // namespace test
135 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698