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