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

Side by Side Diff: chrome/browser/extensions/events_apitest.cc

Issue 2893693002: Remove NOTIFICATION_EXTENSION_ENABLED. (Closed)
Patch Set: address comments Created 3 years, 6 months 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
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "chrome/browser/extensions/extension_apitest.h" 7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/test/base/ui_test_utils.h"
8 #include "extensions/browser/event_router.h" 10 #include "extensions/browser/event_router.h"
9 #include "extensions/browser/extension_registry.h" 11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/browser/scoped_ignore_content_verifier_for_test.h"
13 #include "extensions/test/result_catcher.h"
10 14
11 namespace extensions { 15 namespace extensions {
12 16
13 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Events) { 17 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Events) {
14 ASSERT_TRUE(RunExtensionTest("events")) << message_; 18 ASSERT_TRUE(RunExtensionTest("events")) << message_;
15 } 19 }
16 20
17 // Tests that events are unregistered when an extension page shuts down. 21 // Tests that events are unregistered when an extension page shuts down.
18 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, EventsAreUnregistered) { 22 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, EventsAreUnregistered) {
19 // In this test, page1.html registers for a number of events, then navigates 23 // In this test, page1.html registers for a number of events, then navigates
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 EXPECT_FALSE(event_router->ExtensionHasEventListener( 62 EXPECT_FALSE(event_router->ExtensionHasEventListener(
59 id, "webNavigation.onBeforeNavigate")); 63 id, "webNavigation.onBeforeNavigate"));
60 EXPECT_FALSE( 64 EXPECT_FALSE(
61 event_router->ExtensionHasEventListener(id, "webNavigation.onCommitted")); 65 event_router->ExtensionHasEventListener(id, "webNavigation.onCommitted"));
62 EXPECT_FALSE(event_router->ExtensionHasEventListener( 66 EXPECT_FALSE(event_router->ExtensionHasEventListener(
63 id, "webNavigation.onDOMContentLoaded")); 67 id, "webNavigation.onDOMContentLoaded"));
64 EXPECT_FALSE( 68 EXPECT_FALSE(
65 event_router->ExtensionHasEventListener(id, "webNavigation.onCompleted")); 69 event_router->ExtensionHasEventListener(id, "webNavigation.onCompleted"));
66 } 70 }
67 71
72 class EventsApiTest : public ExtensionApiTest {
73 public:
74 EventsApiTest() {}
75
76 protected:
77 void SetUpOnMainThread() override {
78 ExtensionApiTest::SetUpOnMainThread();
79 EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
80 }
81
82 struct ExtensionCRXData {
83 std::string unpacked_relative_path;
84 base::FilePath crx_path;
85 explicit ExtensionCRXData(const std::string& unpacked_relative_path)
86 : unpacked_relative_path(unpacked_relative_path) {}
87 };
88
89 void SetUpCRX(const std::string& root_dir,
90 const std::string& pem_filename,
91 std::vector<ExtensionCRXData>* crx_data_list) {
92 const base::FilePath test_dir = test_data_dir_.AppendASCII(root_dir);
93 const base::FilePath pem_path = test_dir.AppendASCII(pem_filename);
94 for (ExtensionCRXData& crx_data : *crx_data_list) {
95 crx_data.crx_path = PackExtensionWithOptions(
96 test_dir.AppendASCII(crx_data.unpacked_relative_path),
97 scoped_temp_dir_.GetPath().AppendASCII(
98 crx_data.unpacked_relative_path + ".crx"),
99 pem_path, base::FilePath());
100 }
101 }
102
103 private:
104 base::ScopedTempDir scoped_temp_dir_;
105 ScopedIgnoreContentVerifierForTest ignore_content_verification_;
106
107 DISALLOW_COPY_AND_ASSIGN(EventsApiTest);
108 };
109
110 // Tests that updating an extension sends runtime.onInstalled event to the
111 // updated extension.
112 IN_PROC_BROWSER_TEST_F(EventsApiTest, ExtensionUpdateSendsOnInstalledEvent) {
113 std::vector<ExtensionCRXData> data;
114 data.emplace_back("v1");
115 data.emplace_back("v2");
116 SetUpCRX("lazy_events/on_installed", "pem.pem", &data);
117
118 ExtensionId extension_id;
119 {
120 // Install version 1 of the extension and expect runtime.onInstalled.
121 ResultCatcher catcher;
122 const int expected_change = 1;
123 const Extension* extension_v1 =
124 InstallExtension(data[0].crx_path, expected_change);
125 extension_id = extension_v1->id();
126 ASSERT_TRUE(extension_v1);
127 EXPECT_TRUE(catcher.GetNextResult());
128 }
129 {
130 // Update to version 2, also expect runtime.onInstalled.
131 ResultCatcher catcher;
132 const int expected_change = 0;
133 const Extension* extension_v2 =
134 UpdateExtension(extension_id, data[1].crx_path, expected_change);
135 ASSERT_TRUE(extension_v2);
136 EXPECT_TRUE(catcher.GetNextResult());
137 }
138 }
139
140 // Tests that if updating an extension makes the extension disabled (due to
141 // permissions increase), then enabling the extension fires runtime.onInstalled
142 // correctly to the updated extension.
143 IN_PROC_BROWSER_TEST_F(EventsApiTest,
144 UpdateDispatchesOnInstalledAfterEnablement) {
145 std::vector<ExtensionCRXData> data;
146 data.emplace_back("v1");
147 data.emplace_back("v2");
148 SetUpCRX("lazy_events/on_installed_permissions_increase", "pem.pem", &data);
149
150 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
151 ExtensionId extension_id;
152 {
153 // Install version 1 of the extension and expect runtime.onInstalled.
154 ResultCatcher catcher;
155 const int expected_change = 1;
156 const Extension* extension_v1 =
157 InstallExtension(data[0].crx_path, expected_change);
158 extension_id = extension_v1->id();
159 ASSERT_TRUE(extension_v1);
160 EXPECT_TRUE(catcher.GetNextResult());
161 }
162 {
163 // Update to version 2, which will be disabled due to permissions increase.
164 ResultCatcher catcher;
165 const int expected_change = -1; // Expect extension to be disabled.
166 ASSERT_FALSE(
167 UpdateExtension(extension_id, data[1].crx_path, expected_change));
168
169 const Extension* extension_v2 =
170 registry->disabled_extensions().GetByID(extension_id);
171 ASSERT_TRUE(extension_v2);
172 // Enable the extension.
173 extension_service()->GrantPermissionsAndEnableExtension(extension_v2);
174 EXPECT_TRUE(catcher.GetNextResult());
175 }
176 }
177
178 // Tests that if an extension's updated version has a new lazy listener, it
179 // fires properly after the update.
180 IN_PROC_BROWSER_TEST_F(EventsApiTest, NewlyIntroducedListener) {
181 std::vector<ExtensionCRXData> data;
182 data.emplace_back("v1");
183 data.emplace_back("v2");
184 SetUpCRX("lazy_events/new_event_in_new_version", "pem.pem", &data);
185
186 ExtensionId extension_id;
187 {
188 // Install version 1 of the extension.
189 ResultCatcher catcher;
190 const int expected_change = 1;
191 const Extension* extension_v1 =
192 InstallExtension(data[0].crx_path, expected_change);
193 EXPECT_TRUE(extension_v1);
194 extension_id = extension_v1->id();
195 ASSERT_TRUE(extension_v1);
196 EXPECT_TRUE(catcher.GetNextResult());
197 }
198 {
199 // Update to version 2, that has tabs.onCreated event listener.
200 ResultCatcher catcher;
201 const int expected_change = 0;
202 const Extension* extension_v2 =
203 UpdateExtension(extension_id, data[1].crx_path, expected_change);
204 ASSERT_TRUE(extension_v2);
205 ui_test_utils::NavigateToURLWithDisposition(
206 browser(), GURL(url::kAboutBlankURL),
207 WindowOpenDisposition::NEW_BACKGROUND_TAB,
208 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
209 // Expect tabs.onCreated to fire.
210 EXPECT_TRUE(catcher.GetNextResult());
211 }
212 }
213
68 } // namespace extensions 214 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698