Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / drive / drive_app_registry_unittest.cc
blob6402176f7b08f1a8ac8106724ef88b59357fb07e
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.
5 #include "chrome/browser/drive/drive_app_registry.h"
7 #include "base/files/file_path.h"
8 #include "base/macros.h"
9 #include "base/run_loop.h"
10 #include "base/values.h"
11 #include "chrome/browser/drive/drive_app_registry_observer.h"
12 #include "chrome/browser/drive/fake_drive_service.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "google_apis/drive/drive_api_parser.h"
15 #include "google_apis/drive/gdata_wapi_parser.h"
16 #include "google_apis/drive/test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace drive {
21 class TestDriveAppRegistryObserver : public DriveAppRegistryObserver {
22 public:
23 explicit TestDriveAppRegistryObserver(DriveAppRegistry* registry)
24 : registry_(registry),
25 update_count_(0) {
26 registry_->AddObserver(this);
28 ~TestDriveAppRegistryObserver() override { registry_->RemoveObserver(this); }
30 int update_count() const { return update_count_; }
32 private:
33 // DriveAppRegistryObserver overrides:
34 void OnDriveAppRegistryUpdated() override { ++update_count_; }
36 DriveAppRegistry* registry_;
37 int update_count_;
38 DISALLOW_COPY_AND_ASSIGN(TestDriveAppRegistryObserver);
41 class DriveAppRegistryTest : public testing::Test {
42 protected:
43 void SetUp() override {
44 fake_drive_service_.reset(new FakeDriveService);
45 fake_drive_service_->LoadAppListForDriveApi("drive/applist.json");
47 apps_registry_.reset(new DriveAppRegistry(fake_drive_service_.get()));
50 bool VerifyApp(const std::vector<DriveAppInfo>& list,
51 const std::string& app_id,
52 const std::string& app_name) {
53 bool found = false;
54 for (size_t i = 0; i < list.size(); ++i) {
55 const DriveAppInfo& app = list[i];
56 if (app_id == app.app_id) {
57 EXPECT_EQ(app_name, app.app_name);
58 found = true;
59 break;
62 EXPECT_TRUE(found) << "Unable to find app with app_id " << app_id;
63 return found;
66 content::TestBrowserThreadBundle thread_bundle_;
67 scoped_ptr<FakeDriveService> fake_drive_service_;
68 scoped_ptr<DriveAppRegistry> apps_registry_;
71 TEST_F(DriveAppRegistryTest, BasicParse) {
72 TestDriveAppRegistryObserver observer(apps_registry_.get());
74 apps_registry_->Update();
75 base::RunLoop().RunUntilIdle();
76 EXPECT_EQ(1, observer.update_count());
78 std::vector<DriveAppInfo> apps;
79 apps_registry_->GetAppList(&apps);
81 ASSERT_EQ(2u, apps.size());
82 EXPECT_EQ("123456788192", apps[0].app_id);
83 EXPECT_EQ("Drive app 1", apps[0].app_name);
84 EXPECT_EQ("https://www.example.com/createForApp1",
85 apps[0].create_url.spec());
86 EXPECT_EQ("abcdefghabcdefghabcdefghabcdefgh", apps[0].product_id);
87 EXPECT_TRUE(apps[0].is_removable);
90 TEST_F(DriveAppRegistryTest, LoadAndFindDriveApps) {
91 TestDriveAppRegistryObserver observer(apps_registry_.get());
93 apps_registry_->Update();
94 base::RunLoop().RunUntilIdle();
95 EXPECT_EQ(1, observer.update_count());
97 // Find by primary extension 'exe'.
98 std::vector<DriveAppInfo> ext_results;
99 base::FilePath ext_file(FILE_PATH_LITERAL("drive/file.exe"));
100 apps_registry_->GetAppsForFile(ext_file.Extension(), "", &ext_results);
101 ASSERT_EQ(1U, ext_results.size());
102 VerifyApp(ext_results, "123456788192", "Drive app 1");
104 // Find by primary MIME type.
105 std::vector<DriveAppInfo> primary_app;
106 apps_registry_->GetAppsForFile(base::FilePath::StringType(),
107 "application/vnd.google-apps.drive-sdk.123456788192", &primary_app);
108 ASSERT_EQ(1U, primary_app.size());
109 VerifyApp(primary_app, "123456788192", "Drive app 1");
111 // Find by secondary MIME type.
112 std::vector<DriveAppInfo> secondary_app;
113 apps_registry_->GetAppsForFile(
114 base::FilePath::StringType(), "text/html", &secondary_app);
115 ASSERT_EQ(1U, secondary_app.size());
116 VerifyApp(secondary_app, "123456788192", "Drive app 1");
119 TEST_F(DriveAppRegistryTest, UpdateFromAppList) {
120 scoped_ptr<base::Value> app_info_value =
121 google_apis::test_util::LoadJSONFile("drive/applist.json");
122 scoped_ptr<google_apis::AppList> app_list(
123 google_apis::AppList::CreateFrom(*app_info_value));
125 TestDriveAppRegistryObserver observer(apps_registry_.get());
126 apps_registry_->UpdateFromAppList(*app_list);
127 EXPECT_EQ(1, observer.update_count());
129 // Confirm that something was loaded from applist.json.
130 std::vector<DriveAppInfo> ext_results;
131 base::FilePath ext_file(FILE_PATH_LITERAL("drive/file.exe"));
132 apps_registry_->GetAppsForFile(ext_file.Extension(), "", &ext_results);
133 ASSERT_EQ(1U, ext_results.size());
136 TEST_F(DriveAppRegistryTest, MultipleUpdate) {
137 TestDriveAppRegistryObserver observer(apps_registry_.get());
139 // Call Update().
140 apps_registry_->Update();
141 EXPECT_EQ(0, observer.update_count());
143 // Call Update() again.
144 // This call should be ignored because there is already an ongoing update.
145 apps_registry_->Update();
146 EXPECT_EQ(0, observer.update_count());
148 // The app list should be loaded only once.
149 base::RunLoop().RunUntilIdle();
150 EXPECT_EQ(1, fake_drive_service_->app_list_load_count());
151 EXPECT_EQ(1, observer.update_count());
154 TEST(DriveAppRegistryUtilTest, FindPreferredIcon_Empty) {
155 DriveAppInfo::IconList icons;
156 EXPECT_EQ("",
157 util::FindPreferredIcon(icons, util::kPreferredIconSize).spec());
160 TEST(DriveAppRegistryUtilTest, FindPreferredIcon_) {
161 const char kSmallerIconUrl[] = "http://example.com/smaller.png";
162 const char kMediumIconUrl[] = "http://example.com/medium.png";
163 const char kBiggerIconUrl[] = "http://example.com/bigger.png";
164 const int kMediumSize = 16;
166 DriveAppInfo::IconList icons;
167 // The icons are not sorted by the size.
168 icons.push_back(std::make_pair(kMediumSize,
169 GURL(kMediumIconUrl)));
170 icons.push_back(std::make_pair(kMediumSize + 2,
171 GURL(kBiggerIconUrl)));
172 icons.push_back(std::make_pair(kMediumSize - 2,
173 GURL(kSmallerIconUrl)));
175 // Exact match.
176 EXPECT_EQ(kMediumIconUrl,
177 util::FindPreferredIcon(icons, kMediumSize).spec());
178 // The requested size is in-between of smaller.png and
179 // medium.png. medium.png should be returned.
180 EXPECT_EQ(kMediumIconUrl,
181 util::FindPreferredIcon(icons, kMediumSize - 1).spec());
182 // The requested size is smaller than the smallest icon. The smallest icon
183 // should be returned.
184 EXPECT_EQ(kSmallerIconUrl,
185 util::FindPreferredIcon(icons, kMediumSize - 3).spec());
186 // The requested size is larger than the largest icon. The largest icon
187 // should be returned.
188 EXPECT_EQ(kBiggerIconUrl,
189 util::FindPreferredIcon(icons, kMediumSize + 3).spec());
192 TEST_F(DriveAppRegistryTest, UninstallDriveApp) {
193 apps_registry_->Update();
194 base::RunLoop().RunUntilIdle();
196 std::vector<DriveAppInfo> apps;
197 apps_registry_->GetAppList(&apps);
198 size_t original_count = apps.size();
200 // Uninstall an existing app.
201 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
202 apps_registry_->UninstallApp(
203 "123456788192",
204 google_apis::test_util::CreateCopyResultCallback(&error));
205 base::RunLoop().RunUntilIdle();
206 EXPECT_EQ(error, google_apis::HTTP_NO_CONTENT);
208 // Check that the number of apps is decreased by one.
209 apps_registry_->GetAppList(&apps);
210 EXPECT_EQ(original_count - 1, apps.size());
212 // Try to uninstall a non-existing app.
213 error = google_apis::GDATA_OTHER_ERROR;
214 apps_registry_->UninstallApp(
215 "non-existing-app-id",
216 google_apis::test_util::CreateCopyResultCallback(&error));
217 base::RunLoop().RunUntilIdle();
218 EXPECT_EQ(error, google_apis::HTTP_NOT_FOUND);
220 // Check that the number is not changed this time.
221 apps_registry_->GetAppList(&apps);
222 EXPECT_EQ(original_count - 1, apps.size());
225 } // namespace drive