Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / service / cloud_print / connector_settings_unittest.cc
blob01ac87580570258d6f479f6fc92b297d4944c80c
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.
5 #include "chrome/service/cloud_print/connector_settings.h"
7 #include <string>
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/values.h"
14 #include "chrome/common/cloud_print/cloud_print_constants.h"
15 #include "chrome/service/service_process_prefs.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace cloud_print {
22 const char kServiceStateContent[] =
23 "{"
24 " 'cloud_print': {"
25 " 'auth_token': 'token',"
26 " 'email': 'user@gmail.com',"
27 " 'enabled': true,"
28 " 'proxy_id': 'PROXY',"
29 " 'robot_email': '123@cloudprint.googleusercontent.com',"
30 " 'robot_refresh_token': '123',"
31 " 'xmpp_auth_token': 'xmp token',"
32 " 'xmpp_ping_enabled': true,"
33 " 'xmpp_ping_timeout_sec': 256,"
34 " 'user_settings': {"
35 " 'printers': ["
36 " { 'name': 'prn1', 'connect': false },"
37 " { 'name': 'prn2', 'connect': false },"
38 " { 'name': 'prn3', 'connect': true }"
39 " ],"
40 " 'connectNewPrinters': false"
41 " },"
42 " 'print_system_settings': {"
43 " 'delete_on_enum_fail' : true"
44 " }"
45 " }"
46 "}";
49 class ConnectorSettingsTest : public testing::Test {
50 protected:
51 virtual void SetUp() OVERRIDE {
52 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
53 message_loop_proxy_ = base::MessageLoopProxy::current();
56 ServiceProcessPrefs* CreateTestFile(const char* json) {
57 base::FilePath file_name = temp_dir_.path().AppendASCII("file.txt");
58 base::DeleteFile(file_name, false);
59 if (json) {
60 std::string content = json;
61 std::replace(content.begin(), content.end(), '\'', '"');
62 base::WriteFile(file_name, content.c_str(), content.size());
64 ServiceProcessPrefs* prefs =
65 new ServiceProcessPrefs(file_name, message_loop_proxy_.get());
66 prefs->ReadPrefs();
67 return prefs;
70 base::ScopedTempDir temp_dir_;
71 base::MessageLoop message_loop_;
72 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
75 TEST_F(ConnectorSettingsTest, InitFromEmpty) {
76 const char* const kEmptyJSons[] = {
77 NULL,
78 "{}",
79 "{'foo': []}",
80 "{'foo',,}",
82 for (size_t i = 0; i < arraysize(kEmptyJSons); ++i) {
83 scoped_ptr<ServiceProcessPrefs> prefs(CreateTestFile(kEmptyJSons[i]));
84 ConnectorSettings settings;
85 settings.InitFrom(prefs.get());
87 EXPECT_EQ("https://www.google.com/cloudprint",
88 settings.server_url().spec());
89 EXPECT_FALSE(settings.proxy_id().empty());
90 EXPECT_FALSE(settings.delete_on_enum_fail());
91 EXPECT_EQ(NULL, settings.print_system_settings());
92 EXPECT_TRUE(settings.ShouldConnect("prn1"));
93 EXPECT_FALSE(settings.xmpp_ping_enabled());
97 TEST_F(ConnectorSettingsTest, InitFromFile) {
98 scoped_ptr<ServiceProcessPrefs> prefs(CreateTestFile(kServiceStateContent));
99 ConnectorSettings settings;
100 settings.InitFrom(prefs.get());
101 EXPECT_EQ("https://www.google.com/cloudprint", settings.server_url().spec());
102 EXPECT_EQ("PROXY", settings.proxy_id());
103 EXPECT_FALSE(settings.proxy_id().empty());
104 EXPECT_TRUE(settings.delete_on_enum_fail());
105 EXPECT_TRUE(settings.print_system_settings());
106 EXPECT_TRUE(settings.xmpp_ping_enabled());
107 EXPECT_EQ(settings.xmpp_ping_timeout_sec(), 256);
108 EXPECT_FALSE(settings.ShouldConnect("prn0"));
109 EXPECT_FALSE(settings.ShouldConnect("prn1"));
110 EXPECT_TRUE(settings.ShouldConnect("prn3"));
113 TEST_F(ConnectorSettingsTest, CopyFrom) {
114 scoped_ptr<ServiceProcessPrefs> prefs(CreateTestFile(kServiceStateContent));
115 ConnectorSettings settings1;
116 settings1.InitFrom(prefs.get());
118 ConnectorSettings settings2;
119 settings2.CopyFrom(settings1);
121 EXPECT_EQ(settings1.server_url(), settings2.server_url());
122 EXPECT_EQ(settings1.proxy_id(), settings2.proxy_id());
123 EXPECT_EQ(settings1.delete_on_enum_fail(), settings2.delete_on_enum_fail());
124 EXPECT_EQ(settings1.print_system_settings()->size(),
125 settings2.print_system_settings()->size());
126 EXPECT_EQ(settings1.xmpp_ping_enabled(), settings2.xmpp_ping_enabled());
127 EXPECT_EQ(settings1.xmpp_ping_timeout_sec(),
128 settings2.xmpp_ping_timeout_sec());
129 EXPECT_FALSE(settings2.ShouldConnect("prn0"));
130 EXPECT_FALSE(settings2.ShouldConnect("prn1"));
131 EXPECT_TRUE(settings2.ShouldConnect("prn3"));
134 TEST_F(ConnectorSettingsTest, SettersTest) {
135 scoped_ptr<ServiceProcessPrefs> prefs(CreateTestFile("{}"));
136 ConnectorSettings settings;
137 settings.InitFrom(prefs.get());
138 EXPECT_FALSE(settings.xmpp_ping_enabled());
140 // Set and check valid settings.
141 settings.set_xmpp_ping_enabled(true);
142 settings.SetXmppPingTimeoutSec(256);
143 EXPECT_TRUE(settings.xmpp_ping_enabled());
144 EXPECT_EQ(settings.xmpp_ping_timeout_sec(), 256);
146 // Set invalid settings, and check correct defaults.
147 settings.SetXmppPingTimeoutSec(1);
148 EXPECT_EQ(settings.xmpp_ping_timeout_sec(), kMinXmppPingTimeoutSecs);
151 } // namespace cloud_print