sms_observer_unittest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "ash/system/network/sms_observer.h"
  5. #include <memory>
  6. #include "ash/shell.h"
  7. #include "ash/test/ash_test_base.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/base/l10n/l10n_util.h"
  11. #include "ui/message_center/message_center.h"
  12. #include "ui/message_center/notification_list.h"
  13. #include "ui/message_center/public/cpp/notification.h"
  14. using message_center::MessageCenter;
  15. namespace ash {
  16. namespace {
  17. std::unique_ptr<base::DictionaryValue> CreateMessage(
  18. const char* kDefaultMessage = "FakeSMSClient: \xF0\x9F\x98\x8A",
  19. const char* kDefaultNumber = "000-000-0000",
  20. const char* kDefaultTimestamp = "Fri Jun 8 13:26:04 EDT 2016") {
  21. std::unique_ptr<base::DictionaryValue> sms =
  22. std::make_unique<base::DictionaryValue>();
  23. if (kDefaultNumber)
  24. sms->SetStringKey("number", kDefaultNumber);
  25. if (kDefaultMessage)
  26. sms->SetStringKey("text", kDefaultMessage);
  27. if (kDefaultTimestamp)
  28. sms->SetStringKey("timestamp", kDefaultMessage);
  29. return sms;
  30. }
  31. } // namespace
  32. class SmsObserverTest : public AshTestBase {
  33. public:
  34. SmsObserverTest() = default;
  35. SmsObserverTest(const SmsObserverTest&) = delete;
  36. SmsObserverTest& operator=(const SmsObserverTest&) = delete;
  37. ~SmsObserverTest() override = default;
  38. SmsObserver* GetSmsObserver() { return Shell::Get()->sms_observer_.get(); }
  39. };
  40. // Verify if notification is received after receiving a sms message with
  41. // number and content.
  42. TEST_F(SmsObserverTest, SendTextMessage) {
  43. SmsObserver* sms_observer = GetSmsObserver();
  44. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  45. std::unique_ptr<base::DictionaryValue> sms(CreateMessage());
  46. sms_observer->MessageReceived(*sms);
  47. const message_center::NotificationList::Notifications notifications =
  48. MessageCenter::Get()->GetVisibleNotifications();
  49. EXPECT_EQ(1u, notifications.size());
  50. EXPECT_EQ(u"000-000-0000", (*notifications.begin())->title());
  51. EXPECT_EQ(u"FakeSMSClient: 😊", (*notifications.begin())->message());
  52. MessageCenter::Get()->RemoveAllNotifications(false /* by_user */,
  53. MessageCenter::RemoveType::ALL);
  54. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  55. }
  56. // Verify if no notification is received if phone number is missing in sms
  57. // message.
  58. TEST_F(SmsObserverTest, TextMessageMissingNumber) {
  59. SmsObserver* sms_observer = GetSmsObserver();
  60. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  61. std::unique_ptr<base::DictionaryValue> sms(
  62. CreateMessage("FakeSMSClient: Test Message.", nullptr));
  63. sms_observer->MessageReceived(*sms);
  64. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  65. }
  66. // Verify if no notification is received if text body is empty in sms message.
  67. TEST_F(SmsObserverTest, TextMessageEmptyText) {
  68. SmsObserver* sms_observer = GetSmsObserver();
  69. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  70. std::unique_ptr<base::DictionaryValue> sms(CreateMessage(""));
  71. sms_observer->MessageReceived(*sms);
  72. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  73. }
  74. // Verify if no notification is received if the text is missing in sms message.
  75. TEST_F(SmsObserverTest, TextMessageMissingText) {
  76. SmsObserver* sms_observer = GetSmsObserver();
  77. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  78. std::unique_ptr<base::DictionaryValue> sms(CreateMessage(""));
  79. sms_observer->MessageReceived(*sms);
  80. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  81. }
  82. // Verify if 2 notification received after receiving 2 sms messages from the
  83. // same number.
  84. TEST_F(SmsObserverTest, MultipleTextMessages) {
  85. SmsObserver* sms_observer = GetSmsObserver();
  86. EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
  87. std::unique_ptr<base::DictionaryValue> sms(CreateMessage("first message"));
  88. sms_observer->MessageReceived(*sms);
  89. std::unique_ptr<base::DictionaryValue> sms2(CreateMessage("second message"));
  90. sms_observer->MessageReceived(*sms2);
  91. const message_center::NotificationList::Notifications notifications =
  92. MessageCenter::Get()->GetVisibleNotifications();
  93. EXPECT_EQ(2u, notifications.size());
  94. for (message_center::Notification* iter : notifications) {
  95. if (iter->id().find("chrome://network/sms1") != std::string::npos) {
  96. EXPECT_EQ(u"000-000-0000", iter->title());
  97. EXPECT_EQ(u"first message", iter->message());
  98. } else if (iter->id().find("chrome://network/sms2") != std::string::npos) {
  99. EXPECT_EQ(u"000-000-0000", iter->title());
  100. EXPECT_EQ(u"second message", iter->message());
  101. } else {
  102. ASSERT_TRUE(false);
  103. }
  104. }
  105. }
  106. } // namespace ash