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/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"
18 class DriveAppRegistryTest
: public testing::Test
{
20 virtual void SetUp() OVERRIDE
{
21 fake_drive_service_
.reset(new FakeDriveService
);
22 fake_drive_service_
->LoadAppListForDriveApi("drive/applist.json");
24 apps_registry_
.reset(new DriveAppRegistry(fake_drive_service_
.get()));
27 bool VerifyApp(const std::vector
<DriveAppInfo
>& list
,
28 const std::string
& app_id
,
29 const std::string
& app_name
) {
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
);
39 EXPECT_TRUE(found
) << "Unable to find app with app_id " << app_id
;
43 content::TestBrowserThreadBundle thread_bundle_
;
44 scoped_ptr
<FakeDriveService
> fake_drive_service_
;
45 scoped_ptr
<DriveAppRegistry
> apps_registry_
;
48 TEST_F(DriveAppRegistryTest
, LoadAndFindDriveApps
) {
49 apps_registry_
->Update();
50 base::RunLoop().RunUntilIdle();
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");
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");
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");
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
));
80 apps_registry_
->UpdateFromAppList(*app_list
);
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());
89 TEST_F(DriveAppRegistryTest
, MultipleUpdate
) {
91 apps_registry_
->Update();
93 // Call Update() again.
94 // This call should be ignored because there is already an ongoing update.
95 apps_registry_
->Update();
97 // The app list should be loaded only once.
98 base::RunLoop().RunUntilIdle();
99 EXPECT_EQ(1, fake_drive_service_
->app_list_load_count());
102 TEST(DriveAppRegistryUtilTest
, FindPreferredIcon_Empty
) {
103 google_apis::InstalledApp::IconList icons
;
105 util::FindPreferredIcon(icons
, util::kPreferredIconSize
).spec());
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;
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
)));
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());
140 TEST_F(DriveAppRegistryTest
, UninstallDriveApp
) {
141 apps_registry_
->Update();
142 base::RunLoop().RunUntilIdle();
144 std::vector
<DriveAppInfo
> apps
;
145 apps_registry_
->GetAppList(&apps
);
146 size_t original_count
= apps
.size();
148 // Uninstall an existing app.
149 google_apis::GDataErrorCode error
= google_apis::GDATA_OTHER_ERROR
;
150 apps_registry_
->UninstallApp(
152 google_apis::test_util::CreateCopyResultCallback(&error
));
153 base::RunLoop().RunUntilIdle();
154 EXPECT_EQ(error
, google_apis::HTTP_NO_CONTENT
);
156 // Check that the number of apps is decreased by one.
157 apps_registry_
->GetAppList(&apps
);
158 EXPECT_EQ(original_count
- 1, apps
.size());
160 // Try to uninstall a non-existing app.
161 error
= google_apis::GDATA_OTHER_ERROR
;
162 apps_registry_
->UninstallApp(
163 "non-existing-app-id",
164 google_apis::test_util::CreateCopyResultCallback(&error
));
165 base::RunLoop().RunUntilIdle();
166 EXPECT_EQ(error
, google_apis::HTTP_NOT_FOUND
);
168 // Check that the number is not changed this time.
169 apps_registry_
->GetAppList(&apps
);
170 EXPECT_EQ(original_count
- 1, apps
.size());