1 // Copyright 2013 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.
5 #include "chrome/browser/apps/app_shim/app_shim_host_mac.h"
9 #include "base/basictypes.h"
10 #include "base/memory/scoped_vector.h"
11 #include "chrome/common/mac/app_shim_messages.h"
12 #include "ipc/ipc_message.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class TestingAppShimHost
: public AppShimHost
{
19 TestingAppShimHost() {}
20 ~TestingAppShimHost() override
{}
22 bool ReceiveMessage(IPC::Message
* message
);
24 const std::vector
<IPC::Message
*>& sent_messages() {
25 return sent_messages_
.get();
29 bool Send(IPC::Message
* message
) override
;
32 ScopedVector
<IPC::Message
> sent_messages_
;
34 DISALLOW_COPY_AND_ASSIGN(TestingAppShimHost
);
37 bool TestingAppShimHost::ReceiveMessage(IPC::Message
* message
) {
38 bool handled
= OnMessageReceived(*message
);
43 bool TestingAppShimHost::Send(IPC::Message
* message
) {
44 sent_messages_
.push_back(message
);
48 const char kTestAppId
[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
49 const char kTestProfileDir
[] = "Profile 1";
51 class AppShimHostTest
: public testing::Test
,
52 public apps::AppShimHandler
{
54 AppShimHostTest() : launch_result_(apps::APP_SHIM_LAUNCH_SUCCESS
),
61 TestingAppShimHost
* host() { return host_
.get(); }
63 void LaunchApp(apps::AppShimLaunchType launch_type
) {
64 EXPECT_TRUE(host()->ReceiveMessage(
65 new AppShimHostMsg_LaunchApp(base::FilePath(kTestProfileDir
),
68 std::vector
<base::FilePath
>())));
71 apps::AppShimLaunchResult
GetLaunchResult() {
72 EXPECT_EQ(1u, host()->sent_messages().size());
73 IPC::Message
* message
= host()->sent_messages()[0];
74 EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID
, message
->type());
75 AppShimMsg_LaunchApp_Done::Param param
;
76 AppShimMsg_LaunchApp_Done::Read(message
, ¶m
);
77 return base::get
<0>(param
);
80 void SimulateDisconnect() {
81 implicit_cast
<IPC::Listener
*>(host_
.release())->OnChannelError();
85 void OnShimLaunch(Host
* host
,
86 apps::AppShimLaunchType launch_type
,
87 const std::vector
<base::FilePath
>& file
) override
{
89 if (launch_type
== apps::APP_SHIM_LAUNCH_NORMAL
)
91 host
->OnAppLaunchComplete(launch_result_
);
94 void OnShimClose(Host
* host
) override
{ ++close_count_
; }
96 void OnShimFocus(Host
* host
,
97 apps::AppShimFocusType focus_type
,
98 const std::vector
<base::FilePath
>& file
) override
{
102 void OnShimSetHidden(Host
* host
, bool hidden
) override
{}
104 void OnShimQuit(Host
* host
) override
{ ++quit_count_
; }
106 apps::AppShimLaunchResult launch_result_
;
108 int launch_now_count_
;
114 void SetUp() override
{
115 testing::Test::SetUp();
116 host_
.reset(new TestingAppShimHost());
119 scoped_ptr
<TestingAppShimHost
> host_
;
121 DISALLOW_COPY_AND_ASSIGN(AppShimHostTest
);
127 TEST_F(AppShimHostTest
, TestLaunchAppWithHandler
) {
128 apps::AppShimHandler::RegisterHandler(kTestAppId
, this);
129 LaunchApp(apps::APP_SHIM_LAUNCH_NORMAL
);
130 EXPECT_EQ(kTestAppId
,
131 implicit_cast
<apps::AppShimHandler::Host
*>(host())->GetAppId());
132 EXPECT_EQ(apps::APP_SHIM_LAUNCH_SUCCESS
, GetLaunchResult());
133 EXPECT_EQ(1, launch_count_
);
134 EXPECT_EQ(1, launch_now_count_
);
135 EXPECT_EQ(0, focus_count_
);
136 EXPECT_EQ(0, close_count_
);
138 // A second OnAppLaunchComplete is ignored.
139 implicit_cast
<apps::AppShimHandler::Host
*>(host())->
140 OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_APP_NOT_FOUND
);
141 EXPECT_EQ(apps::APP_SHIM_LAUNCH_SUCCESS
, GetLaunchResult());
143 EXPECT_TRUE(host()->ReceiveMessage(
144 new AppShimHostMsg_FocusApp(apps::APP_SHIM_FOCUS_NORMAL
,
145 std::vector
<base::FilePath
>())));
146 EXPECT_EQ(1, focus_count_
);
148 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_QuitApp()));
149 EXPECT_EQ(1, quit_count_
);
151 SimulateDisconnect();
152 EXPECT_EQ(1, close_count_
);
153 apps::AppShimHandler::RemoveHandler(kTestAppId
);
156 TEST_F(AppShimHostTest
, TestNoLaunchNow
) {
157 apps::AppShimHandler::RegisterHandler(kTestAppId
, this);
158 LaunchApp(apps::APP_SHIM_LAUNCH_REGISTER_ONLY
);
159 EXPECT_EQ(kTestAppId
,
160 implicit_cast
<apps::AppShimHandler::Host
*>(host())->GetAppId());
161 EXPECT_EQ(apps::APP_SHIM_LAUNCH_SUCCESS
, GetLaunchResult());
162 EXPECT_EQ(1, launch_count_
);
163 EXPECT_EQ(0, launch_now_count_
);
164 EXPECT_EQ(0, focus_count_
);
165 EXPECT_EQ(0, close_count_
);
166 apps::AppShimHandler::RemoveHandler(kTestAppId
);
169 TEST_F(AppShimHostTest
, TestFailLaunch
) {
170 apps::AppShimHandler::RegisterHandler(kTestAppId
, this);
171 launch_result_
= apps::APP_SHIM_LAUNCH_APP_NOT_FOUND
;
172 LaunchApp(apps::APP_SHIM_LAUNCH_NORMAL
);
173 EXPECT_EQ(apps::APP_SHIM_LAUNCH_APP_NOT_FOUND
, GetLaunchResult());
174 apps::AppShimHandler::RemoveHandler(kTestAppId
);