• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/rpc_task.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "shill/mock_adaptors.h"
22 #include "shill/nice_mock_control.h"
23 
24 using std::map;
25 using std::string;
26 using std::vector;
27 
28 namespace shill {
29 
30 class RPCTaskTest : public testing::Test,
31                     public RPCTaskDelegate {
32  public:
RPCTaskTest()33   RPCTaskTest()
34       : get_login_calls_(0),
35         notify_calls_(0),
36         task_(&control_, this) {}
37 
38   // Inherited from RPCTaskDelegate.
39   virtual void GetLogin(string* user, string* password);
40   virtual void Notify(const string& reason, const map<string, string>& dict);
41 
42  protected:
43   int get_login_calls_;
44   int notify_calls_;
45   string* last_user_;
46   string* last_password_;
47   string last_notify_reason_;
48   map<string, string> last_notify_dict_;
49   NiceMockControl control_;
50   RPCTask task_;
51 };
52 
GetLogin(string * user,string * password)53 void RPCTaskTest::GetLogin(string* user, string* password) {
54   get_login_calls_++;
55   last_user_ = user;
56   last_password_ = password;
57 }
58 
Notify(const string & reason,const map<string,string> & dict)59 void RPCTaskTest::Notify(const string& reason,
60                          const map<string, string>& dict) {
61   notify_calls_++;
62   last_notify_reason_ = reason;
63   last_notify_dict_ = dict;
64 }
65 
TEST_F(RPCTaskTest,GetEnvironment)66 TEST_F(RPCTaskTest, GetEnvironment) {
67   map<string, string> env = task_.GetEnvironment();
68   ASSERT_EQ(2, env.size());
69   EXPECT_EQ(env[kRPCTaskServiceVariable], RPCTaskMockAdaptor::kRpcConnId);
70   EXPECT_EQ(env[kRPCTaskPathVariable], RPCTaskMockAdaptor::kRpcId);
71 }
72 
TEST_F(RPCTaskTest,GetRpcIdentifiers)73 TEST_F(RPCTaskTest, GetRpcIdentifiers) {
74   EXPECT_EQ(RPCTaskMockAdaptor::kRpcId, task_.GetRpcIdentifier());
75   EXPECT_EQ(RPCTaskMockAdaptor::kRpcConnId, task_.GetRpcConnectionIdentifier());
76 }
77 
TEST_F(RPCTaskTest,GetLogin)78 TEST_F(RPCTaskTest, GetLogin) {
79   string user, password;
80   task_.GetLogin(&user, &password);
81   EXPECT_EQ(1, get_login_calls_);
82   EXPECT_EQ(&user, last_user_);
83   EXPECT_EQ(&password, last_password_);
84 }
85 
TEST_F(RPCTaskTest,Notify)86 TEST_F(RPCTaskTest, Notify) {
87   static const char kReason[] = "up";
88   map<string, string> dict;
89   dict["foo"] = "bar";
90   task_.Notify(kReason, dict);
91   EXPECT_EQ(1, notify_calls_);
92   EXPECT_EQ(kReason, last_notify_reason_);
93   EXPECT_EQ("bar", last_notify_dict_["foo"]);
94 }
95 
96 }  // namespace shill
97