Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1 | // 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) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 7 | #include "base/strings/string_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include "testing/gmock/include/gmock/gmock.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | using ::testing::_; |
| 12 | using ::testing::Exactly; |
| 13 | using ::testing::Return; |
| 14 | |
| 15 | TEST(ServiceStateTest, Empty) { |
| 16 | ServiceState state; |
| 17 | EXPECT_FALSE(state.IsValid()); |
| 18 | } |
| 19 | |
| 20 | TEST(ServiceStateTest, ToString) { |
| 21 | ServiceState state; |
| 22 | EXPECT_STREQ("{\"cloud_print\": {\"enabled\": true}}", |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 23 | base::CollapseWhitespaceASCII(state.ToString(), true).c_str()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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\"}}", |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 35 | base::CollapseWhitespaceASCII(state.ToString(), true).c_str()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | TEST(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 | |
| 59 | class 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 | |
| 72 | TEST(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 | } |