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