1 // Copyright 2013 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 "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "chrome/browser/extensions/extension_prefs.h"
8 #include "chrome/browser/extensions/extension_prefs_unittest.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
12 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
13 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 void AddGalleryPermission(MediaGalleryPrefId gallery
,
23 std::vector
<MediaGalleryPermission
>* vector
) {
24 MediaGalleryPermission permission
;
25 permission
.pref_id
= gallery
;
26 permission
.has_permission
= has_access
;
27 vector
->push_back(permission
);
32 // Test the MediaGalleries permissions functions.
33 class MediaGalleriesPermissionsTest
: public extensions::ExtensionPrefsTest
{
35 MediaGalleriesPermissionsTest()
36 : file_thread_(content::BrowserThread::FILE) {
38 virtual ~MediaGalleriesPermissionsTest() {}
40 // This is the same implementation as ExtensionPrefsTest::TearDown(), except
41 // for also resetting the ExtensionPrefs used by |gallery_prefs_| after
42 // TestExtensionPrefs reconstructs them.
43 virtual void TearDown() OVERRIDE
{
46 // Reset ExtensionPrefs, and re-verify.
47 prefs_
.ResetPrefRegistry();
48 RegisterPreferences(prefs_
.pref_registry().get());
49 prefs_
.RecreateExtensionPrefs();
50 gallery_prefs_
->SetExtensionPrefsForTesting(prefs());
52 prefs_
.pref_service()->CommitPendingWrite();
53 message_loop_
.RunUntilIdle();
55 TestStorageMonitor::RemoveSingleton();
57 testing::Test::TearDown();
60 virtual void Initialize() OVERRIDE
{
63 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall());
64 profile_
.reset(new TestingProfile
);
65 gallery_prefs_
.reset(new MediaGalleriesPreferences(profile_
.get()));
67 gallery_prefs_
->EnsureInitialized(loop
.QuitClosure());
70 gallery_prefs_
->SetExtensionPrefsForTesting(prefs());
72 extension1_id_
= prefs_
.AddExtensionAndReturnId("test1");
73 extension2_id_
= prefs_
.AddExtensionAndReturnId("test2");
74 extension3_id_
= prefs_
.AddExtensionAndReturnId("test3");
75 // Id4 isn't used to ensure that an empty permission list is ok.
76 extension4_id_
= prefs_
.AddExtensionAndReturnId("test4");
79 gallery_prefs_
->SetGalleryPermissionInPrefs(extension1_id_
, 1, false);
80 AddGalleryPermission(1, false, &extension1_expectation_
);
83 gallery_prefs_
->SetGalleryPermissionInPrefs(extension1_id_
, 2, true);
84 AddGalleryPermission(2, true, &extension1_expectation_
);
87 gallery_prefs_
->SetGalleryPermissionInPrefs(extension1_id_
, 2, false);
88 extension1_expectation_
[1].has_permission
= false;
91 gallery_prefs_
->SetGalleryPermissionInPrefs(extension2_id_
, 1, true);
92 gallery_prefs_
->SetGalleryPermissionInPrefs(extension2_id_
, 3, true);
93 gallery_prefs_
->SetGalleryPermissionInPrefs(extension2_id_
, 4, true);
94 AddGalleryPermission(1, true, &extension2_expectation_
);
95 AddGalleryPermission(3, true, &extension2_expectation_
);
96 AddGalleryPermission(4, true, &extension2_expectation_
);
99 gallery_prefs_
->SetGalleryPermissionInPrefs(extension3_id_
, 3, true);
100 AddGalleryPermission(3, true, &extension3_expectation_
);
103 gallery_prefs_
->RemoveGalleryPermissionsFromPrefs(3);
104 extension2_expectation_
.erase(extension2_expectation_
.begin() + 1);
105 extension3_expectation_
.erase(extension3_expectation_
.begin());
108 gallery_prefs_
->UnsetGalleryPermissionInPrefs(extension1_id_
, 1);
109 extension1_expectation_
.erase(extension1_expectation_
.begin());
113 virtual void Verify() OVERRIDE
{
116 std::vector
<MediaGalleryPermission
>* expectation
;
119 const TestData test_data
[] = {{&extension1_id_
, &extension1_expectation_
},
120 {&extension2_id_
, &extension2_expectation_
},
121 {&extension3_id_
, &extension3_expectation_
},
122 {&extension4_id_
, &extension4_expectation_
}};
123 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_data
); i
++) {
124 std::vector
<MediaGalleryPermission
> actual
=
125 gallery_prefs_
->GetGalleryPermissionsFromPrefs(*test_data
[i
].id
);
126 EXPECT_EQ(test_data
[i
].expectation
->size(), actual
.size());
127 for (size_t permission_entry
= 0;
128 permission_entry
< test_data
[i
].expectation
->size() &&
129 permission_entry
< actual
.size();
130 permission_entry
++) {
131 EXPECT_EQ(test_data
[i
].expectation
->at(permission_entry
).pref_id
,
132 actual
[permission_entry
].pref_id
);
133 EXPECT_EQ(test_data
[i
].expectation
->at(permission_entry
).has_permission
,
134 actual
[permission_entry
].has_permission
);
139 std::string extension1_id_
;
140 std::string extension2_id_
;
141 std::string extension3_id_
;
142 std::string extension4_id_
;
144 std::vector
<MediaGalleryPermission
> extension1_expectation_
;
145 std::vector
<MediaGalleryPermission
> extension2_expectation_
;
146 std::vector
<MediaGalleryPermission
> extension3_expectation_
;
147 std::vector
<MediaGalleryPermission
> extension4_expectation_
;
149 // Needed for |gallery_prefs_| to initialize correctly.
150 EnsureMediaDirectoriesExists ensure_media_directories_exists_
;
151 content::TestBrowserThread file_thread_
;
153 scoped_ptr
<TestingProfile
> profile_
;
154 scoped_ptr
<MediaGalleriesPreferences
> gallery_prefs_
;
157 TEST_F(MediaGalleriesPermissionsTest
, MediaGalleries
) {}