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/browser/extensions/api/cloud_print_private/cloud_print_private_api.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/common/extensions/api/cloud_print_private.h"
12 #include "components/cloud_devices/common/cloud_devices_switches.h"
13 #include "net/dns/mock_host_resolver.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::Property
;
19 using ::testing::Return
;
22 // A base class for tests below.
23 class ExtensionCloudPrintPrivateApiTest
: public ExtensionApiTest
{
25 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
26 ExtensionApiTest::SetUpCommandLine(command_line
);
27 command_line
->AppendSwitchASCII(
28 switches::kCloudPrintURL
,
29 "http://www.cloudprintapp.com/files/extensions/api_test/"
30 "cloud_print_private");
33 void SetUpInProcessBrowserTestFixture() override
{
34 // Start up the test server and get us ready for calling the install
36 host_resolver()->AddRule("www.cloudprintapp.com", "127.0.0.1");
37 ASSERT_TRUE(test_server()->Start());
41 // Returns a test server URL, but with host 'www.cloudprintapp.com' so it
42 // matches the cloud print app's extent that we set up via command line
44 GURL
GetTestServerURL(const std::string
& path
) {
45 GURL url
= test_server()->GetURL(
46 "files/extensions/api_test/cloud_print_private/" + path
);
48 // Replace the host with 'www.cloudprintapp.com' so it matches the cloud
49 // print app's extent.
50 GURL::Replacements replace_host
;
51 replace_host
.SetHostStr("www.cloudprintapp.com");
52 return url
.ReplaceComponents(replace_host
);
56 #if !defined(OS_CHROMEOS)
58 using extensions::api::cloud_print_private::UserSettings
;
60 class CloudPrintTestsDelegateMock
: public extensions::CloudPrintTestsDelegate
{
62 CloudPrintTestsDelegateMock() {}
64 MOCK_METHOD4(SetupConnector
,
65 void(const std::string
& user_email
,
66 const std::string
& robot_email
,
67 const std::string
& credentials
,
68 const UserSettings
& user_settings
));
69 MOCK_METHOD0(GetHostName
, std::string());
70 MOCK_METHOD0(GetPrinters
, std::vector
<std::string
>());
71 MOCK_METHOD0(GetClientId
, std::string());
74 DISALLOW_COPY_AND_ASSIGN(CloudPrintTestsDelegateMock
);
77 MATCHER(IsExpectedUserSettings
, "") {
78 const UserSettings
& settings
= arg
;
79 return settings
.connect_new_printers
&&
80 settings
.printers
.size() == 2 &&
81 settings
.printers
[0]->name
== "printer1" &&
82 !settings
.printers
[0]->connect
&&
83 settings
.printers
[1]->name
== "printer2" &&
84 settings
.printers
[1]->connect
;
87 // http://crbug.com/177163
88 #if defined(OS_WIN) && !defined(NDEBUG)
89 #define MAYBE_CloudPrintHosted DISABLED_CloudPrintHosted
91 #define MAYBE_CloudPrintHosted CloudPrintHosted
93 IN_PROC_BROWSER_TEST_F(ExtensionCloudPrintPrivateApiTest
,
94 MAYBE_CloudPrintHosted
) {
95 CloudPrintTestsDelegateMock cloud_print_mock
;
97 EXPECT_CALL(cloud_print_mock
,
98 SetupConnector("foo@gmail.com",
99 "foorobot@googleusercontent.com",
101 IsExpectedUserSettings()));
102 EXPECT_CALL(cloud_print_mock
, GetHostName())
103 .WillRepeatedly(Return("TestHostName"));
105 std::vector
<std::string
> printers
;
106 printers
.push_back("printer1");
107 printers
.push_back("printer2");
108 EXPECT_CALL(cloud_print_mock
, GetPrinters())
109 .WillRepeatedly(Return(printers
));
111 EXPECT_CALL(cloud_print_mock
, GetClientId())
112 .WillRepeatedly(Return("TestAPIClient"));
114 // Run this as a hosted app. Since we have overridden the cloud print service
115 // URL in the command line, this URL should match the web extent for our
116 // cloud print component app and it should work.
117 GURL page_url
= GetTestServerURL(
118 "enable_chrome_connector/cloud_print_success_tests.html");
119 ASSERT_TRUE(RunPageTest(page_url
.spec()));
122 #endif // !defined(OS_CHROMEOS)