OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "chrome/browser/apps/drive/drive_app_provider.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/timer/timer.h" |
| 13 #include "chrome/browser/apps/drive/drive_app_mapping.h" |
| 14 #include "chrome/browser/apps/drive/drive_service_bridge.h" |
| 15 #include "chrome/browser/drive/drive_app_registry.h" |
| 16 #include "chrome/browser/drive/fake_drive_service.h" |
| 17 #include "chrome/browser/extensions/crx_installer.h" |
| 18 #include "chrome/browser/extensions/extension_browsertest.h" |
| 19 #include "chrome/browser/extensions/install_tracker.h" |
| 20 #include "chrome/common/chrome_paths.h" |
| 21 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 22 #include "chrome/common/web_application_info.h" |
| 23 #include "content/public/test/test_utils.h" |
| 24 #include "extensions/browser/extension_registry.h" |
| 25 #include "extensions/browser/extension_system.h" |
| 26 |
| 27 using extensions::AppLaunchInfo; |
| 28 using extensions::Extension; |
| 29 using extensions::ExtensionRegistry; |
| 30 |
| 31 namespace { |
| 32 |
| 33 const char kDriveAppId[] = "drive_app_id"; |
| 34 const char kDriveAppName[] = "Fake Drive App"; |
| 35 const char kLaunchUrl[] = "http://example.com/drive"; |
| 36 |
| 37 // App id of hosted_app.crx. |
| 38 const char kChromeAppId[] = "kbmnembihfiondgfjekmnmcbddelicoi"; |
| 39 |
| 40 // Stub drive service bridge. |
| 41 class TestDriveServiceBridge : public DriveServiceBridge { |
| 42 public: |
| 43 explicit TestDriveServiceBridge(drive::DriveAppRegistry* registry) |
| 44 : registry_(registry) {} |
| 45 virtual ~TestDriveServiceBridge() {} |
| 46 |
| 47 virtual drive::DriveAppRegistry* GetAppRegistry() OVERRIDE { |
| 48 return registry_; |
| 49 } |
| 50 |
| 51 private: |
| 52 drive::DriveAppRegistry* registry_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(TestDriveServiceBridge); |
| 55 }; |
| 56 |
| 57 } // namespace |
| 58 |
| 59 class DriveAppProviderTest : public ExtensionBrowserTest, |
| 60 public extensions::InstallObserver { |
| 61 public: |
| 62 DriveAppProviderTest() {} |
| 63 virtual ~DriveAppProviderTest() {} |
| 64 |
| 65 // ExtensionBrowserTest: |
| 66 virtual void SetUpOnMainThread() OVERRIDE { |
| 67 ExtensionBrowserTest::SetUpOnMainThread(); |
| 68 |
| 69 fake_drive_service_.reset(new drive::FakeDriveService); |
| 70 fake_drive_service_->LoadAppListForDriveApi("drive/applist_empty.json"); |
| 71 apps_registry_.reset( |
| 72 new drive::DriveAppRegistry(fake_drive_service_.get())); |
| 73 |
| 74 provider_.reset(new DriveAppProvider(profile())); |
| 75 provider_->SetDriveServiceBridgeForTest( |
| 76 make_scoped_ptr(new TestDriveServiceBridge(apps_registry_.get())) |
| 77 .PassAs<DriveServiceBridge>()); |
| 78 } |
| 79 |
| 80 virtual void CleanUpOnMainThread() OVERRIDE { |
| 81 provider_.reset(); |
| 82 apps_registry_.reset(); |
| 83 fake_drive_service_.reset(); |
| 84 |
| 85 ExtensionBrowserTest::CleanUpOnMainThread(); |
| 86 } |
| 87 |
| 88 const Extension* InstallChromeApp(int expected_change) { |
| 89 base::FilePath test_data_path; |
| 90 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_path)) { |
| 91 ADD_FAILURE(); |
| 92 return NULL; |
| 93 } |
| 94 test_data_path = |
| 95 test_data_path.AppendASCII("extensions").AppendASCII("hosted_app.crx"); |
| 96 const Extension* extension = |
| 97 InstallExtension(test_data_path, expected_change); |
| 98 return extension; |
| 99 } |
| 100 |
| 101 void RefreshDriveAppRegistry() { |
| 102 apps_registry_->Update(); |
| 103 content::RunAllPendingInMessageLoop(); |
| 104 } |
| 105 |
| 106 void WaitForPendingDriveAppConverters() { |
| 107 DCHECK(!runner_); |
| 108 |
| 109 if (provider_->pending_converters_.empty()) |
| 110 return; |
| 111 |
| 112 runner_ = new content::MessageLoopRunner; |
| 113 |
| 114 pending_drive_app_converter_check_timer_.Start( |
| 115 FROM_HERE, |
| 116 base::TimeDelta::FromMilliseconds(50), |
| 117 base::Bind(&DriveAppProviderTest::OnPendingDriveAppConverterCheckTimer, |
| 118 base::Unretained(this))); |
| 119 |
| 120 runner_->Run(); |
| 121 |
| 122 pending_drive_app_converter_check_timer_.Stop(); |
| 123 runner_ = NULL; |
| 124 } |
| 125 |
| 126 void InstallUserUrlApp(const std::string& url) { |
| 127 DCHECK(!runner_); |
| 128 runner_ = new content::MessageLoopRunner; |
| 129 |
| 130 WebApplicationInfo web_app; |
| 131 web_app.title = base::ASCIIToUTF16("User installed Url app"); |
| 132 web_app.app_url = GURL(url); |
| 133 |
| 134 scoped_refptr<extensions::CrxInstaller> crx_installer = |
| 135 extensions::CrxInstaller::CreateSilent( |
| 136 extensions::ExtensionSystem::Get(profile())->extension_service()); |
| 137 crx_installer->set_creation_flags(Extension::FROM_BOOKMARK); |
| 138 extensions::InstallTracker::Get(profile())->AddObserver(this); |
| 139 crx_installer->InstallWebApp(web_app); |
| 140 |
| 141 runner_->Run(); |
| 142 runner_ = NULL; |
| 143 extensions::InstallTracker::Get(profile())->RemoveObserver(this); |
| 144 |
| 145 content::RunAllPendingInMessageLoop(); |
| 146 } |
| 147 |
| 148 drive::FakeDriveService* fake_drive_service() { |
| 149 return fake_drive_service_.get(); |
| 150 } |
| 151 DriveAppProvider* provider() { return provider_.get(); } |
| 152 DriveAppMapping* mapping() { return provider_->mapping_.get(); } |
| 153 |
| 154 private: |
| 155 void OnPendingDriveAppConverterCheckTimer() { |
| 156 if (provider_->pending_converters_.empty()) |
| 157 runner_->Quit(); |
| 158 } |
| 159 |
| 160 // extensions::InstallObserver |
| 161 virtual void OnFinishCrxInstall(const std::string& extension_id, |
| 162 bool success) OVERRIDE { |
| 163 runner_->Quit(); |
| 164 } |
| 165 |
| 166 scoped_ptr<drive::FakeDriveService> fake_drive_service_; |
| 167 scoped_ptr<drive::DriveAppRegistry> apps_registry_; |
| 168 scoped_ptr<DriveAppProvider> provider_; |
| 169 |
| 170 base::RepeatingTimer<DriveAppProviderTest> |
| 171 pending_drive_app_converter_check_timer_; |
| 172 scoped_refptr<content::MessageLoopRunner> runner_; |
| 173 |
| 174 DISALLOW_COPY_AND_ASSIGN(DriveAppProviderTest); |
| 175 }; |
| 176 |
| 177 // A Drive app maps to an existing Chrome app that has a matching id. |
| 178 // Uninstalling the chrome app would also disconnect the drive app. |
| 179 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, ExistingChromeApp) { |
| 180 // Prepare an existing chrome app. |
| 181 const Extension* chrome_app = InstallChromeApp(1); |
| 182 ASSERT_TRUE(chrome_app); |
| 183 |
| 184 // Prepare a Drive app that matches the chrome app id. |
| 185 fake_drive_service()->AddApp( |
| 186 kDriveAppId, kDriveAppName, chrome_app->id(), kLaunchUrl); |
| 187 RefreshDriveAppRegistry(); |
| 188 WaitForPendingDriveAppConverters(); |
| 189 |
| 190 // The Drive app should use the matching chrome app. |
| 191 EXPECT_EQ(chrome_app->id(), mapping()->GetChromeApp(kDriveAppId)); |
| 192 EXPECT_FALSE(mapping()->IsChromeAppGenerated(chrome_app->id())); |
| 193 |
| 194 // Unintalling chrome app should disconnect the Drive app on server. |
| 195 EXPECT_TRUE(fake_drive_service()->HasApp(kDriveAppId)); |
| 196 UninstallExtension(chrome_app->id()); |
| 197 EXPECT_FALSE(fake_drive_service()->HasApp(kDriveAppId)); |
| 198 } |
| 199 |
| 200 // A Drive app creates an URL app when no matching Chrome app presents. |
| 201 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, CreateUrlApp) { |
| 202 // Prepare a Drive app with no underlying chrome app. |
| 203 fake_drive_service()->AddApp(kDriveAppId, kDriveAppName, "", kLaunchUrl); |
| 204 RefreshDriveAppRegistry(); |
| 205 WaitForPendingDriveAppConverters(); |
| 206 |
| 207 // An Url app should be created. |
| 208 const Extension* chrome_app = |
| 209 ExtensionRegistry::Get(profile())->GetExtensionById( |
| 210 mapping()->GetChromeApp(kDriveAppId), ExtensionRegistry::EVERYTHING); |
| 211 ASSERT_TRUE(chrome_app); |
| 212 EXPECT_EQ(kDriveAppName, chrome_app->name()); |
| 213 EXPECT_TRUE(chrome_app->is_hosted_app()); |
| 214 EXPECT_TRUE(chrome_app->from_bookmark()); |
| 215 EXPECT_EQ(GURL(kLaunchUrl), AppLaunchInfo::GetLaunchWebURL(chrome_app)); |
| 216 |
| 217 EXPECT_EQ(chrome_app->id(), mapping()->GetChromeApp(kDriveAppId)); |
| 218 EXPECT_TRUE(mapping()->IsChromeAppGenerated(chrome_app->id())); |
| 219 |
| 220 // Unintalling the chrome app should disconnect the Drive app on server. |
| 221 EXPECT_TRUE(fake_drive_service()->HasApp(kDriveAppId)); |
| 222 UninstallExtension(chrome_app->id()); |
| 223 EXPECT_FALSE(fake_drive_service()->HasApp(kDriveAppId)); |
| 224 } |
| 225 |
| 226 // A matching Chrome app replaces the created URL app. |
| 227 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, MatchingChromeAppInstalled) { |
| 228 // Prepare a Drive app that matches the not-yet-installed kChromeAppId. |
| 229 fake_drive_service()->AddApp( |
| 230 kDriveAppId, kDriveAppName, kChromeAppId, kLaunchUrl); |
| 231 RefreshDriveAppRegistry(); |
| 232 WaitForPendingDriveAppConverters(); |
| 233 |
| 234 // An Url app should be created. |
| 235 const Extension* url_app = |
| 236 ExtensionRegistry::Get(profile())->GetExtensionById( |
| 237 mapping()->GetChromeApp(kDriveAppId), ExtensionRegistry::EVERYTHING); |
| 238 EXPECT_TRUE(url_app->is_hosted_app()); |
| 239 EXPECT_TRUE(url_app->from_bookmark()); |
| 240 |
| 241 const std::string url_app_id = url_app->id(); |
| 242 EXPECT_NE(kChromeAppId, url_app_id); |
| 243 EXPECT_EQ(url_app_id, mapping()->GetChromeApp(kDriveAppId)); |
| 244 EXPECT_TRUE(mapping()->IsChromeAppGenerated(url_app_id)); |
| 245 |
| 246 // Installs a chrome app with matching id. |
| 247 InstallChromeApp(0); |
| 248 |
| 249 // The Drive app should be mapped to chrome app. |
| 250 EXPECT_EQ(kChromeAppId, mapping()->GetChromeApp(kDriveAppId)); |
| 251 EXPECT_FALSE(mapping()->IsChromeAppGenerated(kChromeAppId)); |
| 252 |
| 253 // Url app should be auto uninstalled. |
| 254 EXPECT_FALSE(ExtensionRegistry::Get(profile())->GetExtensionById( |
| 255 url_app_id, ExtensionRegistry::EVERYTHING)); |
| 256 } |
| 257 |
| 258 // Tests that the corresponding URL app is uninstalled when a Drive app is |
| 259 // disconnected. |
| 260 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, |
| 261 DisconnectDriveAppUninstallUrlApp) { |
| 262 // Prepare a Drive app that matches the not-yet-installed kChromeAppId. |
| 263 fake_drive_service()->AddApp( |
| 264 kDriveAppId, kDriveAppName, kChromeAppId, kLaunchUrl); |
| 265 RefreshDriveAppRegistry(); |
| 266 WaitForPendingDriveAppConverters(); |
| 267 |
| 268 // Url app is created. |
| 269 const std::string url_app_id = mapping()->GetChromeApp(kDriveAppId); |
| 270 EXPECT_TRUE(ExtensionRegistry::Get(profile())->GetExtensionById( |
| 271 url_app_id, ExtensionRegistry::EVERYTHING)); |
| 272 |
| 273 fake_drive_service()->RemoveAppByProductId(kChromeAppId); |
| 274 RefreshDriveAppRegistry(); |
| 275 |
| 276 // Url app is auto uninstalled. |
| 277 EXPECT_FALSE(ExtensionRegistry::Get(profile())->GetExtensionById( |
| 278 url_app_id, ExtensionRegistry::EVERYTHING)); |
| 279 } |
| 280 |
| 281 // Tests that the matching Chrome app is preserved when a Drive app is |
| 282 // disconnected. |
| 283 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, |
| 284 DisconnectDriveAppPreserveChromeApp) { |
| 285 // Prepare an existing chrome app. |
| 286 const Extension* chrome_app = InstallChromeApp(1); |
| 287 ASSERT_TRUE(chrome_app); |
| 288 |
| 289 // Prepare a Drive app that matches the chrome app id. |
| 290 fake_drive_service()->AddApp( |
| 291 kDriveAppId, kDriveAppName, kChromeAppId, kLaunchUrl); |
| 292 RefreshDriveAppRegistry(); |
| 293 WaitForPendingDriveAppConverters(); |
| 294 |
| 295 fake_drive_service()->RemoveAppByProductId(kChromeAppId); |
| 296 RefreshDriveAppRegistry(); |
| 297 |
| 298 // Chrome app is still present after the Drive app is disconnected. |
| 299 EXPECT_TRUE(ExtensionRegistry::Get(profile())->GetExtensionById( |
| 300 kChromeAppId, ExtensionRegistry::EVERYTHING)); |
| 301 } |
| 302 |
| 303 // A new URL app replaces the existing one and keeps existing// position when a |
| 304 // Drive app changes its name or URL. |
| 305 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, DriveAppChanged) { |
| 306 // Prepare a Drive app with no underlying chrome app. |
| 307 fake_drive_service()->AddApp( |
| 308 kDriveAppId, kDriveAppName, kChromeAppId, kLaunchUrl); |
| 309 RefreshDriveAppRegistry(); |
| 310 WaitForPendingDriveAppConverters(); |
| 311 |
| 312 // An Url app should be created. |
| 313 const std::string url_app_id = mapping()->GetChromeApp(kDriveAppId); |
| 314 const Extension* url_app = |
| 315 ExtensionRegistry::Get(profile()) |
| 316 ->GetExtensionById(url_app_id, ExtensionRegistry::EVERYTHING); |
| 317 ASSERT_TRUE(url_app); |
| 318 EXPECT_EQ(kDriveAppName, url_app->name()); |
| 319 EXPECT_TRUE(url_app->is_hosted_app()); |
| 320 EXPECT_TRUE(url_app->from_bookmark()); |
| 321 EXPECT_EQ(GURL(kLaunchUrl), AppLaunchInfo::GetLaunchWebURL(url_app)); |
| 322 |
| 323 // Register the Drive app with a different name and URL. |
| 324 const char kAnotherName[] = "Another drive app name"; |
| 325 const char kAnotherLaunchUrl[] = "http://example.com/another_end_point"; |
| 326 fake_drive_service()->RemoveAppByProductId(kChromeAppId); |
| 327 fake_drive_service()->AddApp( |
| 328 kDriveAppId, kAnotherName, kChromeAppId, kAnotherLaunchUrl); |
| 329 RefreshDriveAppRegistry(); |
| 330 WaitForPendingDriveAppConverters(); |
| 331 |
| 332 // Old URL app should be auto uninstalled. |
| 333 url_app = ExtensionRegistry::Get(profile()) |
| 334 ->GetExtensionById(url_app_id, ExtensionRegistry::EVERYTHING); |
| 335 EXPECT_FALSE(url_app); |
| 336 |
| 337 // New URL app should be used. |
| 338 const std::string new_url_app_id = mapping()->GetChromeApp(kDriveAppId); |
| 339 EXPECT_NE(new_url_app_id, url_app_id); |
| 340 |
| 341 const Extension* new_url_app = |
| 342 ExtensionRegistry::Get(profile()) |
| 343 ->GetExtensionById(new_url_app_id, ExtensionRegistry::EVERYTHING); |
| 344 ASSERT_TRUE(new_url_app); |
| 345 EXPECT_EQ(kAnotherName, new_url_app->name()); |
| 346 EXPECT_TRUE(new_url_app->is_hosted_app()); |
| 347 EXPECT_TRUE(new_url_app->from_bookmark()); |
| 348 EXPECT_EQ(GURL(kAnotherLaunchUrl), |
| 349 AppLaunchInfo::GetLaunchWebURL(new_url_app)); |
| 350 } |
| 351 |
| 352 // An existing URL app is not changed when underlying drive app data (name and |
| 353 // URL) is not changed. |
| 354 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, NoChange) { |
| 355 // Prepare one Drive app. |
| 356 fake_drive_service()->AddApp( |
| 357 kDriveAppId, kDriveAppName, kChromeAppId, kLaunchUrl); |
| 358 RefreshDriveAppRegistry(); |
| 359 WaitForPendingDriveAppConverters(); |
| 360 |
| 361 const std::string url_app_id = mapping()->GetChromeApp(kDriveAppId); |
| 362 const Extension* url_app = |
| 363 ExtensionRegistry::Get(profile()) |
| 364 ->GetExtensionById(url_app_id, ExtensionRegistry::EVERYTHING); |
| 365 |
| 366 // Refresh with no actual change. |
| 367 RefreshDriveAppRegistry(); |
| 368 WaitForPendingDriveAppConverters(); |
| 369 |
| 370 // Url app should remain unchanged. |
| 371 const std::string new_url_app_id = mapping()->GetChromeApp(kDriveAppId); |
| 372 EXPECT_EQ(new_url_app_id, url_app_id); |
| 373 |
| 374 const Extension* new_url_app = |
| 375 ExtensionRegistry::Get(profile()) |
| 376 ->GetExtensionById(new_url_app_id, ExtensionRegistry::EVERYTHING); |
| 377 EXPECT_EQ(url_app, new_url_app); |
| 378 } |
| 379 |
| 380 // User installed url app before Drive app conversion should not be tagged |
| 381 // as generated and not auto uninstalled. |
| 382 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, UserInstalledBeforeDriveApp) { |
| 383 InstallUserUrlApp(kLaunchUrl); |
| 384 |
| 385 fake_drive_service()->AddApp( |
| 386 kDriveAppId, kDriveAppName, kChromeAppId, kLaunchUrl); |
| 387 RefreshDriveAppRegistry(); |
| 388 WaitForPendingDriveAppConverters(); |
| 389 |
| 390 const std::string url_app_id = mapping()->GetChromeApp(kDriveAppId); |
| 391 EXPECT_FALSE(mapping()->IsChromeAppGenerated(url_app_id)); |
| 392 |
| 393 fake_drive_service()->RemoveAppByProductId(kChromeAppId); |
| 394 RefreshDriveAppRegistry(); |
| 395 |
| 396 // Url app is still present after the Drive app is disconnected. |
| 397 EXPECT_TRUE(ExtensionRegistry::Get(profile())->GetExtensionById( |
| 398 url_app_id, ExtensionRegistry::EVERYTHING)); |
| 399 } |
| 400 |
| 401 // Similar to UserInstalledBeforeDriveApp but test the case where user |
| 402 // installation happens after Drive app conversion. |
| 403 IN_PROC_BROWSER_TEST_F(DriveAppProviderTest, UserInstalledAfterDriveApp) { |
| 404 fake_drive_service()->AddApp( |
| 405 kDriveAppId, kDriveAppName, kChromeAppId, kLaunchUrl); |
| 406 RefreshDriveAppRegistry(); |
| 407 WaitForPendingDriveAppConverters(); |
| 408 |
| 409 // Drive app converted and tagged as generated. |
| 410 const std::string url_app_id = mapping()->GetChromeApp(kDriveAppId); |
| 411 EXPECT_TRUE(mapping()->IsChromeAppGenerated(url_app_id)); |
| 412 |
| 413 // User installation resets the generated flag. |
| 414 InstallUserUrlApp(kLaunchUrl); |
| 415 EXPECT_FALSE(mapping()->IsChromeAppGenerated(url_app_id)); |
| 416 |
| 417 fake_drive_service()->RemoveAppByProductId(kChromeAppId); |
| 418 RefreshDriveAppRegistry(); |
| 419 |
| 420 // Url app is still present after the Drive app is disconnected. |
| 421 EXPECT_TRUE(ExtensionRegistry::Get(profile())->GetExtensionById( |
| 422 url_app_id, ExtensionRegistry::EVERYTHING)); |
| 423 } |
OLD | NEW |