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"
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/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/values.h"
15 #include "chrome/common/cloud_print/cloud_print_constants.h"
16 #include "chrome/service/service_process_prefs.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace cloud_print
{
23 const char kServiceStateContent
[] =
26 " 'auth_token': 'token',"
27 " 'email': 'user@gmail.com',"
29 " 'proxy_id': 'PROXY',"
30 " 'robot_email': '123@cloudprint.googleusercontent.com',"
31 " 'robot_refresh_token': '123',"
32 " 'xmpp_auth_token': 'xmp token',"
33 " 'xmpp_ping_enabled': true,"
34 " 'xmpp_ping_timeout_sec': 256,"
37 " { 'name': 'prn1', 'connect': false },"
38 " { 'name': 'prn2', 'connect': false },"
39 " { 'name': 'prn3', 'connect': true }"
41 " 'connectNewPrinters': false"
43 " 'print_system_settings': {"
44 " 'delete_on_enum_fail' : true"
50 class ConnectorSettingsTest
: public testing::Test
{
52 void SetUp() override
{
53 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
54 task_runner_
= base::ThreadTaskRunnerHandle::Get();
57 ServiceProcessPrefs
* CreateTestFile(const char* json
) {
58 base::FilePath file_name
= temp_dir_
.path().AppendASCII("file.txt");
59 base::DeleteFile(file_name
, false);
61 std::string content
= json
;
62 std::replace(content
.begin(), content
.end(), '\'', '"');
63 base::WriteFile(file_name
, content
.c_str(), content
.size());
65 ServiceProcessPrefs
* prefs
=
66 new ServiceProcessPrefs(file_name
, task_runner_
.get());
71 base::ScopedTempDir temp_dir_
;
72 base::MessageLoop message_loop_
;
73 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
76 TEST_F(ConnectorSettingsTest
, InitFromEmpty
) {
77 const char* const kEmptyJSons
[] = {
83 for (size_t i
= 0; i
< arraysize(kEmptyJSons
); ++i
) {
84 scoped_ptr
<ServiceProcessPrefs
> prefs(CreateTestFile(kEmptyJSons
[i
]));
85 ConnectorSettings settings
;
86 settings
.InitFrom(prefs
.get());
88 EXPECT_EQ("https://www.google.com/cloudprint",
89 settings
.server_url().spec());
90 EXPECT_FALSE(settings
.proxy_id().empty());
91 EXPECT_FALSE(settings
.delete_on_enum_fail());
92 EXPECT_EQ(NULL
, settings
.print_system_settings());
93 EXPECT_TRUE(settings
.ShouldConnect("prn1"));
94 EXPECT_FALSE(settings
.xmpp_ping_enabled());
98 TEST_F(ConnectorSettingsTest
, InitFromFile
) {
99 scoped_ptr
<ServiceProcessPrefs
> prefs(CreateTestFile(kServiceStateContent
));
100 ConnectorSettings settings
;
101 settings
.InitFrom(prefs
.get());
102 EXPECT_EQ("https://www.google.com/cloudprint", settings
.server_url().spec());
103 EXPECT_EQ("PROXY", settings
.proxy_id());
104 EXPECT_FALSE(settings
.proxy_id().empty());
105 EXPECT_TRUE(settings
.delete_on_enum_fail());
106 EXPECT_TRUE(settings
.print_system_settings());
107 EXPECT_TRUE(settings
.xmpp_ping_enabled());
108 EXPECT_EQ(settings
.xmpp_ping_timeout_sec(), 256);
109 EXPECT_FALSE(settings
.ShouldConnect("prn0"));
110 EXPECT_FALSE(settings
.ShouldConnect("prn1"));
111 EXPECT_TRUE(settings
.ShouldConnect("prn3"));
114 TEST_F(ConnectorSettingsTest
, CopyFrom
) {
115 scoped_ptr
<ServiceProcessPrefs
> prefs(CreateTestFile(kServiceStateContent
));
116 ConnectorSettings settings1
;
117 settings1
.InitFrom(prefs
.get());
119 ConnectorSettings settings2
;
120 settings2
.CopyFrom(settings1
);
122 EXPECT_EQ(settings1
.server_url(), settings2
.server_url());
123 EXPECT_EQ(settings1
.proxy_id(), settings2
.proxy_id());
124 EXPECT_EQ(settings1
.delete_on_enum_fail(), settings2
.delete_on_enum_fail());
125 EXPECT_EQ(settings1
.print_system_settings()->size(),
126 settings2
.print_system_settings()->size());
127 EXPECT_EQ(settings1
.xmpp_ping_enabled(), settings2
.xmpp_ping_enabled());
128 EXPECT_EQ(settings1
.xmpp_ping_timeout_sec(),
129 settings2
.xmpp_ping_timeout_sec());
130 EXPECT_FALSE(settings2
.ShouldConnect("prn0"));
131 EXPECT_FALSE(settings2
.ShouldConnect("prn1"));
132 EXPECT_TRUE(settings2
.ShouldConnect("prn3"));
135 TEST_F(ConnectorSettingsTest
, SettersTest
) {
136 scoped_ptr
<ServiceProcessPrefs
> prefs(CreateTestFile("{}"));
137 ConnectorSettings settings
;
138 settings
.InitFrom(prefs
.get());
139 EXPECT_FALSE(settings
.xmpp_ping_enabled());
141 // Set and check valid settings.
142 settings
.set_xmpp_ping_enabled(true);
143 settings
.SetXmppPingTimeoutSec(256);
144 EXPECT_TRUE(settings
.xmpp_ping_enabled());
145 EXPECT_EQ(settings
.xmpp_ping_timeout_sec(), 256);
147 // Set invalid settings, and check correct defaults.
148 settings
.SetXmppPingTimeoutSec(1);
149 EXPECT_EQ(settings
.xmpp_ping_timeout_sec(), kMinXmppPingTimeoutSecs
);
152 } // namespace cloud_print