1// Copyright 2013 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. 4 5#include <vector> 6 7#include "base/json/json_writer.h" 8#include "base/values.h" 9#include "sync/test/accounts_client/test_accounts_client.h" 10#include "testing/gmock/include/gmock/gmock.h" 11#include "testing/gtest/include/gtest/gtest.h" 12#include "url/gurl.h" 13 14using std::string; 15using std::vector; 16using testing::_; 17using testing::DoAll; 18using testing::Return; 19using testing::SetArgPointee; 20 21namespace { 22static const string kServer = "https://test-account-service"; 23static const string kUsername = "foobar@baz.com"; 24static const string kAccountSpace = "test_account_space"; 25static const string kSessionId = "1234-ABCD"; 26static const string kExpirationTime = "12:00"; 27} // namespace 28 29static AccountSession CreateValidAccountSession() { 30 AccountSession session; 31 session.username = kUsername; 32 session.account_space = kAccountSpace; 33 session.session_id = kSessionId; 34 session.expiration_time = kExpirationTime; 35 return session; 36} 37 38class NoNetworkTestAccountsClient : public TestAccountsClient { 39 public: 40 NoNetworkTestAccountsClient(const string& server, 41 const string& account_space, 42 vector<string> usernames) 43 : TestAccountsClient(server, account_space, usernames) {} 44 MOCK_METHOD2(SendRequest, bool(const GURL&, string*)); 45}; 46 47TEST(TestAccountsClientTest, ClaimAccountError) { 48 vector<string> usernames; 49 NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames); 50 EXPECT_CALL(client, SendRequest(_, _)) 51 .WillOnce(Return(false)); 52 AccountSession session; 53 EXPECT_FALSE(client.ClaimAccount(&session)); 54} 55 56TEST(TestAccountsClientTest, ClaimAccountSuccess) { 57 vector<string> usernames; 58 usernames.push_back("foo0@gmail.com"); 59 usernames.push_back("foo1@gmail.com"); 60 usernames.push_back("foo2@gmail.com"); 61 NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames); 62 63 base::DictionaryValue success_dict; 64 success_dict.Set("username", new base::StringValue(kUsername)); 65 success_dict.Set("account_space", new base::StringValue(kAccountSpace)); 66 success_dict.Set("session_id", new base::StringValue(kSessionId)); 67 success_dict.Set("expiration_time", new base::StringValue(kExpirationTime)); 68 69 string success_response; 70 base::JSONWriter::Write(&success_dict, &success_response); 71 EXPECT_CALL(client, SendRequest(_, _)) 72 .WillOnce(DoAll(SetArgPointee<1>(success_response), Return(true))); 73 74 AccountSession session; 75 EXPECT_TRUE(client.ClaimAccount(&session)); 76 EXPECT_EQ(kUsername, session.username); 77 EXPECT_EQ(kAccountSpace, session.account_space); 78 EXPECT_EQ(kSessionId, session.session_id); 79 EXPECT_EQ(kExpirationTime, session.expiration_time); 80} 81 82TEST(TestAccountsClientTest, ReleaseAccountEmptySession) { 83 vector<string> usernames; 84 NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames); 85 AccountSession session; 86 // No expectation for SendRequest is made because no network call should be 87 // performed in this scenario. 88 client.ReleaseAccount(session); 89} 90 91TEST(TestAccountsClientTest, ReleaseAccountSuccess) { 92 vector<string> usernames; 93 NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames); 94 EXPECT_CALL(client, SendRequest(_, _)) 95 .WillOnce(Return(true)); 96 AccountSession session = CreateValidAccountSession(); 97 client.ReleaseAccount(session); 98} 99