1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "apps/app_shim/app_shim_host_mac.h"
#include <vector>
#include "apps/app_shim/app_shim_messages.h"
#include "base/basictypes.h"
#include "base/memory/scoped_vector.h"
#include "ipc/ipc_message.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class TestingAppShimHost : public AppShimHost {
public:
TestingAppShimHost() {}
virtual ~TestingAppShimHost() {}
bool ReceiveMessage(IPC::Message* message);
const std::vector<IPC::Message*>& sent_messages() {
return sent_messages_.get();
}
protected:
virtual bool Send(IPC::Message* message) OVERRIDE;
private:
ScopedVector<IPC::Message> sent_messages_;
DISALLOW_COPY_AND_ASSIGN(TestingAppShimHost);
};
bool TestingAppShimHost::ReceiveMessage(IPC::Message* message) {
bool handled = OnMessageReceived(*message);
delete message;
return handled;
}
bool TestingAppShimHost::Send(IPC::Message* message) {
sent_messages_.push_back(message);
return true;
}
const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const char kTestProfileDir[] = "Profile 1";
class AppShimHostTest : public testing::Test,
public apps::AppShimHandler {
public:
AppShimHostTest() : launch_result_(apps::APP_SHIM_LAUNCH_SUCCESS),
launch_count_(0),
launch_now_count_(0),
close_count_(0),
focus_count_(0),
quit_count_(0) {}
TestingAppShimHost* host() { return host_.get(); }
void LaunchApp(apps::AppShimLaunchType launch_type) {
EXPECT_TRUE(host()->ReceiveMessage(
new AppShimHostMsg_LaunchApp(base::FilePath(kTestProfileDir),
kTestAppId,
launch_type,
std::vector<base::FilePath>())));
}
apps::AppShimLaunchResult GetLaunchResult() {
EXPECT_EQ(1u, host()->sent_messages().size());
IPC::Message* message = host()->sent_messages()[0];
EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID, message->type());
AppShimMsg_LaunchApp_Done::Param param;
AppShimMsg_LaunchApp_Done::Read(message, ¶m);
return param.a;
}
void SimulateDisconnect() {
implicit_cast<IPC::Listener*>(host_.release())->OnChannelError();
}
protected:
virtual void OnShimLaunch(Host* host,
apps::AppShimLaunchType launch_type,
const std::vector<base::FilePath>& file) OVERRIDE {
++launch_count_;
if (launch_type == apps::APP_SHIM_LAUNCH_NORMAL)
++launch_now_count_;
host->OnAppLaunchComplete(launch_result_);
}
virtual void OnShimClose(Host* host) OVERRIDE { ++close_count_; }
virtual void OnShimFocus(Host* host,
apps::AppShimFocusType focus_type,
const std::vector<base::FilePath>& file) OVERRIDE {
++focus_count_;
}
virtual void OnShimSetHidden(Host* host, bool hidden) OVERRIDE {}
virtual void OnShimQuit(Host* host) OVERRIDE { ++quit_count_; }
apps::AppShimLaunchResult launch_result_;
int launch_count_;
int launch_now_count_;
int close_count_;
int focus_count_;
int quit_count_;
private:
virtual void SetUp() OVERRIDE {
testing::Test::SetUp();
host_.reset(new TestingAppShimHost());
}
scoped_ptr<TestingAppShimHost> host_;
DISALLOW_COPY_AND_ASSIGN(AppShimHostTest);
};
} // namespace
TEST_F(AppShimHostTest, TestLaunchAppWithHandler) {
apps::AppShimHandler::RegisterHandler(kTestAppId, this);
LaunchApp(apps::APP_SHIM_LAUNCH_NORMAL);
EXPECT_EQ(kTestAppId,
implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId());
EXPECT_EQ(apps::APP_SHIM_LAUNCH_SUCCESS, GetLaunchResult());
EXPECT_EQ(1, launch_count_);
EXPECT_EQ(1, launch_now_count_);
EXPECT_EQ(0, focus_count_);
EXPECT_EQ(0, close_count_);
// A second OnAppLaunchComplete is ignored.
implicit_cast<apps::AppShimHandler::Host*>(host())->
OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_APP_NOT_FOUND);
EXPECT_EQ(apps::APP_SHIM_LAUNCH_SUCCESS, GetLaunchResult());
EXPECT_TRUE(host()->ReceiveMessage(
new AppShimHostMsg_FocusApp(apps::APP_SHIM_FOCUS_NORMAL,
std::vector<base::FilePath>())));
EXPECT_EQ(1, focus_count_);
EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_QuitApp()));
EXPECT_EQ(1, quit_count_);
SimulateDisconnect();
EXPECT_EQ(1, close_count_);
apps::AppShimHandler::RemoveHandler(kTestAppId);
}
TEST_F(AppShimHostTest, TestNoLaunchNow) {
apps::AppShimHandler::RegisterHandler(kTestAppId, this);
LaunchApp(apps::APP_SHIM_LAUNCH_REGISTER_ONLY);
EXPECT_EQ(kTestAppId,
implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId());
EXPECT_EQ(apps::APP_SHIM_LAUNCH_SUCCESS, GetLaunchResult());
EXPECT_EQ(1, launch_count_);
EXPECT_EQ(0, launch_now_count_);
EXPECT_EQ(0, focus_count_);
EXPECT_EQ(0, close_count_);
apps::AppShimHandler::RemoveHandler(kTestAppId);
}
TEST_F(AppShimHostTest, TestFailLaunch) {
apps::AppShimHandler::RegisterHandler(kTestAppId, this);
launch_result_ = apps::APP_SHIM_LAUNCH_APP_NOT_FOUND;
LaunchApp(apps::APP_SHIM_LAUNCH_NORMAL);
EXPECT_EQ(apps::APP_SHIM_LAUNCH_APP_NOT_FOUND, GetLaunchResult());
apps::AppShimHandler::RemoveHandler(kTestAppId);
}
|