blob: 50155e8ad91fbb66020b0686eeb1cfe01f3a354e [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "cloud_print/service/service_state.h"
6
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01007#include "base/strings/string_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include "testing/gmock/include/gmock/gmock.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11using ::testing::_;
12using ::testing::Exactly;
13using ::testing::Return;
14
15TEST(ServiceStateTest, Empty) {
16 ServiceState state;
17 EXPECT_FALSE(state.IsValid());
18}
19
20TEST(ServiceStateTest, ToString) {
21 ServiceState state;
22 EXPECT_STREQ("{\"cloud_print\": {\"enabled\": true}}",
23 CollapseWhitespaceASCII(state.ToString(), true).c_str());
24 state.set_email("test@gmail.com");
25 state.set_proxy_id("proxy");
26 state.set_robot_email("robot@gmail.com");
27 state.set_robot_token("abc");
28 state.set_auth_token("token1");
29 state.set_xmpp_auth_token("token2");
30 EXPECT_TRUE(state.IsValid());
31 EXPECT_STREQ("{\"cloud_print\": {\"auth_token\": \"token1\",\"email\": "
32 "\"test@gmail.com\",\"enabled\": true,\"proxy_id\": \"proxy\","
33 "\"robot_email\": \"robot@gmail.com\",\"robot_refresh_token\": "
34 "\"abc\",\"xmpp_auth_token\": \"token2\"}}",
35 CollapseWhitespaceASCII(state.ToString(), true).c_str());
36}
37
38TEST(ServiceStateTest, FromString) {
39 ServiceState state;
40 // Syntax error.
41 EXPECT_FALSE(state.FromString("<\"cloud_print\": {\"enabled\": true}}"));
42 // No data.
43 EXPECT_FALSE(state.FromString("{\"cloud_print\": {\"enabled\": true}}"));
44 EXPECT_FALSE(state.FromString(
45 "{\"cloud_print\": {\"email\": \"test@gmail.com\"}}"));
46 EXPECT_STREQ("test@gmail.com", state.email().c_str());
47
48 // Good state.
49 EXPECT_TRUE(state.FromString(
50 "{\"cloud_print\": {\"email\": \"test2@gmail.com\",\"enabled\": true,\""
51 "proxy_id\": \"proxy\",\"robot_email\": \"robot@gmail.com\",\""
52 "robot_refresh_token\": \"abc\"}}"));
53 EXPECT_STREQ("test2@gmail.com", state.email().c_str());
54 EXPECT_STREQ("proxy", state.proxy_id().c_str());
55 EXPECT_STREQ("robot@gmail.com", state.robot_email().c_str());
56 EXPECT_STREQ("abc", state.robot_token().c_str());
57}
58
59class ServiceStateMock : public ServiceState {
60 public:
61 ServiceStateMock() {}
62
63 MOCK_METHOD3(LoginToGoogle,
64 std::string(const std::string& service,
65 const std::string& email,
66 const std::string& password));
67
68 private:
69 DISALLOW_COPY_AND_ASSIGN(ServiceStateMock);
70};
71
72TEST(ServiceStateTest, Configure) {
73 ServiceStateMock state;
74 state.set_email("test1@gmail.com");
75 state.set_proxy_id("id1");
76
77 EXPECT_CALL(state, LoginToGoogle("cloudprint", "test2@gmail.com", "abc"))
78 .Times(Exactly(1))
79 .WillOnce(Return("auth1"));
80 EXPECT_CALL(state, LoginToGoogle("chromiumsync", "test2@gmail.com", "abc"))
81 .Times(Exactly(1))
82 .WillOnce(Return("auth2"));
83
84 EXPECT_TRUE(state.Configure("test2@gmail.com", "abc", "id2"));
85
86 EXPECT_STREQ("id2", state.proxy_id().c_str());
87 EXPECT_STREQ("auth1", state.auth_token().c_str());
88 EXPECT_STREQ("auth2", state.xmpp_auth_token().c_str());
89}