Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / apps / app_shim / app_shim_host_mac_unittest.cc
blobb4b56fb676119cb378eccabb7359b81aeeb3376d
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 "apps/app_shim/app_shim_host_mac.h"
7 #include <vector>
9 #include "apps/app_shim/app_shim_messages.h"
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_vector.h"
12 #include "ipc/ipc_message.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace {
17 class TestingAppShimHost : public AppShimHost {
18 public:
19 TestingAppShimHost() {}
20 virtual ~TestingAppShimHost() {}
22 bool ReceiveMessage(IPC::Message* message);
24 const std::vector<IPC::Message*>& sent_messages() {
25 return sent_messages_.get();
28 protected:
29 virtual bool Send(IPC::Message* message) OVERRIDE;
31 private:
32 ScopedVector<IPC::Message> sent_messages_;
34 DISALLOW_COPY_AND_ASSIGN(TestingAppShimHost);
37 bool TestingAppShimHost::ReceiveMessage(IPC::Message* message) {
38 bool handled = OnMessageReceived(*message);
39 delete message;
40 return handled;
43 bool TestingAppShimHost::Send(IPC::Message* message) {
44 sent_messages_.push_back(message);
45 return true;
48 const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
49 const char kTestProfileDir[] = "Profile 1";
51 class AppShimHostTest : public testing::Test,
52 public apps::AppShimHandler {
53 public:
54 AppShimHostTest() : launch_result_(apps::APP_SHIM_LAUNCH_SUCCESS),
55 launch_count_(0),
56 launch_now_count_(0),
57 close_count_(0),
58 focus_count_(0),
59 quit_count_(0) {}
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),
66 kTestAppId,
67 launch_type,
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, &param);
77 return param.a;
80 void SimulateDisconnect() {
81 implicit_cast<IPC::Listener*>(host_.release())->OnChannelError();
84 protected:
85 virtual void OnShimLaunch(Host* host,
86 apps::AppShimLaunchType launch_type,
87 const std::vector<base::FilePath>& file) OVERRIDE {
88 ++launch_count_;
89 if (launch_type == apps::APP_SHIM_LAUNCH_NORMAL)
90 ++launch_now_count_;
91 host->OnAppLaunchComplete(launch_result_);
94 virtual void OnShimClose(Host* host) OVERRIDE { ++close_count_; }
96 virtual void OnShimFocus(Host* host,
97 apps::AppShimFocusType focus_type,
98 const std::vector<base::FilePath>& file) OVERRIDE {
99 ++focus_count_;
102 virtual void OnShimSetHidden(Host* host, bool hidden) OVERRIDE {}
104 virtual void OnShimQuit(Host* host) OVERRIDE { ++quit_count_; }
106 apps::AppShimLaunchResult launch_result_;
107 int launch_count_;
108 int launch_now_count_;
109 int close_count_;
110 int focus_count_;
111 int quit_count_;
113 private:
114 virtual void SetUp() OVERRIDE {
115 testing::Test::SetUp();
116 host_.reset(new TestingAppShimHost());
119 scoped_ptr<TestingAppShimHost> host_;
121 DISALLOW_COPY_AND_ASSIGN(AppShimHostTest);
125 } // namespace
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);