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 "chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.h" 6 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/chrome_switches.h" 12 #include "chrome/common/extensions/api/cloud_print_private.h" 13 #include "chrome/test/base/ui_test_utils.h" 14 #include "net/dns/mock_host_resolver.h" 15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 using ::testing::Eq; 19 using ::testing::Property; 20 using ::testing::Return; 21 using ::testing::_; 22 23 // A base class for tests below. 24 class ExtensionCloudPrintPrivateApiTest : public ExtensionApiTest { 25 public: 26 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 27 ExtensionApiTest::SetUpCommandLine(command_line); 28 command_line->AppendSwitchASCII(switches::kCloudPrintServiceURL, 29 "http://www.cloudprintapp.com/files/extensions/api_test/" 30 "cloud_print_private"); 31 } 32 33 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 34 // Start up the test server and get us ready for calling the install 35 // API functions. 36 host_resolver()->AddRule("www.cloudprintapp.com", "127.0.0.1"); 37 ASSERT_TRUE(test_server()->Start()); 38 } 39 40 protected: 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 43 // flags. 44 GURL GetTestServerURL(const std::string& path) { 45 GURL url = test_server()->GetURL( 46 "files/extensions/api_test/cloud_print_private/" + path); 47 48 // Replace the host with 'www.cloudprintapp.com' so it matches the cloud 49 // print app's extent. 50 GURL::Replacements replace_host; 51 std::string host_str("www.cloudprintapp.com"); 52 replace_host.SetHostStr(host_str); 53 return url.ReplaceComponents(replace_host); 54 } 55 }; 56 57 #if !defined(OS_CHROMEOS) 58 59 using extensions::api::cloud_print_private::UserSettings; 60 61 class CloudPrintTestsDelegateMock : public extensions::CloudPrintTestsDelegate { 62 public: 63 CloudPrintTestsDelegateMock() {} 64 65 MOCK_METHOD4(SetupConnector, 66 void(const std::string& user_email, 67 const std::string& robot_email, 68 const std::string& credentials, 69 const UserSettings& user_settings)); 70 MOCK_METHOD0(GetHostName, std::string()); 71 MOCK_METHOD0(GetPrinters, std::vector<std::string>()); 72 MOCK_METHOD0(GetClientId, std::string()); 73 74 private: 75 DISALLOW_COPY_AND_ASSIGN(CloudPrintTestsDelegateMock); 76 }; 77 78 MATCHER(IsExpectedUserSettings, "") { 79 const UserSettings& settings = arg; 80 return settings.connect_new_printers && 81 settings.printers.size() == 2 && 82 settings.printers[0]->name == "printer1" && 83 !settings.printers[0]->connect && 84 settings.printers[1]->name == "printer2" && 85 settings.printers[1]->connect; 86 } 87 88 // http://crbug.com/177163 89 #if defined(OS_WIN) && !defined(NDEBUG) 90 #define MAYBE_CloudPrintHosted DISABLED_CloudPrintHosted 91 #else 92 #define MAYBE_CloudPrintHosted CloudPrintHosted 93 #endif 94 IN_PROC_BROWSER_TEST_F(ExtensionCloudPrintPrivateApiTest, 95 MAYBE_CloudPrintHosted) { 96 CloudPrintTestsDelegateMock cloud_print_mock; 97 98 EXPECT_CALL(cloud_print_mock, 99 SetupConnector("foo (at) gmail.com", 100 "foorobot (at) googleusercontent.com", 101 "1/23546efa54", 102 IsExpectedUserSettings())); 103 EXPECT_CALL(cloud_print_mock, GetHostName()) 104 .WillRepeatedly(Return("TestHostName")); 105 106 std::vector<std::string> printers; 107 printers.push_back("printer1"); 108 printers.push_back("printer2"); 109 EXPECT_CALL(cloud_print_mock, GetPrinters()) 110 .WillRepeatedly(Return(printers)); 111 112 EXPECT_CALL(cloud_print_mock, GetClientId()) 113 .WillRepeatedly(Return("TestAPIClient")); 114 115 // Run this as a hosted app. Since we have overridden the cloud print service 116 // URL in the command line, this URL should match the web extent for our 117 // cloud print component app and it should work. 118 GURL page_url = GetTestServerURL( 119 "enable_chrome_connector/cloud_print_success_tests.html"); 120 ASSERT_TRUE(RunPageTest(page_url.spec())); 121 } 122 123 #endif // !defined(OS_CHROMEOS) 124