OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "base/version.h" | 10 #include "base/version.h" |
10 #include "chrome/browser/browser_thread.h" | 11 #include "chrome/browser/browser_thread.h" |
11 #include "chrome/browser/extensions/external_policy_extension_provider.h" | 12 #include "chrome/browser/extensions/external_extension_provider_interface.h" |
| 13 #include "chrome/browser/extensions/external_extension_provider_impl.h" |
| 14 #include "chrome/browser/extensions/external_policy_extension_loader.h" |
12 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/testing_pref_service.h" |
| 18 #include "chrome/test/testing_profile.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
14 | 20 |
15 class ExternalPolicyExtensionProviderTest : public testing::Test { | 21 class ExternalPolicyExtensionProviderTest : public testing::Test { |
16 public: | 22 public: |
17 ExternalPolicyExtensionProviderTest() | 23 ExternalPolicyExtensionProviderTest() |
18 : loop_(MessageLoop::TYPE_IO), | 24 : loop_(MessageLoop::TYPE_IO), |
19 ui_thread_(BrowserThread::UI, &loop_), | 25 ui_thread_(BrowserThread::UI, &loop_) { |
20 file_thread_(BrowserThread::FILE, &loop_) { | |
21 } | 26 } |
22 | 27 |
23 virtual ~ExternalPolicyExtensionProviderTest() { | 28 virtual ~ExternalPolicyExtensionProviderTest() {} |
24 } | |
25 | 29 |
26 private: | 30 private: |
| 31 // We need these to satisfy BrowserThread::CurrentlyOn(BrowserThread::UI) |
| 32 // checks in ExternalExtensionProviderImpl. |
27 MessageLoop loop_; | 33 MessageLoop loop_; |
28 BrowserThread ui_thread_; | 34 BrowserThread ui_thread_; |
29 BrowserThread file_thread_; | |
30 }; | 35 }; |
31 | 36 |
32 class MockExternalPolicyExtensionProviderVisitor | 37 class MockExternalPolicyExtensionProviderVisitor |
33 : public ExternalExtensionProvider::Visitor { | 38 : public ExternalExtensionProviderInterface::VisitorInterface { |
34 public: | 39 public: |
35 MockExternalPolicyExtensionProviderVisitor() { | 40 MockExternalPolicyExtensionProviderVisitor() { |
36 } | 41 } |
37 | 42 |
38 // Initialize a provider with |policy_forcelist|, and check that it parses | 43 // Initialize a provider with |policy_forcelist|, and check that it parses |
39 // exactly those extensions, that are specified in |policy_validlist|. | 44 // exactly those extensions, that are specified in |policy_validlist|. |
40 void Visit(ListValue* policy_forcelist, | 45 void Visit(ListValue* policy_forcelist, |
41 ListValue* policy_validlist, | 46 ListValue* policy_validlist, |
42 const std::set<std::string>& ignore_list) { | 47 const std::set<std::string>& ignore_list) { |
43 provider_.reset(new ExternalPolicyExtensionProvider(policy_forcelist)); | 48 profile_.reset(new TestingProfile); |
| 49 profile_->GetTestingPrefService()->SetManagedPref( |
| 50 prefs::kExtensionInstallForceList, |
| 51 policy_forcelist->DeepCopy()); |
| 52 provider_.reset(new ExternalExtensionProviderImpl( |
| 53 this, |
| 54 new ExternalPolicyExtensionLoader(profile_.get()), |
| 55 Extension::INVALID, |
| 56 Extension::EXTERNAL_POLICY_DOWNLOAD)); |
44 | 57 |
45 // Extensions will be removed from this list as they visited, | 58 // Extensions will be removed from this list as they visited, |
46 // so it should be emptied by the end. | 59 // so it should be emptied by the end. |
47 remaining_extensions = policy_validlist; | 60 remaining_extensions = policy_validlist; |
48 provider_->VisitRegisteredExtension(this); | 61 provider_->VisitRegisteredExtension(); |
49 EXPECT_EQ(0u, remaining_extensions->GetSize()); | 62 EXPECT_EQ(0u, remaining_extensions->GetSize()); |
50 } | 63 } |
51 | 64 |
52 virtual void OnExternalExtensionFileFound(const std::string& id, | 65 virtual void OnExternalExtensionFileFound(const std::string& id, |
53 const Version* version, | 66 const Version* version, |
54 const FilePath& path, | 67 const FilePath& path, |
55 Extension::Location unused) { | 68 Extension::Location unused) { |
56 ADD_FAILURE() << "There should be no external extensions from files."; | 69 ADD_FAILURE() << "There should be no external extensions from files."; |
57 } | 70 } |
58 | 71 |
59 virtual void OnExternalExtensionUpdateUrlFound( | 72 virtual void OnExternalExtensionUpdateUrlFound( |
60 const std::string& id, const GURL& update_url, | 73 const std::string& id, const GURL& update_url, |
61 Extension::Location location) { | 74 Extension::Location location) { |
62 // Extension has the correct location. | 75 // Extension has the correct location. |
63 EXPECT_EQ(Extension::EXTERNAL_POLICY_DOWNLOAD, location); | 76 EXPECT_EQ(Extension::EXTERNAL_POLICY_DOWNLOAD, location); |
64 | 77 |
65 // Provider returns the correct location when asked. | 78 // Provider returns the correct location when asked. |
66 Extension::Location location1; | 79 Extension::Location location1; |
67 scoped_ptr<Version> version1; | 80 scoped_ptr<Version> version1; |
68 provider_->GetExtensionDetails(id, &location1, &version1); | 81 provider_->GetExtensionDetails(id, &location1, &version1); |
69 EXPECT_EQ(Extension::EXTERNAL_POLICY_DOWNLOAD, location1); | 82 EXPECT_EQ(Extension::EXTERNAL_POLICY_DOWNLOAD, location1); |
70 EXPECT_FALSE(version1.get()); | 83 EXPECT_FALSE(version1.get()); |
71 | 84 |
72 // Remove the extension from our list. | 85 // Remove the extension from our list. |
73 StringValue ext_str(id + ";" + update_url.spec()); | 86 StringValue ext_str(id + ";" + update_url.spec()); |
74 EXPECT_NE(-1, remaining_extensions->Remove(ext_str)); | 87 EXPECT_NE(-1, remaining_extensions->Remove(ext_str)); |
75 } | 88 } |
76 | 89 |
| 90 virtual void OnExternalProviderReady() { |
| 91 EXPECT_TRUE(provider_->IsReady()); |
| 92 } |
| 93 |
77 private: | 94 private: |
78 ListValue* remaining_extensions; | 95 ListValue* remaining_extensions; |
79 | 96 |
80 scoped_ptr<ExternalPolicyExtensionProvider> provider_; | 97 scoped_ptr<TestingProfile> profile_; |
| 98 |
| 99 scoped_ptr<ExternalExtensionProviderImpl> provider_; |
81 | 100 |
82 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyExtensionProviderVisitor); | 101 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyExtensionProviderVisitor); |
83 }; | 102 }; |
84 | 103 |
85 TEST_F(ExternalPolicyExtensionProviderTest, PolicyIsParsed) { | 104 TEST_F(ExternalPolicyExtensionProviderTest, PolicyIsParsed) { |
86 ListValue forced_extensions; | 105 ListValue forced_extensions; |
87 forced_extensions.Append(Value::CreateStringValue( | 106 forced_extensions.Append(Value::CreateStringValue( |
88 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;http://www.example.com/crx?a=5;b=6")); | 107 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;http://www.example.com/crx?a=5;b=6")); |
89 forced_extensions.Append(Value::CreateStringValue( | 108 forced_extensions.Append(Value::CreateStringValue( |
90 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;" | 109 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;" |
(...skipping 26 matching lines...) Expand all Loading... |
117 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;http#//www.example.com/crx")); | 136 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;http#//www.example.com/crx")); |
118 forced_extensions.Append(Value::CreateStringValue( | 137 forced_extensions.Append(Value::CreateStringValue( |
119 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | 138 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); |
120 forced_extensions.Append(Value::CreateStringValue( | 139 forced_extensions.Append(Value::CreateStringValue( |
121 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahttp#//www.example.com/crx")); | 140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahttp#//www.example.com/crx")); |
122 | 141 |
123 MockExternalPolicyExtensionProviderVisitor mv; | 142 MockExternalPolicyExtensionProviderVisitor mv; |
124 std::set<std::string> empty; | 143 std::set<std::string> empty; |
125 mv.Visit(&forced_extensions, &valid_extensions, empty); | 144 mv.Visit(&forced_extensions, &valid_extensions, empty); |
126 } | 145 } |
OLD | NEW |