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

Side by Side Diff: chrome/browser/extensions/activity_log/activity_log_unittest.cc

Issue 1871713002: Convert //chrome/browser/extensions from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and fix header Created 4 years, 8 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) 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 #include "chrome/browser/extensions/activity_log/activity_log.h"
6
5 #include <stddef.h> 7 #include <stddef.h>
6 8
9 #include <memory>
10
7 #include "base/command_line.h" 11 #include "base/command_line.h"
8 #include "base/macros.h" 12 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 14 #include "base/run_loop.h"
12 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
13 #include "build/build_config.h" 16 #include "build/build_config.h"
14 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" 17 #include "chrome/browser/extensions/activity_log/activity_action_constants.h"
15 #include "chrome/browser/extensions/activity_log/activity_log.h"
16 #include "chrome/browser/extensions/extension_service.h" 18 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/test_extension_system.h" 19 #include "chrome/browser/extensions/test_extension_system.h"
18 #include "chrome/browser/prerender/prerender_handle.h" 20 #include "chrome/browser/prerender/prerender_handle.h"
19 #include "chrome/browser/prerender/prerender_manager.h" 21 #include "chrome/browser/prerender/prerender_manager.h"
20 #include "chrome/browser/prerender/prerender_manager_factory.h" 22 #include "chrome/browser/prerender/prerender_manager_factory.h"
21 #include "chrome/common/chrome_constants.h" 23 #include "chrome/common/chrome_constants.h"
22 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 25 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
24 #include "chrome/test/base/testing_profile.h" 26 #include "chrome/test/base/testing_profile.h"
25 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 79
78 void TearDown() override { 80 void TearDown() override {
79 #if defined OS_CHROMEOS 81 #if defined OS_CHROMEOS
80 test_user_manager_.reset(); 82 test_user_manager_.reset();
81 #endif 83 #endif
82 base::RunLoop().RunUntilIdle(); 84 base::RunLoop().RunUntilIdle();
83 ChromeRenderViewHostTestHarness::TearDown(); 85 ChromeRenderViewHostTestHarness::TearDown();
84 } 86 }
85 87
86 static void RetrieveActions_LogAndFetchActions0( 88 static void RetrieveActions_LogAndFetchActions0(
87 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { 89 std::unique_ptr<std::vector<scoped_refptr<Action>>> i) {
88 ASSERT_EQ(0, static_cast<int>(i->size())); 90 ASSERT_EQ(0, static_cast<int>(i->size()));
89 } 91 }
90 92
91 static void RetrieveActions_LogAndFetchActions2( 93 static void RetrieveActions_LogAndFetchActions2(
92 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { 94 std::unique_ptr<std::vector<scoped_refptr<Action>>> i) {
93 ASSERT_EQ(2, static_cast<int>(i->size())); 95 ASSERT_EQ(2, static_cast<int>(i->size()));
94 } 96 }
95 97
96 void SetPolicy(bool log_arguments) { 98 void SetPolicy(bool log_arguments) {
97 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 99 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
98 if (log_arguments) 100 if (log_arguments)
99 activity_log->SetDatabasePolicy(ActivityLogPolicy::POLICY_FULLSTREAM); 101 activity_log->SetDatabasePolicy(ActivityLogPolicy::POLICY_FULLSTREAM);
100 else 102 else
101 activity_log->SetDatabasePolicy(ActivityLogPolicy::POLICY_COUNTS); 103 activity_log->SetDatabasePolicy(ActivityLogPolicy::POLICY_COUNTS);
102 } 104 }
103 105
104 bool GetDatabaseEnabled() { 106 bool GetDatabaseEnabled() {
105 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 107 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
106 return activity_log->IsDatabaseEnabled(); 108 return activity_log->IsDatabaseEnabled();
107 } 109 }
108 110
109 bool GetWatchdogActive() { 111 bool GetWatchdogActive() {
110 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 112 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
111 return activity_log->IsWatchdogAppActive(); 113 return activity_log->IsWatchdogAppActive();
112 } 114 }
113 115
114 static void Arguments_Prerender( 116 static void Arguments_Prerender(
115 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { 117 std::unique_ptr<std::vector<scoped_refptr<Action>>> i) {
116 ASSERT_EQ(1U, i->size()); 118 ASSERT_EQ(1U, i->size());
117 scoped_refptr<Action> last = i->front(); 119 scoped_refptr<Action> last = i->front();
118 120
119 ASSERT_EQ("odlameecjipmbmbejkplpemijjgpljce", last->extension_id()); 121 ASSERT_EQ("odlameecjipmbmbejkplpemijjgpljce", last->extension_id());
120 ASSERT_EQ(Action::ACTION_CONTENT_SCRIPT, last->action_type()); 122 ASSERT_EQ(Action::ACTION_CONTENT_SCRIPT, last->action_type());
121 ASSERT_EQ("[\"script\"]", 123 ASSERT_EQ("[\"script\"]",
122 ActivityLogPolicy::Util::Serialize(last->args())); 124 ActivityLogPolicy::Util::Serialize(last->args()));
123 ASSERT_EQ("http://www.google.com/", last->SerializePageUrl()); 125 ASSERT_EQ("http://www.google.com/", last->SerializePageUrl());
124 ASSERT_EQ("{\"prerender\":true}", 126 ASSERT_EQ("{\"prerender\":true}",
125 ActivityLogPolicy::Util::Serialize(last->other())); 127 ActivityLogPolicy::Util::Serialize(last->other()));
126 ASSERT_EQ("", last->api_name()); 128 ASSERT_EQ("", last->api_name());
127 ASSERT_EQ("", last->page_title()); 129 ASSERT_EQ("", last->page_title());
128 ASSERT_EQ("", last->SerializeArgUrl()); 130 ASSERT_EQ("", last->SerializeArgUrl());
129 } 131 }
130 132
131 static void RetrieveActions_ArgUrlExtraction( 133 static void RetrieveActions_ArgUrlExtraction(
132 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { 134 std::unique_ptr<std::vector<scoped_refptr<Action>>> i) {
133 const base::DictionaryValue* other = NULL; 135 const base::DictionaryValue* other = NULL;
134 int dom_verb = -1; 136 int dom_verb = -1;
135 137
136 ASSERT_EQ(4U, i->size()); 138 ASSERT_EQ(4U, i->size());
137 scoped_refptr<Action> action = i->at(0); 139 scoped_refptr<Action> action = i->at(0);
138 ASSERT_EQ("XMLHttpRequest.open", action->api_name()); 140 ASSERT_EQ("XMLHttpRequest.open", action->api_name());
139 ASSERT_EQ("[\"POST\",\"\\u003Carg_url>\"]", 141 ASSERT_EQ("[\"POST\",\"\\u003Carg_url>\"]",
140 ActivityLogPolicy::Util::Serialize(action->args())); 142 ActivityLogPolicy::Util::Serialize(action->args()));
141 ASSERT_EQ("http://api.google.com/", action->arg_url().spec()); 143 ASSERT_EQ("http://api.google.com/", action->arg_url().spec());
142 // Test that the dom_verb field was changed to XHR (from METHOD). This 144 // Test that the dom_verb field was changed to XHR (from METHOD). This
(...skipping 18 matching lines...) Expand all
161 ASSERT_FALSE(action->arg_url().is_valid()); 163 ASSERT_FALSE(action->arg_url().is_valid());
162 164
163 action = i->at(3); 165 action = i->at(3);
164 ASSERT_EQ("windows.create", action->api_name()); 166 ASSERT_EQ("windows.create", action->api_name());
165 ASSERT_EQ("[{\"url\":\"\\u003Carg_url>\"}]", 167 ASSERT_EQ("[{\"url\":\"\\u003Carg_url>\"}]",
166 ActivityLogPolicy::Util::Serialize(action->args())); 168 ActivityLogPolicy::Util::Serialize(action->args()));
167 ASSERT_EQ("http://www.google.co.uk/", action->arg_url().spec()); 169 ASSERT_EQ("http://www.google.co.uk/", action->arg_url().spec());
168 } 170 }
169 171
170 static void RetrieveActions_ArgUrlApiCalls( 172 static void RetrieveActions_ArgUrlApiCalls(
171 scoped_ptr<std::vector<scoped_refptr<Action> > > actions) { 173 std::unique_ptr<std::vector<scoped_refptr<Action>>> actions) {
172 size_t api_calls_size = arraysize(kUrlApiCalls); 174 size_t api_calls_size = arraysize(kUrlApiCalls);
173 const base::DictionaryValue* other = NULL; 175 const base::DictionaryValue* other = NULL;
174 int dom_verb = -1; 176 int dom_verb = -1;
175 177
176 ASSERT_EQ(api_calls_size, actions->size()); 178 ASSERT_EQ(api_calls_size, actions->size());
177 179
178 for (size_t i = 0; i < actions->size(); i++) { 180 for (size_t i = 0; i < actions->size(); i++) {
179 scoped_refptr<Action> action = actions->at(i); 181 scoped_refptr<Action> action = actions->at(i);
180 ASSERT_EQ(kExtensionId, action->extension_id()); 182 ASSERT_EQ(kExtensionId, action->extension_id());
181 ASSERT_EQ(Action::ACTION_DOM_ACCESS, action->action_type()); 183 ASSERT_EQ(Action::ACTION_DOM_ACCESS, action->action_type());
182 ASSERT_EQ(kUrlApiCalls[i], action->api_name()); 184 ASSERT_EQ(kUrlApiCalls[i], action->api_name());
183 ASSERT_EQ("[\"\\u003Carg_url>\"]", 185 ASSERT_EQ("[\"\\u003Carg_url>\"]",
184 ActivityLogPolicy::Util::Serialize(action->args())); 186 ActivityLogPolicy::Util::Serialize(action->args()));
185 ASSERT_EQ("http://www.google.co.uk/", action->arg_url().spec()); 187 ASSERT_EQ("http://www.google.co.uk/", action->arg_url().spec());
186 other = action->other(); 188 other = action->other();
187 ASSERT_TRUE(other); 189 ASSERT_TRUE(other);
188 ASSERT_TRUE( 190 ASSERT_TRUE(
189 other->GetInteger(activity_log_constants::kActionDomVerb, &dom_verb)); 191 other->GetInteger(activity_log_constants::kActionDomVerb, &dom_verb));
190 ASSERT_EQ(DomActionType::SETTER, dom_verb); 192 ASSERT_EQ(DomActionType::SETTER, dom_verb);
191 } 193 }
192 } 194 }
193 195
194 ExtensionService* extension_service_; 196 ExtensionService* extension_service_;
195 197
196 #if defined OS_CHROMEOS 198 #if defined OS_CHROMEOS
197 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 199 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
198 chromeos::ScopedTestCrosSettings test_cros_settings_; 200 chromeos::ScopedTestCrosSettings test_cros_settings_;
199 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; 201 std::unique_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
200 #endif 202 #endif
201 }; 203 };
202 204
203 TEST_F(ActivityLogTest, Construct) { 205 TEST_F(ActivityLogTest, Construct) {
204 ASSERT_TRUE(GetDatabaseEnabled()); 206 ASSERT_TRUE(GetDatabaseEnabled());
205 ASSERT_FALSE(GetWatchdogActive()); 207 ASSERT_FALSE(GetWatchdogActive());
206 } 208 }
207 209
208 TEST_F(ActivityLogTest, LogAndFetchActions) { 210 TEST_F(ActivityLogTest, LogAndFetchActions) {
209 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 211 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
210 scoped_ptr<base::ListValue> args(new base::ListValue()); 212 std::unique_ptr<base::ListValue> args(new base::ListValue());
211 ASSERT_TRUE(GetDatabaseEnabled()); 213 ASSERT_TRUE(GetDatabaseEnabled());
212 214
213 // Write some API calls 215 // Write some API calls
214 scoped_refptr<Action> action = new Action(kExtensionId, 216 scoped_refptr<Action> action = new Action(kExtensionId,
215 base::Time::Now(), 217 base::Time::Now(),
216 Action::ACTION_API_CALL, 218 Action::ACTION_API_CALL,
217 "tabs.testMethod"); 219 "tabs.testMethod");
218 activity_log->LogAction(action); 220 activity_log->LogAction(action);
219 action = new Action(kExtensionId, 221 action = new Action(kExtensionId,
220 base::Time::Now(), 222 base::Time::Now(),
(...skipping 24 matching lines...) Expand all
245 extension_service_->AddExtension(extension.get()); 247 extension_service_->AddExtension(extension.get());
246 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 248 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
247 ASSERT_TRUE(GetDatabaseEnabled()); 249 ASSERT_TRUE(GetDatabaseEnabled());
248 GURL url("http://www.google.com"); 250 GURL url("http://www.google.com");
249 251
250 prerender::PrerenderManager* prerender_manager = 252 prerender::PrerenderManager* prerender_manager =
251 prerender::PrerenderManagerFactory::GetForProfile( 253 prerender::PrerenderManagerFactory::GetForProfile(
252 Profile::FromBrowserContext(profile())); 254 Profile::FromBrowserContext(profile()));
253 255
254 const gfx::Size kSize(640, 480); 256 const gfx::Size kSize(640, 480);
255 scoped_ptr<prerender::PrerenderHandle> prerender_handle( 257 std::unique_ptr<prerender::PrerenderHandle> prerender_handle(
256 prerender_manager->AddPrerenderFromOmnibox( 258 prerender_manager->AddPrerenderFromOmnibox(
257 url, 259 url,
258 web_contents()->GetController().GetDefaultSessionStorageNamespace(), 260 web_contents()->GetController().GetDefaultSessionStorageNamespace(),
259 kSize)); 261 kSize));
260 262
261 const std::vector<content::WebContents*> contentses = 263 const std::vector<content::WebContents*> contentses =
262 prerender_manager->GetAllPrerenderingContents(); 264 prerender_manager->GetAllPrerenderingContents();
263 ASSERT_EQ(1U, contentses.size()); 265 ASSERT_EQ(1U, contentses.size());
264 content::WebContents *contents = contentses[0]; 266 content::WebContents *contents = contentses[0];
265 ASSERT_TRUE(prerender_manager->IsWebContentsPrerendering(contents, NULL)); 267 ASSERT_TRUE(prerender_manager->IsWebContentsPrerendering(contents, NULL));
(...skipping 11 matching lines...) Expand all
277 "", 279 "",
278 "", 280 "",
279 0, 281 0,
280 base::Bind(ActivityLogTest::Arguments_Prerender)); 282 base::Bind(ActivityLogTest::Arguments_Prerender));
281 283
282 prerender_manager->CancelAllPrerenders(); 284 prerender_manager->CancelAllPrerenders();
283 } 285 }
284 286
285 TEST_F(ActivityLogTest, ArgUrlExtraction) { 287 TEST_F(ActivityLogTest, ArgUrlExtraction) {
286 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 288 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
287 scoped_ptr<base::ListValue> args(new base::ListValue()); 289 std::unique_ptr<base::ListValue> args(new base::ListValue());
288 290
289 base::Time now = base::Time::Now(); 291 base::Time now = base::Time::Now();
290 292
291 // Submit a DOM API call which should have its URL extracted into the arg_url 293 // Submit a DOM API call which should have its URL extracted into the arg_url
292 // field. 294 // field.
293 scoped_refptr<Action> action = new Action(kExtensionId, 295 scoped_refptr<Action> action = new Action(kExtensionId,
294 now, 296 now,
295 Action::ACTION_DOM_ACCESS, 297 Action::ACTION_DOM_ACCESS,
296 "XMLHttpRequest.open"); 298 "XMLHttpRequest.open");
297 action->set_page_url(GURL("http://www.google.com/")); 299 action->set_page_url(GURL("http://www.google.com/"));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 scoped_refptr<const Extension> extension = 354 scoped_refptr<const Extension> extension =
353 ExtensionBuilder() 355 ExtensionBuilder()
354 .SetManifest(DictionaryBuilder() 356 .SetManifest(DictionaryBuilder()
355 .Set("name", "Test extension") 357 .Set("name", "Test extension")
356 .Set("version", "1.0.0") 358 .Set("version", "1.0.0")
357 .Set("manifest_version", 2) 359 .Set("manifest_version", 2)
358 .Build()) 360 .Build())
359 .Build(); 361 .Build();
360 362
361 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 363 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
362 scoped_ptr<base::ListValue> args(new base::ListValue()); 364 std::unique_ptr<base::ListValue> args(new base::ListValue());
363 ASSERT_TRUE(GetDatabaseEnabled()); 365 ASSERT_TRUE(GetDatabaseEnabled());
364 366
365 // Write some API calls 367 // Write some API calls
366 scoped_refptr<Action> action = new Action(extension->id(), 368 scoped_refptr<Action> action = new Action(extension->id(),
367 base::Time::Now(), 369 base::Time::Now(),
368 Action::ACTION_API_CALL, 370 Action::ACTION_API_CALL,
369 "tabs.testMethod"); 371 "tabs.testMethod");
370 activity_log->LogAction(action); 372 activity_log->LogAction(action);
371 action = new Action(extension->id(), 373 action = new Action(extension->id(),
372 base::Time::Now(), 374 base::Time::Now(),
373 Action::ACTION_DOM_ACCESS, 375 Action::ACTION_DOM_ACCESS,
374 "document.write"); 376 "document.write");
375 action->set_page_url(GURL("http://www.google.com")); 377 action->set_page_url(GURL("http://www.google.com"));
376 378
377 activity_log->OnExtensionUninstalled( 379 activity_log->OnExtensionUninstalled(
378 NULL, extension.get(), extensions::UNINSTALL_REASON_FOR_TESTING); 380 NULL, extension.get(), extensions::UNINSTALL_REASON_FOR_TESTING);
379 activity_log->GetFilteredActions( 381 activity_log->GetFilteredActions(
380 extension->id(), 382 extension->id(),
381 Action::ACTION_ANY, 383 Action::ACTION_ANY,
382 "", 384 "",
383 "", 385 "",
384 "", 386 "",
385 -1, 387 -1,
386 base::Bind(ActivityLogTest::RetrieveActions_LogAndFetchActions0)); 388 base::Bind(ActivityLogTest::RetrieveActions_LogAndFetchActions0));
387 } 389 }
388 390
389 TEST_F(ActivityLogTest, ArgUrlApiCalls) { 391 TEST_F(ActivityLogTest, ArgUrlApiCalls) {
390 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 392 ActivityLog* activity_log = ActivityLog::GetInstance(profile());
391 scoped_ptr<base::ListValue> args(new base::ListValue()); 393 std::unique_ptr<base::ListValue> args(new base::ListValue());
392 base::Time now = base::Time::Now(); 394 base::Time now = base::Time::Now();
393 int api_calls_size = arraysize(kUrlApiCalls); 395 int api_calls_size = arraysize(kUrlApiCalls);
394 scoped_refptr<Action> action; 396 scoped_refptr<Action> action;
395 397
396 for (int i = 0; i < api_calls_size; i++) { 398 for (int i = 0; i < api_calls_size; i++) {
397 action = new Action(kExtensionId, 399 action = new Action(kExtensionId,
398 now - base::TimeDelta::FromSeconds(i), 400 now - base::TimeDelta::FromSeconds(i),
399 Action::ACTION_DOM_ACCESS, 401 Action::ACTION_DOM_ACCESS,
400 kUrlApiCalls[i]); 402 kUrlApiCalls[i]);
401 action->mutable_args()->AppendString("http://www.google.co.uk"); 403 action->mutable_args()->AppendString("http://www.google.co.uk");
402 action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb, 404 action->mutable_other()->SetInteger(activity_log_constants::kActionDomVerb,
403 DomActionType::SETTER); 405 DomActionType::SETTER);
404 activity_log->LogAction(action); 406 activity_log->LogAction(action);
405 } 407 }
406 408
407 activity_log->GetFilteredActions( 409 activity_log->GetFilteredActions(
408 kExtensionId, 410 kExtensionId,
409 Action::ACTION_ANY, 411 Action::ACTION_ANY,
410 "", 412 "",
411 "", 413 "",
412 "", 414 "",
413 -1, 415 -1,
414 base::Bind(ActivityLogTest::RetrieveActions_ArgUrlApiCalls)); 416 base::Bind(ActivityLogTest::RetrieveActions_ArgUrlApiCalls));
415 } 417 }
416 418
417 } // namespace extensions 419 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698