summaryrefslogtreecommitdiffstats
path: root/cloud_print/service/service_state_unittest.cc
blob: b0f0029d4db8f2ddf35a5e1d27b9893cfe422259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cloud_print/service/service_state.h"

#include "base/strings/string_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::Exactly;
using ::testing::Return;

TEST(ServiceStateTest, Empty) {
  ServiceState state;
  EXPECT_FALSE(state.IsValid());
}

TEST(ServiceStateTest, ToString) {
  ServiceState state;
  EXPECT_STREQ("{\"cloud_print\": {\"enabled\": true}}",
               base::CollapseWhitespaceASCII(state.ToString(), true).c_str());
  state.set_email("test@gmail.com");
  state.set_proxy_id("proxy");
  state.set_robot_email("robot@gmail.com");
  state.set_robot_token("abc");
  state.set_auth_token("token1");
  state.set_xmpp_auth_token("token2");
  EXPECT_TRUE(state.IsValid());
  EXPECT_STREQ("{\"cloud_print\": {\"auth_token\": \"token1\",\"email\": "
               "\"test@gmail.com\",\"enabled\": true,\"proxy_id\": \"proxy\","
               "\"robot_email\": \"robot@gmail.com\",\"robot_refresh_token\": "
               "\"abc\",\"xmpp_auth_token\": \"token2\"}}",
               base::CollapseWhitespaceASCII(state.ToString(), true).c_str());
}

TEST(ServiceStateTest, FromString) {
  ServiceState state;
  // Syntax error.
  EXPECT_FALSE(state.FromString("<\"cloud_print\": {\"enabled\": true}}"));
  // No data.
  EXPECT_FALSE(state.FromString("{\"cloud_print\": {\"enabled\": true}}"));
  EXPECT_FALSE(state.FromString(
      "{\"cloud_print\": {\"email\": \"test@gmail.com\"}}"));
  EXPECT_STREQ("test@gmail.com", state.email().c_str());

  // Good state.
  EXPECT_TRUE(state.FromString(
      "{\"cloud_print\": {\"email\": \"test2@gmail.com\",\"enabled\": true,\""
      "proxy_id\": \"proxy\",\"robot_email\": \"robot@gmail.com\",\""
      "robot_refresh_token\": \"abc\"}}"));
  EXPECT_STREQ("test2@gmail.com", state.email().c_str());
  EXPECT_STREQ("proxy", state.proxy_id().c_str());
  EXPECT_STREQ("robot@gmail.com", state.robot_email().c_str());
  EXPECT_STREQ("abc", state.robot_token().c_str());
}

class ServiceStateMock : public ServiceState {
 public:
  ServiceStateMock() {}

  MOCK_METHOD3(LoginToGoogle,
               std::string(const std::string& service,
                           const std::string& email,
                           const std::string& password));

 private:
  DISALLOW_COPY_AND_ASSIGN(ServiceStateMock);
};

TEST(ServiceStateTest, Configure) {
  ServiceStateMock state;
  state.set_email("test1@gmail.com");
  state.set_proxy_id("id1");

  EXPECT_CALL(state, LoginToGoogle("cloudprint", "test2@gmail.com", "abc"))
    .Times(Exactly(1))
    .WillOnce(Return("auth1"));
  EXPECT_CALL(state, LoginToGoogle("chromiumsync", "test2@gmail.com", "abc"))
    .Times(Exactly(1))
    .WillOnce(Return("auth2"));

  EXPECT_TRUE(state.Configure("test2@gmail.com", "abc", "id2"));

  EXPECT_STREQ("id2", state.proxy_id().c_str());
  EXPECT_STREQ("auth1", state.auth_token().c_str());
  EXPECT_STREQ("auth2", state.xmpp_auth_token().c_str());
}