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 "base/json/json_file_value_serializer.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/path_service.h"
8 #include "chrome/common/chrome_paths.h"
9 #include "content/public/test/test_browser_thread.h"
10 #include "extensions/browser/info_map.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "extensions/common/permissions/permissions_data.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using content::BrowserThread
;
18 namespace extensions
{
21 scoped_refptr
<Extension
> LoadManifest(const std::string
& dir
,
22 const std::string
& test_file
) {
24 PathService::Get(chrome::DIR_TEST_DATA
, &path
);
25 path
= path
.AppendASCII("extensions").AppendASCII(dir
).AppendASCII(test_file
);
27 JSONFileValueSerializer
serializer(path
);
28 scoped_ptr
<base::Value
> result(serializer
.Deserialize(NULL
, NULL
));
33 scoped_refptr
<Extension
> extension
=
34 Extension::Create(path
,
35 Manifest::INVALID_LOCATION
,
36 *static_cast<base::DictionaryValue
*>(result
.get()),
39 EXPECT_TRUE(extension
.get()) << error
;
46 // This test lives in Chrome because it depends on hosted app permissions
47 // (specifically, notifications) that do not exist in src/extensions.
48 class ChromeInfoMapTest
: public testing::Test
{
51 : ui_thread_(BrowserThread::UI
, &message_loop_
),
52 io_thread_(BrowserThread::IO
, &message_loop_
) {}
55 base::MessageLoop message_loop_
;
56 content::TestBrowserThread ui_thread_
;
57 content::TestBrowserThread io_thread_
;
60 // Tests API access permissions given both extension and app URLs.
61 TEST_F(ChromeInfoMapTest
, CheckPermissions
) {
62 scoped_refptr
<InfoMap
> info_map(new InfoMap());
64 scoped_refptr
<Extension
> app(
65 LoadManifest("manifest_tests", "valid_app.json"));
66 scoped_refptr
<Extension
> extension(
67 LoadManifest("manifest_tests", "tabs_extension.json"));
69 GURL
app_url("http://www.google.com/mail/foo.html");
70 ASSERT_TRUE(app
->is_app());
71 ASSERT_TRUE(app
->web_extent().MatchesURL(app_url
));
73 info_map
->AddExtension(app
.get(), base::Time(), false, false);
74 info_map
->AddExtension(extension
.get(), base::Time(), false, false);
76 // The app should have the notifications permission, either from a
77 // chrome-extension URL or from its web extent.
78 const Extension
* match
= info_map
->extensions().GetExtensionOrAppByURL(
79 app
->GetResourceURL("a.html"));
81 match
->permissions_data()->HasAPIPermission(
82 APIPermission::kNotifications
));
83 match
= info_map
->extensions().GetExtensionOrAppByURL(app_url
);
85 match
->permissions_data()->HasAPIPermission(
86 APIPermission::kNotifications
));
89 match
->permissions_data()->HasAPIPermission(APIPermission::kTab
));
91 // The extension should have the tabs permission.
92 match
= info_map
->extensions().GetExtensionOrAppByURL(
93 extension
->GetResourceURL("a.html"));
95 match
->permissions_data()->HasAPIPermission(APIPermission::kTab
));
97 match
->permissions_data()->HasAPIPermission(
98 APIPermission::kNotifications
));
100 // Random URL should not have any permissions.
101 GURL
evil_url("http://evil.com/a.html");
102 match
= info_map
->extensions().GetExtensionOrAppByURL(evil_url
);
106 TEST_F(ChromeInfoMapTest
, TestNotificationsDisabled
) {
107 scoped_refptr
<InfoMap
> info_map(new InfoMap());
108 scoped_refptr
<Extension
> app(
109 LoadManifest("manifest_tests", "valid_app.json"));
110 info_map
->AddExtension(app
.get(), base::Time(), false, false);
112 EXPECT_FALSE(info_map
->AreNotificationsDisabled(app
->id()));
113 info_map
->SetNotificationsDisabled(app
->id(), true);
114 EXPECT_TRUE(info_map
->AreNotificationsDisabled(app
->id()));
115 info_map
->SetNotificationsDisabled(app
->id(), false);
118 } // namespace extensions