Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: chrome/browser/media_gallery/media_file_system_registry_unittest.cc

Issue 11442047: Media Galleries: Add more tests for media gallery names. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // MediaFileSystemRegistry unit tests. 5 // MediaFileSystemRegistry unit tests.
6 6
7 #include <set>
8
7 #include "base/command_line.h" 9 #include "base/command_line.h"
8 #include "base/file_util.h" 10 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/json/json_reader.h"
10 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h" 15 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop.h" 16 #include "base/message_loop.h"
14 #include "base/stl_util.h" 17 #include "base/stl_util.h"
15 #include "base/stringprintf.h" 18 #include "base/stringprintf.h"
16 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
17 #include "base/values.h" 20 #include "base/values.h"
18 #include "chrome/browser/extensions/extension_service.h" 21 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_system.h" 22 #include "chrome/browser/extensions/extension_system.h"
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 DCHECK(!path.ReferencesParent()); 145 DCHECK(!path.ReferencesParent());
143 146
144 std::string fsid = base::StringPrintf("FSID:%d", ++fsid_); 147 std::string fsid = base::StringPrintf("FSID:%d", ++fsid_);
145 FSInfo info(device_id, path, fsid); 148 FSInfo info(device_id, path, fsid);
146 file_systems_by_id_[fsid] = info; 149 file_systems_by_id_[fsid] = info;
147 return fsid; 150 return fsid;
148 } 151 }
149 152
150 namespace { 153 namespace {
151 154
155 const char kIsRemovableKey[] = "isRemovable";
156
157 void GetGalleryNamesCB(std::set<std::string>* results,
158 const std::vector<MediaFileSystemInfo>& file_systems) {
159 for (size_t i = 0; i < file_systems.size(); i++) {
kmadhusu 2012/12/12 17:45:02 nit: ++i
Lei Zhang 2012/12/12 20:15:15 The rest of the file had i++, so I was being consi
160 std::set<std::string>::const_iterator it =
161 results->find(file_systems[i].name);
162 ASSERT_EQ(it, results->end());
163 results->insert(file_systems[i].name);
164 }
165 }
166
152 class TestMediaStorageUtil : public MediaStorageUtil { 167 class TestMediaStorageUtil : public MediaStorageUtil {
153 public: 168 public:
154 static void SetTestingMode(); 169 static void SetTestingMode();
155 170
156 static bool GetDeviceInfoFromPathTestFunction(const FilePath& path, 171 static bool GetDeviceInfoFromPathTestFunction(const FilePath& path,
157 std::string* device_id, 172 std::string* device_id,
158 string16* device_name, 173 string16* device_name,
159 FilePath* relative_path); 174 FilePath* relative_path);
160 }; 175 };
161 176
(...skipping 25 matching lines...) Expand all
187 MockProfileSharedRenderProcessHostFactory* rph_factory); 202 MockProfileSharedRenderProcessHostFactory* rph_factory);
188 ~ProfileState(); 203 ~ProfileState();
189 204
190 MediaGalleriesPreferences* GetMediaGalleriesPrefs(); 205 MediaGalleriesPreferences* GetMediaGalleriesPrefs();
191 206
192 void CheckGalleries( 207 void CheckGalleries(
193 const std::string& test, 208 const std::string& test,
194 const std::vector<MediaFileSystemInfo>& regular_extension_galleries, 209 const std::vector<MediaFileSystemInfo>& regular_extension_galleries,
195 const std::vector<MediaFileSystemInfo>& all_extension_galleries); 210 const std::vector<MediaFileSystemInfo>& all_extension_galleries);
196 211
212 std::set<std::string> GetGalleryNames(extensions::Extension* extension);
213
197 extensions::Extension* all_permission_extension(); 214 extensions::Extension* all_permission_extension();
198 extensions::Extension* regular_permission_extension(); 215 extensions::Extension* regular_permission_extension();
199 216
200 private: 217 private:
201 void CompareResults(const std::string& test, 218 void CompareResults(const std::string& test,
202 const std::vector<MediaFileSystemInfo>& expected, 219 const std::vector<MediaFileSystemInfo>& expected,
203 const std::vector<MediaFileSystemInfo>& actual); 220 const std::vector<MediaFileSystemInfo>& actual);
204 221
205 int GetAndClearComparisonCount(); 222 int GetAndClearComparisonCount();
206 223
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 // All galleries permission. 458 // All galleries permission.
442 registry->GetMediaFileSystemsForExtension( 459 registry->GetMediaFileSystemsForExtension(
443 rvh, all_permission_extension_.get(), 460 rvh, all_permission_extension_.get(),
444 base::Bind(&ProfileState::CompareResults, base::Unretained(this), 461 base::Bind(&ProfileState::CompareResults, base::Unretained(this),
445 StringPrintf("%s (all permission)", test.c_str()), 462 StringPrintf("%s (all permission)", test.c_str()),
446 base::ConstRef(all_extension_galleries))); 463 base::ConstRef(all_extension_galleries)));
447 MessageLoop::current()->RunUntilIdle(); 464 MessageLoop::current()->RunUntilIdle();
448 EXPECT_EQ(1, GetAndClearComparisonCount()); 465 EXPECT_EQ(1, GetAndClearComparisonCount());
449 } 466 }
450 467
468 std::set<std::string> ProfileState::GetGalleryNames(
469 extensions::Extension* extension) {
470 content::RenderViewHost* rvh = single_web_contents_->GetRenderViewHost();
471 std::set<std::string> results;
472 MediaFileSystemRegistry* registry =
473 g_browser_process->media_file_system_registry();
474 registry->GetMediaFileSystemsForExtension(
475 rvh, extension,
476 base::Bind(&GetGalleryNamesCB, base::Unretained(&results)));
477 MessageLoop::current()->RunUntilIdle();
478 return results;
479 }
480
451 extensions::Extension* ProfileState::all_permission_extension() { 481 extensions::Extension* ProfileState::all_permission_extension() {
452 return all_permission_extension_.get(); 482 return all_permission_extension_.get();
453 } 483 }
454 484
455 extensions::Extension* ProfileState::regular_permission_extension() { 485 extensions::Extension* ProfileState::regular_permission_extension() {
456 return regular_permission_extension_.get(); 486 return regular_permission_extension_.get();
457 } 487 }
458 488
459 void ProfileState::CompareResults( 489 void ProfileState::CompareResults(
460 const std::string& test, 490 const std::string& test,
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 // Add it to the all galleries extension. 689 // Add it to the all galleries extension.
660 SetGalleryPermission(kProfileId, 690 SetGalleryPermission(kProfileId,
661 profile_state->all_permission_extension(), 691 profile_state->all_permission_extension(),
662 device_id, 692 device_id,
663 true /*has access*/); 693 true /*has access*/);
664 auto_galleries.push_back(added_info); 694 auto_galleries.push_back(added_info);
665 profile_state->CheckGalleries("user added all", added_galleries, 695 profile_state->CheckGalleries("user added all", added_galleries,
666 auto_galleries); 696 auto_galleries);
667 } 697 }
668 698
699 TEST_F(MediaFileSystemRegistryTest, GalleryName) {
700 CreateProfileState(1);
701 AssertAllAutoAddedGalleries();
702 const size_t kProfileId = 0U;
703
704 // Get all existing gallery names.
705 ProfileState* profile_state = GetProfileState(kProfileId);
706 std::set<std::string> gallery_names =
707 profile_state->GetGalleryNames(profile_state->all_permission_extension());
708 ASSERT_EQ(3U, gallery_names.size());
709
710 // Add a couple new galleries.
711 FilePath mtp_bogus_location(FILE_PATH_LITERAL("/mtp_bogus_location"));
712 std::string mtp_device_id = AttachDevice(MediaStorageUtil::MTP_OR_PTP,
713 "mtp_fake_id",
kmadhusu 2012/12/12 17:45:02 style nit: Fix indentation.
Lei Zhang 2012/12/12 20:15:15 Done.
714 mtp_bogus_location);
715 SetGalleryPermission(kProfileId,
716 profile_state->all_permission_extension(),
717 mtp_device_id,
718 true /*has access*/);
719
720 FilePath removable_bogus_location(
721 FILE_PATH_LITERAL("/removable_bogus_location"));
722 std::string removable_device_id =
723 AttachDevice(MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM,
724 "removable_fake_id",
725 removable_bogus_location);
726 SetGalleryPermission(kProfileId,
727 profile_state->all_permission_extension(),
728 removable_device_id,
729 true /*has access*/);
730
731 // Get new list of all gallery names.
732 std::set<std::string> new_gallery_names =
733 profile_state->GetGalleryNames(profile_state->all_permission_extension());
734 ASSERT_EQ(5U, new_gallery_names.size());
735
736 // Check the intersection.
737 std::vector<std::string> intersection_names;
738 std::set_intersection(gallery_names.begin(), gallery_names.end(),
739 new_gallery_names.begin(), new_gallery_names.end(),
740 std::back_inserter(intersection_names));
741 EXPECT_EQ(3U, intersection_names.size());
742 for (size_t i = 0; i < intersection_names.size(); i++) {
kmadhusu 2012/12/12 17:45:02 nit: ++i
743 scoped_ptr<DictionaryValue> dict_value(static_cast<DictionaryValue*>(
744 base::JSONReader::Read(intersection_names[i])));
745 ASSERT_TRUE(dict_value);
746 bool is_removable;
747 ASSERT_TRUE(dict_value->GetBoolean(kIsRemovableKey, &is_removable));
748 EXPECT_FALSE(is_removable);
749 }
750
751 // Check the difference.
752 std::vector<std::string> difference_names;
753 std::set_symmetric_difference(
754 gallery_names.begin(), gallery_names.end(),
755 new_gallery_names.begin(), new_gallery_names.end(),
756 std::back_inserter(difference_names));
757 EXPECT_EQ(2U, difference_names.size());
758 for (size_t i = 0; i < difference_names.size(); i++) {
759 scoped_ptr<DictionaryValue> dict_value(static_cast<DictionaryValue*>(
760 base::JSONReader::Read(difference_names[i])));
761 ASSERT_TRUE(dict_value);
762 bool is_removable;
763 ASSERT_TRUE(dict_value->GetBoolean(kIsRemovableKey, &is_removable));
764 EXPECT_TRUE(is_removable);
765 }
766 }
767
669 } // namespace 768 } // namespace
670 769
671 } // namespace chrome 770 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698