Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / external_protocol / external_protocol_handler_unittest.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/external_protocol/external_protocol_handler.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "content/public/test/test_browser_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 using content::BrowserThread;
12
13 class FakeExternalProtocolHandlerWorker
14     : public ShellIntegration::DefaultProtocolClientWorker {
15  public:
16   FakeExternalProtocolHandlerWorker(
17       ShellIntegration::DefaultWebClientObserver* observer,
18       const std::string& protocol,
19       ShellIntegration::DefaultWebClientState os_state)
20       : ShellIntegration::DefaultProtocolClientWorker(observer, protocol),
21         os_state_(os_state) {}
22
23  private:
24   ~FakeExternalProtocolHandlerWorker() override {}
25
26   ShellIntegration::DefaultWebClientState CheckIsDefault() override {
27     return os_state_;
28   }
29
30   bool SetAsDefault(bool interactive_permitted) override { return true; }
31
32   ShellIntegration::DefaultWebClientState os_state_;
33 };
34
35 class FakeExternalProtocolHandlerDelegate
36     : public ExternalProtocolHandler::Delegate {
37  public:
38   FakeExternalProtocolHandlerDelegate()
39       : block_state_(ExternalProtocolHandler::BLOCK),
40         os_state_(ShellIntegration::UNKNOWN_DEFAULT),
41         has_launched_(false),
42         has_prompted_(false),
43         has_blocked_ (false) {}
44
45   ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
46       ShellIntegration::DefaultWebClientObserver* observer,
47       const std::string& protocol) override {
48     return new FakeExternalProtocolHandlerWorker(observer, protocol, os_state_);
49   }
50
51   ExternalProtocolHandler::BlockState GetBlockState(
52       const std::string& scheme) override {
53     return block_state_;
54   }
55
56   void BlockRequest() override {
57     ASSERT_TRUE(block_state_ == ExternalProtocolHandler::BLOCK ||
58                 os_state_ == ShellIntegration::IS_DEFAULT);
59     has_blocked_ = true;
60   }
61
62   void RunExternalProtocolDialog(const GURL& url,
63                                  int render_process_host_id,
64                                  int routing_id) override {
65     ASSERT_EQ(block_state_, ExternalProtocolHandler::UNKNOWN);
66     ASSERT_NE(os_state_, ShellIntegration::IS_DEFAULT);
67     has_prompted_ = true;
68   }
69
70   void LaunchUrlWithoutSecurityCheck(const GURL& url) override {
71     ASSERT_EQ(block_state_, ExternalProtocolHandler::DONT_BLOCK);
72     ASSERT_NE(os_state_, ShellIntegration::IS_DEFAULT);
73     has_launched_ = true;
74   }
75
76   void FinishedProcessingCheck() override {
77     base::MessageLoop::current()->Quit();
78   }
79
80   void set_os_state(ShellIntegration::DefaultWebClientState value) {
81     os_state_ = value;
82   }
83
84   void set_block_state(ExternalProtocolHandler::BlockState value) {
85     block_state_ = value;
86   }
87
88   bool has_launched() { return has_launched_; }
89   bool has_prompted() { return has_prompted_; }
90   bool has_blocked() { return has_blocked_; }
91
92  private:
93   ExternalProtocolHandler::BlockState block_state_;
94   ShellIntegration::DefaultWebClientState os_state_;
95   bool has_launched_;
96   bool has_prompted_;
97   bool has_blocked_;
98 };
99
100 class ExternalProtocolHandlerTest : public testing::Test {
101  protected:
102   ExternalProtocolHandlerTest()
103       : ui_thread_(BrowserThread::UI, base::MessageLoop::current()),
104         file_thread_(BrowserThread::FILE) {}
105
106   void SetUp() override { file_thread_.Start(); }
107
108   void TearDown() override {
109     // Ensure that g_accept_requests gets set back to true after test execution.
110     ExternalProtocolHandler::PermitLaunchUrl();
111   }
112
113   void DoTest(ExternalProtocolHandler::BlockState block_state,
114               ShellIntegration::DefaultWebClientState os_state,
115               bool should_prompt, bool should_launch, bool should_block) {
116     GURL url("mailto:test@test.com");
117     ASSERT_FALSE(delegate_.has_prompted());
118     ASSERT_FALSE(delegate_.has_launched());
119     ASSERT_FALSE(delegate_.has_blocked());
120
121     delegate_.set_block_state(block_state);
122     delegate_.set_os_state(os_state);
123     ExternalProtocolHandler::LaunchUrlWithDelegate(url, 0, 0, &delegate_);
124     if (block_state != ExternalProtocolHandler::BLOCK)
125       base::MessageLoop::current()->Run();
126
127     ASSERT_EQ(should_prompt, delegate_.has_prompted());
128     ASSERT_EQ(should_launch, delegate_.has_launched());
129     ASSERT_EQ(should_block, delegate_.has_blocked());
130   }
131
132   base::MessageLoopForUI ui_message_loop_;
133   content::TestBrowserThread ui_thread_;
134   content::TestBrowserThread file_thread_;
135
136   FakeExternalProtocolHandlerDelegate delegate_;
137 };
138
139 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeDefault) {
140   DoTest(ExternalProtocolHandler::BLOCK,
141          ShellIntegration::IS_DEFAULT,
142          false, false, true);
143 }
144
145 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeNotDefault) {
146   DoTest(ExternalProtocolHandler::BLOCK,
147          ShellIntegration::NOT_DEFAULT,
148          false, false, true);
149 }
150
151 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeUnknown) {
152   DoTest(ExternalProtocolHandler::BLOCK,
153          ShellIntegration::UNKNOWN_DEFAULT,
154          false, false, true);
155 }
156
157 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeDefault) {
158   DoTest(ExternalProtocolHandler::DONT_BLOCK,
159          ShellIntegration::IS_DEFAULT,
160          false, false, true);
161 }
162
163 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeNotDefault) {
164   DoTest(ExternalProtocolHandler::DONT_BLOCK,
165          ShellIntegration::NOT_DEFAULT,
166          false, true, false);
167 }
168
169 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeUnknown) {
170   DoTest(ExternalProtocolHandler::DONT_BLOCK,
171          ShellIntegration::UNKNOWN_DEFAULT,
172          false, true, false);
173 }
174
175 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeDefault) {
176   DoTest(ExternalProtocolHandler::UNKNOWN,
177          ShellIntegration::IS_DEFAULT,
178          false, false, true);
179 }
180
181 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeNotDefault) {
182   DoTest(ExternalProtocolHandler::UNKNOWN,
183          ShellIntegration::NOT_DEFAULT,
184          true, false, false);
185 }
186
187 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeUnknown) {
188   DoTest(ExternalProtocolHandler::UNKNOWN,
189          ShellIntegration::UNKNOWN_DEFAULT,
190          true, false, false);
191 }