1 // Copyright (c) 2012 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.
5 #include "chromeos/network/network_sms_handler.h"
10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "chromeos/chromeos_switches.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/shill_device_client.h"
16 #include "chromeos/dbus/shill_manager_client.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
24 class TestObserver
: public NetworkSmsHandler::Observer
{
27 virtual ~TestObserver() {}
29 virtual void MessageReceived(const base::DictionaryValue
& message
) OVERRIDE
{
31 if (message
.GetStringWithoutPathExpansion(
32 NetworkSmsHandler::kTextKey
, &text
)) {
33 messages_
.insert(text
);
37 void ClearMessages() {
41 int message_count() { return messages_
.size(); }
42 const std::set
<std::string
>& messages() const {
47 std::set
<std::string
> messages_
;
52 class NetworkSmsHandlerTest
: public testing::Test
{
54 NetworkSmsHandlerTest() {}
55 virtual ~NetworkSmsHandlerTest() {}
57 virtual void SetUp() OVERRIDE
{
58 // Append '--sms-test-messages' to the command line to tell
59 // SMSClientStubImpl to generate a series of test SMS messages.
60 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
61 command_line
->AppendSwitch(chromeos::switches::kSmsTestMessages
);
63 // Initialize DBusThreadManager with a stub implementation.
64 DBusThreadManager::InitializeWithStub();
65 ShillManagerClient::TestInterface
* manager_test
=
66 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
67 ASSERT_TRUE(manager_test
);
68 manager_test
->AddDevice("stub_cellular_device2");
69 ShillDeviceClient::TestInterface
* device_test
=
70 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
71 ASSERT_TRUE(device_test
);
72 device_test
->AddDevice("stub_cellular_device2", shill::kTypeCellular
,
73 "/org/freedesktop/ModemManager1/stub/0");
75 // This relies on the stub dbus implementations for ShillManagerClient,
76 // ShillDeviceClient, GsmSMSClient, ModemMessagingClient and SMSClient.
77 // Initialize a sms handler. The stub dbus clients will not send the
78 // first test message until RequestUpdate has been called.
79 network_sms_handler_
.reset(new NetworkSmsHandler());
80 network_sms_handler_
->Init();
81 test_observer_
.reset(new TestObserver());
82 network_sms_handler_
->AddObserver(test_observer_
.get());
83 network_sms_handler_
->RequestUpdate(true);
84 message_loop_
.RunUntilIdle();
87 virtual void TearDown() OVERRIDE
{
88 network_sms_handler_
.reset();
89 DBusThreadManager::Shutdown();
93 base::MessageLoopForUI message_loop_
;
94 scoped_ptr
<NetworkSmsHandler
> network_sms_handler_
;
95 scoped_ptr
<TestObserver
> test_observer_
;
98 TEST_F(NetworkSmsHandlerTest
, SmsHandlerDbusStub
) {
99 EXPECT_EQ(test_observer_
->message_count(), 0);
101 // Test that no messages have been received yet
102 const std::set
<std::string
>& messages(test_observer_
->messages());
103 // Note: The following string corresponds to values in
104 // ModemMessagingClientStubImpl and SmsClientStubImpl.
105 // TODO(stevenjb): Use a TestInterface to set this up to remove dependency.
106 const char kMessage1
[] = "FakeSMSClient: Test Message: /SMS/0";
107 EXPECT_EQ(messages
.find(kMessage1
), messages
.end());
109 // Test for messages delivered by signals.
110 test_observer_
->ClearMessages();
111 network_sms_handler_
->RequestUpdate(false);
112 message_loop_
.RunUntilIdle();
113 EXPECT_GE(test_observer_
->message_count(), 1);
114 EXPECT_NE(messages
.find(kMessage1
), messages
.end());
117 } // namespace chromeos