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 3 // found in the LICENSE file. 4 5 #include "base/command_line.h" 6 #include "base/memory/scoped_ptr.h" 7 #include "base/message_loop/message_loop.h" 8 #include "base/run_loop.h" 9 #include "base/synchronization/waitable_event.h" 10 #include "chrome/browser/extensions/activity_log/activity_log.h" 11 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/test_extension_system.h" 13 #include "chrome/browser/prerender/prerender_handle.h" 14 #include "chrome/browser/prerender/prerender_manager.h" 15 #include "chrome/browser/prerender/prerender_manager_factory.h" 16 #include "chrome/common/chrome_constants.h" 17 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/extensions/dom_action_types.h" 19 #include "chrome/common/extensions/extension_builder.h" 20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 21 #include "chrome/test/base/testing_profile.h" 22 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "sql/statement.h" 24 #include "testing/gtest/include/gtest/gtest.h" 25 26 #if defined(OS_CHROMEOS) 27 #include "chrome/browser/chromeos/login/user_manager.h" 28 #include "chrome/browser/chromeos/settings/cros_settings.h" 29 #include "chrome/browser/chromeos/settings/device_settings_service.h" 30 #endif 31 32 namespace { 33 34 const char kExtensionId[] = "abc"; 35 36 } // namespace 37 38 namespace extensions { 39 40 class ActivityLogTest : public ChromeRenderViewHostTestHarness { 41 protected: 42 ActivityLogTest() : saved_cmdline_(CommandLine::NO_PROGRAM) {} 43 44 virtual void SetUp() OVERRIDE { 45 ChromeRenderViewHostTestHarness::SetUp(); 46 #if defined OS_CHROMEOS 47 test_user_manager_.reset(new chromeos::ScopedTestUserManager()); 48 #endif 49 CommandLine command_line(CommandLine::NO_PROGRAM); 50 saved_cmdline_ = *CommandLine::ForCurrentProcess(); 51 CommandLine::ForCurrentProcess()->AppendSwitch( 52 switches::kEnableExtensionActivityLogging); 53 CommandLine::ForCurrentProcess()->AppendSwitch( 54 switches::kEnableExtensionActivityLogTesting); 55 ActivityLog::RecomputeLoggingIsEnabled(true); // Logging now enabled. 56 extension_service_ = static_cast<TestExtensionSystem*>( 57 ExtensionSystem::Get(profile()))->CreateExtensionService 58 (&command_line, base::FilePath(), false); 59 ActivityLog::GetInstance(profile())->Init(); 60 base::RunLoop().RunUntilIdle(); 61 } 62 63 virtual void TearDown() OVERRIDE { 64 #if defined OS_CHROMEOS 65 test_user_manager_.reset(); 66 #endif 67 base::RunLoop().RunUntilIdle(); 68 // Restore the original command line and undo the affects of SetUp(). 69 *CommandLine::ForCurrentProcess() = saved_cmdline_; 70 ActivityLog::RecomputeLoggingIsEnabled(false); // Logging now disabled. 71 ChromeRenderViewHostTestHarness::TearDown(); 72 } 73 74 static void RetrieveActions_LogAndFetchActions( 75 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { 76 ASSERT_EQ(2, static_cast<int>(i->size())); 77 } 78 79 void SetPolicy(bool log_arguments) { 80 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 81 if (log_arguments) 82 activity_log->SetDefaultPolicy(ActivityLogPolicy::POLICY_FULLSTREAM); 83 else 84 activity_log->SetDefaultPolicy(ActivityLogPolicy::POLICY_COUNTS); 85 } 86 87 static void Arguments_Prerender( 88 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { 89 ASSERT_EQ(1U, i->size()); 90 scoped_refptr<Action> last = i->front(); 91 std::string args = 92 "ID=odlameecjipmbmbejkplpemijjgpljce CATEGORY=content_script API= " 93 "ARGS=[\"script\"] PAGE_URL=http://www.google.com/ " 94 "OTHER={\"prerender\":true}"; 95 ASSERT_EQ(args, last->PrintForDebug()); 96 } 97 98 ExtensionService* extension_service_; 99 // Used to preserve a copy of the original command line. 100 // The test framework will do this itself as well. However, by then, 101 // it is too late to call ActivityLog::RecomputeLoggingIsEnabled() in 102 // TearDown(). 103 CommandLine saved_cmdline_; 104 105 #if defined OS_CHROMEOS 106 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 107 chromeos::ScopedTestCrosSettings test_cros_settings_; 108 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_; 109 #endif 110 }; 111 112 TEST_F(ActivityLogTest, Enabled) { 113 ASSERT_TRUE(ActivityLog::IsLogEnabledOnAnyProfile()); 114 } 115 116 TEST_F(ActivityLogTest, Construct) { 117 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 118 ASSERT_TRUE(activity_log->IsLogEnabled()); 119 120 scoped_refptr<Action> action = new Action(kExtensionId, 121 base::Time::Now(), 122 Action::ACTION_API_CALL, 123 "tabs.testMethod"); 124 activity_log->LogAction(action); 125 } 126 127 TEST_F(ActivityLogTest, LogAndFetchActions) { 128 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 129 scoped_ptr<base::ListValue> args(new base::ListValue()); 130 ASSERT_TRUE(activity_log->IsLogEnabled()); 131 132 // Write some API calls 133 scoped_refptr<Action> action = new Action(kExtensionId, 134 base::Time::Now(), 135 Action::ACTION_API_CALL, 136 "tabs.testMethod"); 137 activity_log->LogAction(action); 138 action = new Action(kExtensionId, 139 base::Time::Now(), 140 Action::ACTION_DOM_ACCESS, 141 "document.write"); 142 action->set_page_url(GURL("http://www.google.com")); 143 activity_log->LogAction(action); 144 145 activity_log->GetActions( 146 kExtensionId, 147 0, 148 base::Bind(ActivityLogTest::RetrieveActions_LogAndFetchActions)); 149 } 150 151 TEST_F(ActivityLogTest, LogPrerender) { 152 scoped_refptr<const Extension> extension = 153 ExtensionBuilder() 154 .SetManifest(DictionaryBuilder() 155 .Set("name", "Test extension") 156 .Set("version", "1.0.0") 157 .Set("manifest_version", 2)) 158 .Build(); 159 extension_service_->AddExtension(extension.get()); 160 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 161 ASSERT_TRUE(activity_log->IsLogEnabled()); 162 GURL url("http://www.google.com"); 163 164 prerender::PrerenderManager* prerender_manager = 165 prerender::PrerenderManagerFactory::GetForProfile( 166 Profile::FromBrowserContext(profile())); 167 168 const gfx::Size kSize(640, 480); 169 scoped_ptr<prerender::PrerenderHandle> prerender_handle( 170 prerender_manager->AddPrerenderFromLocalPredictor( 171 url, 172 web_contents()->GetController().GetDefaultSessionStorageNamespace(), 173 kSize)); 174 175 const std::vector<content::WebContents*> contentses = 176 prerender_manager->GetAllPrerenderingContents(); 177 ASSERT_EQ(1U, contentses.size()); 178 content::WebContents *contents = contentses[0]; 179 ASSERT_TRUE(prerender_manager->IsWebContentsPrerendering(contents, NULL)); 180 181 TabHelper::ScriptExecutionObserver::ExecutingScriptsMap executing_scripts; 182 executing_scripts[extension->id()].insert("script"); 183 184 static_cast<TabHelper::ScriptExecutionObserver*>(activity_log)-> 185 OnScriptsExecuted(contents, executing_scripts, 0, url); 186 187 activity_log->GetActions( 188 extension->id(), 0, base::Bind(ActivityLogTest::Arguments_Prerender)); 189 190 prerender_manager->CancelAllPrerenders(); 191 } 192 193 } // namespace extensions 194