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/extension_app_shim_handler_mac.h" | 5 #include "apps/app_shim/extension_app_shim_handler_mac.h" |
6 | 6 |
7 #include "apps/app_shim/app_shim_host_mac.h" | 7 #include "apps/app_shim/app_shim_host_mac.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/common/extensions/extension.h" | |
10 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
11 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace apps { | 16 namespace apps { |
16 | 17 |
18 using extensions::Extension; | |
19 typedef extensions::ShellWindowRegistry::ShellWindowList ShellWindowList; | |
20 | |
21 using ::testing::_; | |
17 using ::testing::Return; | 22 using ::testing::Return; |
18 | 23 |
19 class MockProfileManagerFacade | 24 class MockDelegate |
20 : public ExtensionAppShimHandler::ProfileManagerFacade { | 25 : public ExtensionAppShimHandler::Delegate { |
tapted
2013/06/21 04:30:47
nit: one line?
jackhou1
2013/06/21 05:46:27
Done.
| |
21 public: | 26 public: |
22 virtual ~MockProfileManagerFacade() {} | 27 virtual ~MockDelegate() {} |
23 | 28 |
24 MOCK_METHOD1(ProfileExistsForPath, bool(const base::FilePath& path)); | 29 MOCK_METHOD1(ProfileExistsForPath, bool(const base::FilePath&)); |
25 MOCK_METHOD1(ProfileForPath, Profile*(const base::FilePath& path)); | 30 MOCK_METHOD1(ProfileForPath, Profile*(const base::FilePath&)); |
31 | |
32 MOCK_METHOD2(GetWindows, ShellWindowList(Profile*, const std::string&)); | |
33 | |
34 MOCK_METHOD2(GetExtension, const Extension*(Profile*, const std::string&)); | |
35 MOCK_METHOD2(LaunchApp, content::WebContents*(Profile*, const Extension*)); | |
36 MOCK_METHOD2(LaunchShim, void(Profile*, const Extension*)); | |
37 | |
26 }; | 38 }; |
27 | 39 |
28 class TestingExtensionAppShimHandler : public ExtensionAppShimHandler { | 40 class TestingExtensionAppShimHandler : public ExtensionAppShimHandler { |
29 public: | 41 public: |
30 TestingExtensionAppShimHandler(ProfileManagerFacade* profile_manager_facade) { | 42 TestingExtensionAppShimHandler(Delegate* dependency_facade) { |
31 set_profile_manager_facade(profile_manager_facade); | 43 set_dependency_facade(dependency_facade); |
32 } | 44 } |
33 virtual ~TestingExtensionAppShimHandler() {} | 45 virtual ~TestingExtensionAppShimHandler() {} |
34 | 46 |
35 MOCK_METHOD3(LaunchApp, bool(Profile*, | 47 MOCK_METHOD1(OnShimFocus, void(Host* host)); |
36 const std::string&, | |
37 AppShimLaunchType)); | |
38 | 48 |
39 AppShimHandler::Host* FindHost(Profile* profile, | 49 AppShimHandler::Host* FindHost(Profile* profile, |
40 const std::string& app_id) { | 50 const std::string& app_id) { |
41 HostMap::const_iterator it = hosts().find(make_pair(profile, app_id)); | 51 HostMap::const_iterator it = hosts().find(make_pair(profile, app_id)); |
42 return it == hosts().end() ? NULL : it->second; | 52 return it == hosts().end() ? NULL : it->second; |
43 } | 53 } |
44 | 54 |
45 content::NotificationRegistrar& GetRegistrar() { return registrar(); } | 55 content::NotificationRegistrar& GetRegistrar() { return registrar(); } |
46 | 56 |
47 private: | 57 private: |
(...skipping 29 matching lines...) Expand all Loading... | |
77 std::string app_id_; | 87 std::string app_id_; |
78 TestingExtensionAppShimHandler* handler_; | 88 TestingExtensionAppShimHandler* handler_; |
79 int close_count_; | 89 int close_count_; |
80 | 90 |
81 DISALLOW_COPY_AND_ASSIGN(FakeHost); | 91 DISALLOW_COPY_AND_ASSIGN(FakeHost); |
82 }; | 92 }; |
83 | 93 |
84 class ExtensionAppShimHandlerTest : public testing::Test { | 94 class ExtensionAppShimHandlerTest : public testing::Test { |
85 protected: | 95 protected: |
86 ExtensionAppShimHandlerTest() | 96 ExtensionAppShimHandlerTest() |
87 : profile_manager_facade_(new MockProfileManagerFacade), | 97 : delegate_(new MockDelegate), |
88 handler_(new TestingExtensionAppShimHandler(profile_manager_facade_)), | 98 handler_(new TestingExtensionAppShimHandler(delegate_)), |
89 profile_path_a_("Profile A"), | 99 profile_path_a_("Profile A"), |
90 profile_path_b_("Profile B"), | 100 profile_path_b_("Profile B"), |
91 host_aa_(profile_path_a_, kTestAppIdA, handler_.get()), | 101 host_aa_(profile_path_a_, kTestAppIdA, handler_.get()), |
92 host_ab_(profile_path_a_, kTestAppIdB, handler_.get()), | 102 host_ab_(profile_path_a_, kTestAppIdB, handler_.get()), |
93 host_bb_(profile_path_b_, kTestAppIdB, handler_.get()), | 103 host_bb_(profile_path_b_, kTestAppIdB, handler_.get()), |
94 host_aa_duplicate_(profile_path_a_, kTestAppIdA, handler_.get()) { | 104 host_aa_duplicate_(profile_path_a_, kTestAppIdA, handler_.get()) { |
95 EXPECT_CALL(*profile_manager_facade_, ProfileExistsForPath(profile_path_a_)) | 105 base::FilePath extension_path("/fake/path"); |
106 base::DictionaryValue manifest; | |
107 manifest.SetString("name", "Fake Name"); | |
108 manifest.SetString("version", "1"); | |
109 std::string error; | |
110 extension_a_ = Extension::Create( | |
111 extension_path, extensions::Manifest::INTERNAL, manifest, | |
112 Extension::NO_FLAGS, kTestAppIdA, &error); | |
113 EXPECT_TRUE(extension_a_.get()) << error; | |
114 | |
115 extension_b_ = Extension::Create( | |
116 extension_path, extensions::Manifest::INTERNAL, manifest, | |
117 Extension::NO_FLAGS, kTestAppIdB, &error); | |
118 EXPECT_TRUE(extension_b_.get()) << error; | |
119 | |
120 EXPECT_CALL(*delegate_, ProfileExistsForPath(profile_path_a_)) | |
96 .WillRepeatedly(Return(true)); | 121 .WillRepeatedly(Return(true)); |
97 EXPECT_CALL(*profile_manager_facade_, ProfileForPath(profile_path_a_)) | 122 EXPECT_CALL(*delegate_, ProfileForPath(profile_path_a_)) |
98 .WillRepeatedly(Return(&profile_a_)); | 123 .WillRepeatedly(Return(&profile_a_)); |
99 EXPECT_CALL(*profile_manager_facade_, ProfileExistsForPath(profile_path_b_)) | 124 EXPECT_CALL(*delegate_, ProfileExistsForPath(profile_path_b_)) |
100 .WillRepeatedly(Return(true)); | 125 .WillRepeatedly(Return(true)); |
101 EXPECT_CALL(*profile_manager_facade_, ProfileForPath(profile_path_b_)) | 126 EXPECT_CALL(*delegate_, ProfileForPath(profile_path_b_)) |
102 .WillRepeatedly(Return(&profile_b_)); | 127 .WillRepeatedly(Return(&profile_b_)); |
128 | |
129 // In most tests, we don't care about the result of GetWindows, it just | |
130 // needs to be non-empty. | |
131 ShellWindowList shell_window_list; | |
132 shell_window_list.push_back(static_cast<ShellWindow*>(NULL)); | |
133 EXPECT_CALL(*delegate_, GetWindows(_, _)) | |
134 .WillRepeatedly(Return(shell_window_list)); | |
135 | |
136 EXPECT_CALL(*delegate_, GetExtension(_, kTestAppIdA)) | |
137 .WillRepeatedly(Return(extension_a_.get())); | |
138 EXPECT_CALL(*delegate_, GetExtension(_, kTestAppIdB)) | |
139 .WillRepeatedly(Return(extension_b_.get())); | |
140 EXPECT_CALL(*delegate_, LaunchApp(_, _)) | |
141 .WillRepeatedly(Return(static_cast<content::WebContents*>(NULL))); | |
103 } | 142 } |
104 | 143 |
105 MockProfileManagerFacade* profile_manager_facade_; | 144 MockDelegate* delegate_; |
106 scoped_ptr<TestingExtensionAppShimHandler> handler_; | 145 scoped_ptr<TestingExtensionAppShimHandler> handler_; |
107 base::FilePath profile_path_a_; | 146 base::FilePath profile_path_a_; |
108 base::FilePath profile_path_b_; | 147 base::FilePath profile_path_b_; |
109 TestingProfile profile_a_; | 148 TestingProfile profile_a_; |
110 TestingProfile profile_b_; | 149 TestingProfile profile_b_; |
111 FakeHost host_aa_; | 150 FakeHost host_aa_; |
112 FakeHost host_ab_; | 151 FakeHost host_ab_; |
113 FakeHost host_bb_; | 152 FakeHost host_bb_; |
114 FakeHost host_aa_duplicate_; | 153 FakeHost host_aa_duplicate_; |
154 scoped_refptr<Extension> extension_a_; | |
155 scoped_refptr<Extension> extension_b_; | |
115 | 156 |
116 private: | 157 private: |
117 DISALLOW_COPY_AND_ASSIGN(ExtensionAppShimHandlerTest); | 158 DISALLOW_COPY_AND_ASSIGN(ExtensionAppShimHandlerTest); |
118 }; | 159 }; |
119 | 160 |
120 TEST_F(ExtensionAppShimHandlerTest, LaunchAndCloseShim) { | 161 TEST_F(ExtensionAppShimHandlerTest, LaunchAndCloseShim) { |
121 const AppShimLaunchType normal_launch = APP_SHIM_LAUNCH_NORMAL; | 162 const AppShimLaunchType normal_launch = APP_SHIM_LAUNCH_NORMAL; |
122 | 163 |
123 // If launch fails, the host is not added to the map. | 164 // If launch fails, the host is not added to the map. |
124 EXPECT_CALL(*handler_, LaunchApp(&profile_a_, kTestAppIdA, normal_launch)) | 165 EXPECT_CALL(*delegate_, GetExtension(&profile_a_, kTestAppIdA)) |
125 .WillOnce(Return(false)); | 166 .WillOnce(Return(static_cast<const Extension*>(NULL))) |
167 .WillRepeatedly(Return(extension_a_.get())); | |
126 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_, normal_launch)); | 168 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_, normal_launch)); |
127 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); | 169 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); |
128 | 170 |
129 // Normal startup. | 171 // Normal startup. |
130 EXPECT_CALL(*handler_, LaunchApp(&profile_a_, kTestAppIdA, normal_launch)) | |
131 .WillOnce(Return(true)); | |
132 EXPECT_EQ(true, handler_->OnShimLaunch(&host_aa_, normal_launch)); | 172 EXPECT_EQ(true, handler_->OnShimLaunch(&host_aa_, normal_launch)); |
133 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); | 173 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); |
134 | 174 |
135 EXPECT_CALL(*handler_, LaunchApp(&profile_a_, kTestAppIdB, normal_launch)) | |
136 .WillOnce(Return(true)); | |
137 EXPECT_EQ(true, handler_->OnShimLaunch(&host_ab_, normal_launch)); | 175 EXPECT_EQ(true, handler_->OnShimLaunch(&host_ab_, normal_launch)); |
138 EXPECT_EQ(&host_ab_, handler_->FindHost(&profile_a_, kTestAppIdB)); | 176 EXPECT_EQ(&host_ab_, handler_->FindHost(&profile_a_, kTestAppIdB)); |
139 | 177 |
140 EXPECT_CALL(*handler_, LaunchApp(&profile_b_, kTestAppIdB, normal_launch)) | |
141 .WillOnce(Return(true)); | |
142 EXPECT_EQ(true, handler_->OnShimLaunch(&host_bb_, normal_launch)); | 178 EXPECT_EQ(true, handler_->OnShimLaunch(&host_bb_, normal_launch)); |
143 EXPECT_EQ(&host_bb_, handler_->FindHost(&profile_b_, kTestAppIdB)); | 179 EXPECT_EQ(&host_bb_, handler_->FindHost(&profile_b_, kTestAppIdB)); |
144 | 180 |
145 // Starting and closing a second host does nothing. | 181 // Starting and closing a second host just focuses the app. |
146 EXPECT_CALL(*handler_, LaunchApp(&profile_a_, kTestAppIdA, normal_launch)) | 182 EXPECT_CALL(*handler_, OnShimFocus(&host_aa_duplicate_)); |
147 .WillOnce(Return(false)); | |
148 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_duplicate_, normal_launch)); | 183 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_duplicate_, normal_launch)); |
149 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); | 184 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); |
150 handler_->OnShimClose(&host_aa_duplicate_); | 185 handler_->OnShimClose(&host_aa_duplicate_); |
151 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); | 186 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); |
152 | 187 |
153 // Normal close. | 188 // Normal close. |
154 handler_->OnShimClose(&host_aa_); | 189 handler_->OnShimClose(&host_aa_); |
155 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); | 190 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); |
156 | 191 |
157 // Closing the second host afterward does nothing. | 192 // Closing the second host afterward does nothing. |
158 handler_->OnShimClose(&host_aa_duplicate_); | 193 handler_->OnShimClose(&host_aa_duplicate_); |
159 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); | 194 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); |
160 } | 195 } |
161 | 196 |
197 TEST_F(ExtensionAppShimHandlerTest, AppLifetime) { | |
198 EXPECT_CALL(*delegate_, LaunchShim(&profile_a_, extension_a_.get())); | |
199 handler_->OnAppActivated(&profile_a_, kTestAppIdA); | |
200 | |
201 // Launch the shim, adding an entry in the map. | |
202 EXPECT_EQ(true, handler_->OnShimLaunch(&host_aa_, APP_SHIM_LAUNCH_NORMAL)); | |
203 EXPECT_EQ(&host_aa_, handler_->FindHost(&profile_a_, kTestAppIdA)); | |
204 | |
205 handler_->OnAppDeactivated(&profile_a_, kTestAppIdA); | |
206 EXPECT_EQ(1, host_aa_.close_count()); | |
207 } | |
208 | |
209 TEST_F(ExtensionAppShimHandlerTest, RegisterOnly) { | |
210 // For an APP_SHIM_LAUNCH_REGISTER_ONLY, don't launch the app. | |
211 EXPECT_CALL(*delegate_, LaunchApp(_, _)) | |
212 .Times(0); | |
213 EXPECT_EQ(true, handler_->OnShimLaunch(&host_aa_, | |
214 APP_SHIM_LAUNCH_REGISTER_ONLY)); | |
215 EXPECT_TRUE(handler_->FindHost(&profile_a_, kTestAppIdA)); | |
216 | |
217 // Close the shim, removing the entry in the map. | |
218 handler_->OnShimClose(&host_aa_); | |
219 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); | |
220 | |
221 // Don't register if there are no windows open for the app. This can happen if | |
222 // the app shim was launched in response to an app being activated, but the | |
223 // app was deactivated before the shim is registered. | |
224 ShellWindowList shell_window_list; | |
225 EXPECT_CALL(*delegate_, GetWindows(&profile_a_, kTestAppIdA)) | |
226 .WillRepeatedly(Return(shell_window_list)); | |
227 EXPECT_EQ(false, handler_->OnShimLaunch(&host_aa_, | |
228 APP_SHIM_LAUNCH_REGISTER_ONLY)); | |
229 EXPECT_FALSE(handler_->FindHost(&profile_a_, kTestAppIdA)); | |
230 } | |
231 | |
162 } // namespace apps | 232 } // namespace apps |
OLD | NEW |