Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / drive / drive_app_registry_unittest.cc
blobda23cb6a8ac3d2b2cbdd2c843837013806a69668
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/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/values.h"
12 #include "chrome/browser/drive/drive_app_registry_observer.h"
13 #include "chrome/browser/drive/fake_drive_service.h"
14 #include "google_apis/drive/drive_api_parser.h"
15 #include "google_apis/drive/test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace drive {
20 class TestDriveAppRegistryObserver : public DriveAppRegistryObserver {
21 public:
22 explicit TestDriveAppRegistryObserver(DriveAppRegistry* registry)
23 : registry_(registry),
24 update_count_(0) {
25 registry_->AddObserver(this);
27 ~TestDriveAppRegistryObserver() override { registry_->RemoveObserver(this); }
29 int update_count() const { return update_count_; }
31 private:
32 // DriveAppRegistryObserver overrides:
33 void OnDriveAppRegistryUpdated() override { ++update_count_; }
35 DriveAppRegistry* registry_;
36 int update_count_;
37 DISALLOW_COPY_AND_ASSIGN(TestDriveAppRegistryObserver);
40 class DriveAppRegistryTest : public testing::Test {
41 protected:
42 void SetUp() override {
43 fake_drive_service_.reset(new FakeDriveService);
44 fake_drive_service_->LoadAppListForDriveApi("drive/applist.json");
46 apps_registry_.reset(new DriveAppRegistry(fake_drive_service_.get()));
49 bool VerifyApp(const std::vector<DriveAppInfo>& list,
50 const std::string& app_id,
51 const std::string& app_name) {
52 bool found = false;
53 for (size_t i = 0; i < list.size(); ++i) {
54 const DriveAppInfo& app = list[i];
55 if (app_id == app.app_id) {
56 EXPECT_EQ(app_name, app.app_name);
57 found = true;
58 break;
61 EXPECT_TRUE(found) << "Unable to find app with app_id " << app_id;
62 return found;
65 base::MessageLoop message_loop_;
66 scoped_ptr<FakeDriveService> fake_drive_service_;
67 scoped_ptr<DriveAppRegistry> apps_registry_;
70 TEST_F(DriveAppRegistryTest, BasicParse) {
71 TestDriveAppRegistryObserver observer(apps_registry_.get());
73 apps_registry_->Update();
74 base::RunLoop().RunUntilIdle();
75 EXPECT_EQ(1, observer.update_count());
77 std::vector<DriveAppInfo> apps;
78 apps_registry_->GetAppList(&apps);
80 ASSERT_EQ(2u, apps.size());
81 EXPECT_EQ("123456788192", apps[0].app_id);
82 EXPECT_EQ("Drive app 1", apps[0].app_name);
83 EXPECT_EQ("https://www.example.com/createForApp1",
84 apps[0].create_url.spec());
85 EXPECT_EQ("abcdefghabcdefghabcdefghabcdefgh", apps[0].product_id);
86 EXPECT_TRUE(apps[0].is_removable);
89 TEST_F(DriveAppRegistryTest, LoadAndFindDriveApps) {
90 TestDriveAppRegistryObserver observer(apps_registry_.get());
92 apps_registry_->Update();
93 base::RunLoop().RunUntilIdle();
94 EXPECT_EQ(1, observer.update_count());
96 // Find by primary extension 'exe'.
97 std::vector<DriveAppInfo> ext_results;
98 base::FilePath ext_file(FILE_PATH_LITERAL("drive/file.exe"));
99 apps_registry_->GetAppsForFile(ext_file.Extension(), "", &ext_results);
100 ASSERT_EQ(1U, ext_results.size());
101 VerifyApp(ext_results, "123456788192", "Drive app 1");
103 // Find by primary MIME type.
104 std::vector<DriveAppInfo> primary_app;
105 apps_registry_->GetAppsForFile(base::FilePath::StringType(),
106 "application/vnd.google-apps.drive-sdk.123456788192", &primary_app);
107 ASSERT_EQ(1U, primary_app.size());
108 VerifyApp(primary_app, "123456788192", "Drive app 1");
110 // Find by secondary MIME type.
111 std::vector<DriveAppInfo> secondary_app;
112 apps_registry_->GetAppsForFile(
113 base::FilePath::StringType(), "text/html", &secondary_app);
114 ASSERT_EQ(1U, secondary_app.size());
115 VerifyApp(secondary_app, "123456788192", "Drive app 1");
118 TEST_F(DriveAppRegistryTest, UpdateFromAppList) {
119 scoped_ptr<base::Value> app_info_value =
120 google_apis::test_util::LoadJSONFile("drive/applist.json");
121 scoped_ptr<google_apis::AppList> app_list(
122 google_apis::AppList::CreateFrom(*app_info_value));
124 TestDriveAppRegistryObserver observer(apps_registry_.get());
125 apps_registry_->UpdateFromAppList(*app_list);
126 EXPECT_EQ(1, observer.update_count());
128 // Confirm that something was loaded from applist.json.
129 std::vector<DriveAppInfo> ext_results;
130 base::FilePath ext_file(FILE_PATH_LITERAL("drive/file.exe"));
131 apps_registry_->GetAppsForFile(ext_file.Extension(), "", &ext_results);
132 ASSERT_EQ(1U, ext_results.size());
135 TEST_F(DriveAppRegistryTest, MultipleUpdate) {
136 TestDriveAppRegistryObserver observer(apps_registry_.get());
138 // Call Update().
139 apps_registry_->Update();
140 EXPECT_EQ(0, observer.update_count());
142 // Call Update() again.
143 // This call should be ignored because there is already an ongoing update.
144 apps_registry_->Update();
145 EXPECT_EQ(0, observer.update_count());
147 // The app list should be loaded only once.
148 base::RunLoop().RunUntilIdle();
149 EXPECT_EQ(1, fake_drive_service_->app_list_load_count());
150 EXPECT_EQ(1, observer.update_count());
153 TEST(DriveAppRegistryUtilTest, FindPreferredIcon_Empty) {
154 DriveAppInfo::IconList icons;
155 EXPECT_EQ("",
156 util::FindPreferredIcon(icons, util::kPreferredIconSize).spec());
159 TEST(DriveAppRegistryUtilTest, FindPreferredIcon_) {
160 const char kSmallerIconUrl[] = "http://example.com/smaller.png";
161 const char kMediumIconUrl[] = "http://example.com/medium.png";
162 const char kBiggerIconUrl[] = "http://example.com/bigger.png";
163 const int kMediumSize = 16;
165 DriveAppInfo::IconList icons;
166 // The icons are not sorted by the size.
167 icons.push_back(std::make_pair(kMediumSize,
168 GURL(kMediumIconUrl)));
169 icons.push_back(std::make_pair(kMediumSize + 2,
170 GURL(kBiggerIconUrl)));
171 icons.push_back(std::make_pair(kMediumSize - 2,
172 GURL(kSmallerIconUrl)));
174 // Exact match.
175 EXPECT_EQ(kMediumIconUrl,
176 util::FindPreferredIcon(icons, kMediumSize).spec());
177 // The requested size is in-between of smaller.png and
178 // medium.png. medium.png should be returned.
179 EXPECT_EQ(kMediumIconUrl,
180 util::FindPreferredIcon(icons, kMediumSize - 1).spec());
181 // The requested size is smaller than the smallest icon. The smallest icon
182 // should be returned.
183 EXPECT_EQ(kSmallerIconUrl,
184 util::FindPreferredIcon(icons, kMediumSize - 3).spec());
185 // The requested size is larger than the largest icon. The largest icon
186 // should be returned.
187 EXPECT_EQ(kBiggerIconUrl,
188 util::FindPreferredIcon(icons, kMediumSize + 3).spec());
191 TEST_F(DriveAppRegistryTest, UninstallDriveApp) {
192 apps_registry_->Update();
193 base::RunLoop().RunUntilIdle();
195 std::vector<DriveAppInfo> apps;
196 apps_registry_->GetAppList(&apps);
197 size_t original_count = apps.size();
199 // Uninstall an existing app.
200 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
201 apps_registry_->UninstallApp(
202 "123456788192",
203 google_apis::test_util::CreateCopyResultCallback(&error));
204 base::RunLoop().RunUntilIdle();
205 EXPECT_EQ(error, google_apis::HTTP_NO_CONTENT);
207 // Check that the number of apps is decreased by one.
208 apps_registry_->GetAppList(&apps);
209 EXPECT_EQ(original_count - 1, apps.size());
211 // Try to uninstall a non-existing app.
212 error = google_apis::DRIVE_OTHER_ERROR;
213 apps_registry_->UninstallApp(
214 "non-existing-app-id",
215 google_apis::test_util::CreateCopyResultCallback(&error));
216 base::RunLoop().RunUntilIdle();
217 EXPECT_EQ(error, google_apis::HTTP_NOT_FOUND);
219 // Check that the number is not changed this time.
220 apps_registry_->GetAppList(&apps);
221 EXPECT_EQ(original_count - 1, apps.size());
224 } // namespace drive