Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 "shill/rpc_task.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 9 | #include "shill/mock_adaptors.h" |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 10 | #include "shill/nice_mock_control.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 11 | |
| 12 | using std::map; |
| 13 | using std::string; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 14 | using std::vector; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 15 | |
| 16 | namespace shill { |
| 17 | |
| 18 | class RPCTaskTest : public testing::Test, |
| 19 | public RPCTaskDelegate { |
| 20 | public: |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 21 | RPCTaskTest() |
| 22 | : get_login_calls_(0), |
| 23 | notify_calls_(0), |
| 24 | task_(&control_, this) {} |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 25 | |
| 26 | // Inherited from RPCTaskDelegate. |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 27 | virtual void GetLogin(string *user, string *password); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 28 | virtual void Notify(const string &reason, const map<string, string> &dict); |
| 29 | |
| 30 | protected: |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 31 | int get_login_calls_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 32 | int notify_calls_; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 33 | string *last_user_; |
| 34 | string *last_password_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 35 | string last_notify_reason_; |
| 36 | map<string, string> last_notify_dict_; |
| 37 | NiceMockControl control_; |
| 38 | RPCTask task_; |
| 39 | }; |
| 40 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 41 | void RPCTaskTest::GetLogin(string *user, string *password) { |
| 42 | get_login_calls_++; |
| 43 | last_user_ = user; |
| 44 | last_password_ = password; |
| 45 | } |
| 46 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 47 | void RPCTaskTest::Notify(const string &reason, |
| 48 | const map<string, string> &dict) { |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 49 | notify_calls_++; |
| 50 | last_notify_reason_ = reason; |
| 51 | last_notify_dict_ = dict; |
| 52 | } |
| 53 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 54 | TEST_F(RPCTaskTest, GetEnvironment) { |
| 55 | vector<string> env = task_.GetEnvironment(); |
| 56 | ASSERT_EQ(2, env.size()); |
| 57 | EXPECT_EQ( |
| 58 | string(kRPCTaskServiceVariable) + "=" + RPCTaskMockAdaptor::kRpcConnId, |
| 59 | env[0]); |
| 60 | EXPECT_EQ(string(kRPCTaskPathVariable) + "=" + RPCTaskMockAdaptor::kRpcId, |
| 61 | env[1]); |
| 62 | } |
| 63 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 64 | TEST_F(RPCTaskTest, GetRpcIdentifiers) { |
| 65 | EXPECT_EQ(RPCTaskMockAdaptor::kRpcId, task_.GetRpcIdentifier()); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 66 | EXPECT_EQ(RPCTaskMockAdaptor::kRpcConnId, task_.GetRpcConnectionIdentifier()); |
| 67 | } |
| 68 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 69 | TEST_F(RPCTaskTest, GetLogin) { |
| 70 | string user, password; |
| 71 | task_.GetLogin(&user, &password); |
| 72 | EXPECT_EQ(1, get_login_calls_); |
| 73 | EXPECT_EQ(&user, last_user_); |
| 74 | EXPECT_EQ(&password, last_password_); |
| 75 | } |
| 76 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 77 | TEST_F(RPCTaskTest, Notify) { |
| 78 | static const char kReason[] = "up"; |
| 79 | map<string, string> dict; |
| 80 | dict["foo"] = "bar"; |
| 81 | task_.Notify(kReason, dict); |
| 82 | EXPECT_EQ(1, notify_calls_); |
| 83 | EXPECT_EQ(kReason, last_notify_reason_); |
| 84 | EXPECT_EQ("bar", last_notify_dict_["foo"]); |
| 85 | } |
| 86 | |
| 87 | } // namespace shill |