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