| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/chromeos/drive/drive_app_registry.h" | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "chrome/browser/drive/fake_drive_service.h" | |
| 10 #include "content/public/test/test_browser_thread_bundle.h" | |
| 11 #include "google_apis/drive/drive_api_parser.h" | |
| 12 #include "google_apis/drive/gdata_wapi_parser.h" | |
| 13 #include "google_apis/drive/test_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace drive { | |
| 17 | |
| 18 class DriveAppRegistryTest : public testing::Test { | |
| 19 protected: | |
| 20 virtual void SetUp() OVERRIDE { | |
| 21 fake_drive_service_.reset(new FakeDriveService); | |
| 22 fake_drive_service_->LoadAppListForDriveApi("drive/applist.json"); | |
| 23 | |
| 24 apps_registry_.reset(new DriveAppRegistry(fake_drive_service_.get())); | |
| 25 } | |
| 26 | |
| 27 bool VerifyApp(const std::vector<DriveAppInfo>& list, | |
| 28 const std::string& app_id, | |
| 29 const std::string& app_name) { | |
| 30 bool found = false; | |
| 31 for (size_t i = 0; i < list.size(); ++i) { | |
| 32 const DriveAppInfo& app = list[i]; | |
| 33 if (app_id == app.app_id) { | |
| 34 EXPECT_EQ(app_name, app.app_name); | |
| 35 found = true; | |
| 36 break; | |
| 37 } | |
| 38 } | |
| 39 EXPECT_TRUE(found) << "Unable to find app with app_id " << app_id; | |
| 40 return found; | |
| 41 } | |
| 42 | |
| 43 content::TestBrowserThreadBundle thread_bundle_; | |
| 44 scoped_ptr<FakeDriveService> fake_drive_service_; | |
| 45 scoped_ptr<DriveAppRegistry> apps_registry_; | |
| 46 }; | |
| 47 | |
| 48 TEST_F(DriveAppRegistryTest, LoadAndFindDriveApps) { | |
| 49 apps_registry_->Update(); | |
| 50 base::RunLoop().RunUntilIdle(); | |
| 51 | |
| 52 // Find by primary extension 'exe'. | |
| 53 std::vector<DriveAppInfo> ext_results; | |
| 54 base::FilePath ext_file(FILE_PATH_LITERAL("drive/file.exe")); | |
| 55 apps_registry_->GetAppsForFile(ext_file.Extension(), "", &ext_results); | |
| 56 ASSERT_EQ(1U, ext_results.size()); | |
| 57 VerifyApp(ext_results, "123456788192", "Drive app 1"); | |
| 58 | |
| 59 // Find by primary MIME type. | |
| 60 std::vector<DriveAppInfo> primary_app; | |
| 61 apps_registry_->GetAppsForFile(base::FilePath::StringType(), | |
| 62 "application/vnd.google-apps.drive-sdk.123456788192", &primary_app); | |
| 63 ASSERT_EQ(1U, primary_app.size()); | |
| 64 VerifyApp(primary_app, "123456788192", "Drive app 1"); | |
| 65 | |
| 66 // Find by secondary MIME type. | |
| 67 std::vector<DriveAppInfo> secondary_app; | |
| 68 apps_registry_->GetAppsForFile( | |
| 69 base::FilePath::StringType(), "text/html", &secondary_app); | |
| 70 ASSERT_EQ(1U, secondary_app.size()); | |
| 71 VerifyApp(secondary_app, "123456788192", "Drive app 1"); | |
| 72 } | |
| 73 | |
| 74 TEST_F(DriveAppRegistryTest, UpdateFromAppList) { | |
| 75 scoped_ptr<base::Value> app_info_value = | |
| 76 google_apis::test_util::LoadJSONFile("drive/applist.json"); | |
| 77 scoped_ptr<google_apis::AppList> app_list( | |
| 78 google_apis::AppList::CreateFrom(*app_info_value)); | |
| 79 | |
| 80 apps_registry_->UpdateFromAppList(*app_list); | |
| 81 | |
| 82 // Confirm that something was loaded from applist.json. | |
| 83 std::vector<DriveAppInfo> ext_results; | |
| 84 base::FilePath ext_file(FILE_PATH_LITERAL("drive/file.exe")); | |
| 85 apps_registry_->GetAppsForFile(ext_file.Extension(), "", &ext_results); | |
| 86 ASSERT_EQ(1U, ext_results.size()); | |
| 87 } | |
| 88 | |
| 89 TEST_F(DriveAppRegistryTest, MultipleUpdate) { | |
| 90 // Call Update(). | |
| 91 apps_registry_->Update(); | |
| 92 | |
| 93 // Call Update() again. | |
| 94 // This call should be ignored because there is already an ongoing update. | |
| 95 apps_registry_->Update(); | |
| 96 | |
| 97 // The app list should be loaded only once. | |
| 98 base::RunLoop().RunUntilIdle(); | |
| 99 EXPECT_EQ(1, fake_drive_service_->app_list_load_count()); | |
| 100 } | |
| 101 | |
| 102 TEST(DriveAppRegistryUtilTest, FindPreferredIcon_Empty) { | |
| 103 google_apis::InstalledApp::IconList icons; | |
| 104 EXPECT_EQ("", | |
| 105 util::FindPreferredIcon(icons, util::kPreferredIconSize).spec()); | |
| 106 } | |
| 107 | |
| 108 TEST(DriveAppRegistryUtilTest, FindPreferredIcon_) { | |
| 109 const char kSmallerIconUrl[] = "http://example.com/smaller.png"; | |
| 110 const char kMediumIconUrl[] = "http://example.com/medium.png"; | |
| 111 const char kBiggerIconUrl[] = "http://example.com/bigger.png"; | |
| 112 const int kMediumSize = 16; | |
| 113 | |
| 114 google_apis::InstalledApp::IconList icons; | |
| 115 // The icons are not sorted by the size. | |
| 116 icons.push_back(std::make_pair(kMediumSize, | |
| 117 GURL(kMediumIconUrl))); | |
| 118 icons.push_back(std::make_pair(kMediumSize + 2, | |
| 119 GURL(kBiggerIconUrl))); | |
| 120 icons.push_back(std::make_pair(kMediumSize - 2, | |
| 121 GURL(kSmallerIconUrl))); | |
| 122 | |
| 123 // Exact match. | |
| 124 EXPECT_EQ(kMediumIconUrl, | |
| 125 util::FindPreferredIcon(icons, kMediumSize).spec()); | |
| 126 // The requested size is in-between of smaller.png and | |
| 127 // medium.png. medium.png should be returned. | |
| 128 EXPECT_EQ(kMediumIconUrl, | |
| 129 util::FindPreferredIcon(icons, kMediumSize - 1).spec()); | |
| 130 // The requested size is smaller than the smallest icon. The smallest icon | |
| 131 // should be returned. | |
| 132 EXPECT_EQ(kSmallerIconUrl, | |
| 133 util::FindPreferredIcon(icons, kMediumSize - 3).spec()); | |
| 134 // The requested size is larger than the largest icon. The largest icon | |
| 135 // should be returned. | |
| 136 EXPECT_EQ(kBiggerIconUrl, | |
| 137 util::FindPreferredIcon(icons, kMediumSize + 3).spec()); | |
| 138 } | |
| 139 | |
| 140 } // namespace drive | |
| OLD | NEW |