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