1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/network/sms_observer.h"
#include <memory>
#include <optional>
#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ash/components/network/fake_network_metadata_store.h"
#include "chromeos/ash/components/network/metrics/cellular_network_metrics_logger.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_sms_handler.h"
#include "chromeos/ash/components/network/text_message_suppression_state.h"
#include "components/onc/onc_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/public/cpp/notification.h"
using message_center::MessageCenter;
namespace ash {
namespace {
constexpr char kTestGuid[] = "TestGuid";
std::optional<const std::string> GetStringOptional(const char* text) {
if (text) {
return std::make_optional<const std::string>(text);
}
return std::nullopt;
}
} // namespace
struct SmsObserverTestCase {
std::string test_name;
bool use_suppress_text_message_flag;
};
class SmsObserverTest : public AshTestBase {
public:
SmsObserverTest() = default;
SmsObserverTest(const SmsObserverTest&) = delete;
SmsObserverTest& operator=(const SmsObserverTest&) = delete;
~SmsObserverTest() override = default;
SmsObserver* GetSmsObserver() { return Shell::Get()->sms_observer_.get(); }
void SetUp() override {
AshTestBase::SetUp();
test_helper_ = std::make_unique<NetworkHandlerTestHelper>();
NetworkHandler::Get()->text_message_provider()->SetNetworkMetadataStore(
&network_metadata_store_);
}
void SimulateMessageReceived(
const char* kDefaultMessage = "FakeSMSClient: \xF0\x9F\x98\x8A",
const char* kDefaultNumber = "000-000-0000",
const char* kDefaultTimestamp = "Fri Jun 8 13:26:04 EDT 2016") {
TextMessageData message_data(GetStringOptional(kDefaultNumber),
GetStringOptional(kDefaultMessage),
GetStringOptional(kDefaultTimestamp));
GetSmsObserver()->MessageReceived(kTestGuid, message_data);
}
NetworkMetadataStore* network_metadata_store() {
return &network_metadata_store_;
}
ManagedNetworkConfigurationHandler* managed_network_configuration_handler() {
return NetworkHandler::Get()->managed_network_configuration_handler();
}
private:
base::test::ScopedFeatureList features_;
std::unique_ptr<NetworkHandlerTestHelper> test_helper_;
FakeNetworkMetadataStore network_metadata_store_;
};
// Verify if notification is received after receiving a sms message with
// number and content.
TEST_F(SmsObserverTest, SendTextMessage) {
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
SimulateMessageReceived();
const message_center::NotificationList::Notifications notifications =
MessageCenter::Get()->GetVisibleNotifications();
EXPECT_EQ(1u, notifications.size());
EXPECT_EQ(u"000-000-0000", (*notifications.begin())->title());
EXPECT_EQ(u"FakeSMSClient: 😊", (*notifications.begin())->message());
MessageCenter::Get()->RemoveAllNotifications(false /* by_user */,
MessageCenter::RemoveType::ALL);
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
}
// Verify if no notification is received if phone number is missing in sms
// message.
TEST_F(SmsObserverTest, TextMessageMissingNumber) {
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
SimulateMessageReceived("FakeSMSClient: Test Message.", nullptr);
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
}
// Verify if no notification is received if text body is empty in sms message.
TEST_F(SmsObserverTest, TextMessageEmptyText) {
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
SimulateMessageReceived("");
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
}
// Verify if no notification is received if the text is missing in sms message.
TEST_F(SmsObserverTest, TextMessageMissingText) {
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
SimulateMessageReceived("");
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
}
// Verify if 2 notification received after receiving 2 sms messages from the
// same number.
TEST_F(SmsObserverTest, MultipleTextMessages) {
EXPECT_EQ(0u, MessageCenter::Get()->GetVisibleNotifications().size());
SimulateMessageReceived("first message");
SimulateMessageReceived("second message");
const message_center::NotificationList::Notifications notifications =
MessageCenter::Get()->GetVisibleNotifications();
EXPECT_EQ(2u, notifications.size());
for (message_center::Notification* iter : notifications) {
if (iter->id().find("chrome://network/sms1") != std::string::npos) {
EXPECT_EQ(u"000-000-0000", iter->title());
EXPECT_EQ(u"first message", iter->message());
} else if (iter->id().find("chrome://network/sms2") != std::string::npos) {
EXPECT_EQ(u"000-000-0000", iter->title());
EXPECT_EQ(u"second message", iter->message());
} else {
ASSERT_TRUE(false);
}
}
}
class SmsObserverSuppressTextMessagesEnabled : public SmsObserverTest {
public:
void SetUp() override {
SmsObserverTest::SetUp();
histogram_tester_ = std::make_unique<base::HistogramTester>();
}
void ChangeUserSuppressionState(UserTextMessageSuppressionState state) {
network_metadata_store()->SetUserTextMessageSuppressionState(kTestGuid,
state);
}
void ChangePolicySuppressionState(PolicyTextMessageSuppressionState state) {
std::string state_onc;
switch (state) {
case PolicyTextMessageSuppressionState::kUnset:
state_onc = ::onc::cellular::kTextMessagesUnset;
break;
case PolicyTextMessageSuppressionState::kAllow:
state_onc = ::onc::cellular::kTextMessagesAllow;
break;
case PolicyTextMessageSuppressionState::kSuppress:
state_onc = ::onc::cellular::kTextMessagesSuppress;
break;
}
base::Value::Dict global_config;
global_config.Set(::onc::global_network_config::kAllowTextMessages,
state_onc);
managed_network_configuration_handler()->SetPolicy(
::onc::ONC_SOURCE_DEVICE_POLICY, /*userhash=*/std::string(),
base::Value::List(), global_config);
base::RunLoop().RunUntilIdle();
}
void AssertHistogramCounts(size_t user_suppressed_count,
size_t policy_suppressed_count,
size_t not_suppressed_count) {
histogram_tester_->ExpectBucketCount(
CellularNetworkMetricsLogger::
kAllowTextMessagesNotificationSuppressionState,
CellularNetworkMetricsLogger::NotificationSuppressionState::
kUserSuppressed,
/*expected_count=*/user_suppressed_count);
histogram_tester_->ExpectBucketCount(
CellularNetworkMetricsLogger::
kAllowTextMessagesNotificationSuppressionState,
CellularNetworkMetricsLogger::NotificationSuppressionState::
kPolicySuppressed,
/*expected_count=*/policy_suppressed_count);
histogram_tester_->ExpectBucketCount(
CellularNetworkMetricsLogger::
kAllowTextMessagesNotificationSuppressionState,
CellularNetworkMetricsLogger::NotificationSuppressionState::
kNotSuppressed,
/*expected_count=*/not_suppressed_count);
size_t total_count =
user_suppressed_count + policy_suppressed_count + not_suppressed_count;
histogram_tester_->ExpectTotalCount(
CellularNetworkMetricsLogger::
kAllowTextMessagesNotificationSuppressionState,
/*expected_count=*/total_count);
}
private:
std::unique_ptr<base::HistogramTester> histogram_tester_;
};
TEST_F(SmsObserverSuppressTextMessagesEnabled, SuccessGuardRailMetricsTest) {
base::HistogramTester histogram_tester;
AssertHistogramCounts(/*user_suppressed_count=*/0u,
/*policy_suppressed_count=*/0u,
/*not_suppressed_count=*/0u);
ChangeUserSuppressionState(UserTextMessageSuppressionState::kAllow);
ChangePolicySuppressionState(PolicyTextMessageSuppressionState::kUnset);
SimulateMessageReceived();
AssertHistogramCounts(/*user_suppressed_count=*/0u,
/*policy_suppressed_count=*/0u,
/*not_suppressed_count=*/1u);
ChangeUserSuppressionState(UserTextMessageSuppressionState::kSuppress);
ChangePolicySuppressionState(PolicyTextMessageSuppressionState::kUnset);
SimulateMessageReceived();
AssertHistogramCounts(/*user_suppressed_count=*/1u,
/*policy_suppressed_count=*/0u,
/*not_suppressed_count=*/1u);
ChangeUserSuppressionState(UserTextMessageSuppressionState::kAllow);
ChangePolicySuppressionState(PolicyTextMessageSuppressionState::kAllow);
SimulateMessageReceived();
AssertHistogramCounts(/*user_suppressed_count=*/1u,
/*policy_suppressed_count=*/0u,
/*not_suppressed_count=*/2u);
ChangeUserSuppressionState(UserTextMessageSuppressionState::kSuppress);
ChangePolicySuppressionState(PolicyTextMessageSuppressionState::kAllow);
SimulateMessageReceived();
AssertHistogramCounts(/*user_suppressed_count=*/1u,
/*policy_suppressed_count=*/0u,
/*not_suppressed_count=*/3u);
ChangeUserSuppressionState(UserTextMessageSuppressionState::kSuppress);
ChangePolicySuppressionState(PolicyTextMessageSuppressionState::kSuppress);
SimulateMessageReceived();
AssertHistogramCounts(/*user_suppressed_count=*/1u,
/*policy_suppressed_count=*/1u,
/*not_suppressed_count=*/3u);
ChangeUserSuppressionState(UserTextMessageSuppressionState::kAllow);
ChangePolicySuppressionState(PolicyTextMessageSuppressionState::kSuppress);
SimulateMessageReceived();
AssertHistogramCounts(/*user_suppressed_count=*/1u,
/*policy_suppressed_count=*/2u,
/*not_suppressed_count=*/3u);
}
} // namespace ash
|