Fix copyright line on hotword extension files.
[chromium-blink-merge.git] / chromeos / network / network_sms_handler_unittest.cc
blob199e70f5026586fe42aa69748aedbfe402a9eec1
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"
7 #include <set>
8 #include <string>
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 "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
19 namespace chromeos {
21 namespace {
23 class TestObserver : public NetworkSmsHandler::Observer {
24 public:
25 TestObserver() {}
26 virtual ~TestObserver() {}
28 virtual void MessageReceived(const base::DictionaryValue& message) override {
29 std::string text;
30 if (message.GetStringWithoutPathExpansion(
31 NetworkSmsHandler::kTextKey, &text)) {
32 messages_.insert(text);
36 void ClearMessages() {
37 messages_.clear();
40 int message_count() { return messages_.size(); }
41 const std::set<std::string>& messages() const {
42 return messages_;
45 private:
46 std::set<std::string> messages_;
49 } // namespace
51 class NetworkSmsHandlerTest : public testing::Test {
52 public:
53 NetworkSmsHandlerTest() {}
54 virtual ~NetworkSmsHandlerTest() {}
56 virtual void SetUp() override {
57 // Append '--sms-test-messages' to the command line to tell
58 // SMSClientStubImpl to generate a series of test SMS messages.
59 CommandLine* command_line = CommandLine::ForCurrentProcess();
60 command_line->AppendSwitch(chromeos::switches::kSmsTestMessages);
62 // Initialize DBusThreadManager with a stub implementation.
63 DBusThreadManager::Initialize();
64 ShillDeviceClient::TestInterface* device_test =
65 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
66 ASSERT_TRUE(device_test);
67 device_test->AddDevice("/org/freedesktop/ModemManager1/stub/0",
68 shill::kTypeCellular,
69 "stub_cellular_device2");
71 // This relies on the stub dbus implementations for ShillManagerClient,
72 // ShillDeviceClient, GsmSMSClient, ModemMessagingClient and SMSClient.
73 // Initialize a sms handler. The stub dbus clients will not send the
74 // first test message until RequestUpdate has been called.
75 network_sms_handler_.reset(new NetworkSmsHandler());
76 network_sms_handler_->Init();
77 test_observer_.reset(new TestObserver());
78 network_sms_handler_->AddObserver(test_observer_.get());
79 network_sms_handler_->RequestUpdate(true);
80 message_loop_.RunUntilIdle();
83 virtual void TearDown() override {
84 network_sms_handler_.reset();
85 DBusThreadManager::Shutdown();
88 protected:
89 base::MessageLoopForUI message_loop_;
90 scoped_ptr<NetworkSmsHandler> network_sms_handler_;
91 scoped_ptr<TestObserver> test_observer_;
94 TEST_F(NetworkSmsHandlerTest, SmsHandlerDbusStub) {
95 EXPECT_EQ(test_observer_->message_count(), 0);
97 // Test that no messages have been received yet
98 const std::set<std::string>& messages(test_observer_->messages());
99 // Note: The following string corresponds to values in
100 // ModemMessagingClientStubImpl and SmsClientStubImpl.
101 // TODO(stevenjb): Use a TestInterface to set this up to remove dependency.
102 const char kMessage1[] = "FakeSMSClient: Test Message: /SMS/0";
103 EXPECT_EQ(messages.find(kMessage1), messages.end());
105 // Test for messages delivered by signals.
106 test_observer_->ClearMessages();
107 network_sms_handler_->RequestUpdate(false);
108 message_loop_.RunUntilIdle();
109 EXPECT_GE(test_observer_->message_count(), 1);
110 EXPECT_NE(messages.find(kMessage1), messages.end());
113 } // namespace chromeos