Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / push_messaging / push_messaging_canary_test.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 <string>
6
7 #include "base/strings/string16.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/api/push_messaging/sync_setup_helper.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "extensions/common/extension_set.h"
19 #include "extensions/test/result_catcher.h"
20 #include "net/dns/mock_host_resolver.h"
21
22 namespace {
23 const char kTestExtensionId[] = "mfaehphpebmlbfdiegjnpidmibldjbjk";
24 const char kPasswordFileForTest[] = "password-file-for-test";
25 const char kOverrideUserDataDir[] = "override-user-data-dir";
26 }  // namespace
27
28 namespace extensions {
29
30 // This class provides tests specific to the push messaging
31 // canary test server.  These tests require network access,
32 // and should not be run by normal buildbots as part of the normal
33 // checkin process.
34 class PushMessagingCanaryTest : public ExtensionApiTest {
35  public:
36   PushMessagingCanaryTest() {
37     sync_setup_helper_.reset(new SyncSetupHelper());
38   }
39
40   ~PushMessagingCanaryTest() override {}
41
42   void SetUp() override {
43     CommandLine* command_line = CommandLine::ForCurrentProcess();
44
45     ASSERT_TRUE(command_line->HasSwitch(kPasswordFileForTest));
46     base::FilePath password_file =
47         command_line->GetSwitchValuePath(kPasswordFileForTest);
48     ASSERT_TRUE(sync_setup_helper_->ReadPasswordFile(password_file));
49
50     // The test framework overrides any command line user-data-dir
51     // argument with a /tmp/.org.chromium.Chromium.XXXXXX directory.
52     // That happens in the ChromeTestLauncherDelegate, and affects
53     // all unit tests (no opt out available).  It intentionally erases
54     // any --user-data-dir switch if present and appends a new one.
55     // Re-override the default data dir for our test so we can persist
56     // the profile for this particular test so we can persist the max
57     // invalidation version between runs.
58     const base::FilePath& override_user_data_dir =
59         command_line->GetSwitchValuePath(kOverrideUserDataDir);
60     ASSERT_TRUE(!override_user_data_dir.empty());
61     command_line->AppendSwitchPath(switches::kUserDataDir,
62                                    base::FilePath(override_user_data_dir));
63     LOG(INFO) << "command line file override switch is "
64               << override_user_data_dir.value();
65
66     ExtensionApiTest::SetUp();
67   }
68
69   void InitializeSync() {
70     ASSERT_TRUE(sync_setup_helper_->InitializeSync(profile()));
71   }
72
73   // InProcessBrowserTest override. Destroys the sync client and sync
74   // profile created by the test.  We must clean up ProfileSyncServiceHarness
75   // now before the profile is cleaned up.
76   void TearDownOnMainThread() override { sync_setup_helper_.reset(); }
77
78   const SyncSetupHelper* sync_setup_helper() const {
79     return sync_setup_helper_.get();
80   }
81
82  protected:
83   // Override InProcessBrowserTest. Change behavior of the default host
84   // resolver to avoid DNS lookup errors, so we can make network calls.
85   void SetUpInProcessBrowserTestFixture() override {
86     // The resolver object lifetime is managed by sync_test_setup, not here.
87     EnableDNSLookupForThisTest(
88         new net::RuleBasedHostResolverProc(host_resolver()));
89   }
90
91   void TearDownInProcessBrowserTestFixture() override {
92     DisableDNSLookupForThisTest();
93   }
94
95
96   // Change behavior of the default host resolver to allow DNS lookup
97   // to proceed instead of being blocked by the test infrastructure.
98   void EnableDNSLookupForThisTest(
99       net::RuleBasedHostResolverProc* host_resolver) {
100     // mock_host_resolver_override_ takes ownership of the resolver.
101     scoped_refptr<net::RuleBasedHostResolverProc> resolver =
102         new net::RuleBasedHostResolverProc(host_resolver);
103     resolver->AllowDirectLookup("*.google.com");
104     // On Linux, we use Chromium's NSS implementation which uses the following
105     // hosts for certificate verification. Without these overrides, running the
106     // integration tests on Linux causes error as we make external DNS lookups.
107     resolver->AllowDirectLookup("*.thawte.com");
108     resolver->AllowDirectLookup("*.geotrust.com");
109     resolver->AllowDirectLookup("*.gstatic.com");
110     resolver->AllowDirectLookup("*.googleapis.com");
111     mock_host_resolver_override_.reset(
112         new net::ScopedDefaultHostResolverProc(resolver.get()));
113   }
114
115   // We need to reset the DNS lookup when we finish, or the test will fail.
116   void DisableDNSLookupForThisTest() {
117     mock_host_resolver_override_.reset();
118   }
119
120  private:
121   scoped_ptr<SyncSetupHelper> sync_setup_helper_;
122
123   // This test needs to make live DNS requests for access to
124   // GAIA and sync server URLs under google.com. We use a scoped version
125   // to override the default resolver while the test is active.
126   scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_;
127 };
128
129 // Test that a push can make a round trip through the servers.
130 // This test is disabled to keep it from running on trybots since
131 // it requires network access, and it is not a good idea to run
132 // this test as part of a checkin or nightly test.
133 IN_PROC_BROWSER_TEST_F(PushMessagingCanaryTest, MANUAL_ReceivesPush) {
134   InitializeSync();
135
136   const ExtensionSet* installed_extensions = extension_service()->extensions();
137   if (!installed_extensions->Contains(kTestExtensionId)) {
138     const Extension* extension =
139         LoadExtension(test_data_dir_.AppendASCII("push_messaging_canary"));
140     ASSERT_TRUE(extension);
141   }
142   ASSERT_TRUE(installed_extensions->Contains(kTestExtensionId));
143
144   ResultCatcher catcher;
145   catcher.RestrictToBrowserContext(profile());
146
147   const Extension* extension =
148       extension_service()->extensions()->GetByID(kTestExtensionId);
149   ASSERT_TRUE(extension);
150   ui_test_utils::NavigateToURL(
151       browser(), extension->GetResourceURL("push_messaging_canary.html"));
152
153   const std::string& client_id = sync_setup_helper()->client_id();
154   const std::string& client_secret = sync_setup_helper()->client_secret();
155   const std::string& refresh_token = sync_setup_helper()->refresh_token();
156
157   const base::string16& script_string = base::UTF8ToUTF16(base::StringPrintf(
158       "startTestWithCredentials('%s', '%s', '%s');",
159       client_id.c_str(), client_secret.c_str(), refresh_token.c_str()));
160
161   browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
162       ExecuteJavaScript(script_string);
163
164   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
165 }
166
167 }  // namespace extensions