1 // Copyright (c) 2013 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/location.h"
6 #include "base/single_thread_task_runner.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "chrome/browser/extensions/activity_log/activity_log.h"
10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_browsertest.h"
12 #include "chrome/browser/prerender/prerender_handle.h"
13 #include "chrome/browser/prerender/prerender_manager.h"
14 #include "chrome/browser/prerender/prerender_manager_factory.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "content/public/test/test_utils.h"
20 #include "net/dns/mock_host_resolver.h"
21 #include "net/test/embedded_test_server/embedded_test_server.h"
23 namespace extensions
{
25 // Only the prerender tests are in this file. To add tests for activity
26 // logging please see:
27 // chrome/test/data/extensions/api_test/activity_log_private/README
29 class ActivityLogPrerenderTest
: public ExtensionApiTest
{
31 // Make sure the activity log is turned on.
32 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
33 ExtensionBrowserTest::SetUpCommandLine(command_line
);
34 command_line
->AppendSwitch(switches::kEnableExtensionActivityLogging
);
35 command_line
->AppendSwitchASCII(switches::kPrerenderMode
,
36 switches::kPrerenderModeSwitchValueEnabled
);
39 static void Prerender_Arguments(
40 const std::string
& extension_id
,
42 scoped_ptr
<std::vector
<scoped_refptr
<Action
> > > i
) {
43 // This is to exit RunLoop (base::MessageLoop::current()->Run()) below
44 base::ThreadTaskRunnerHandle::Get()->PostTask(
45 FROM_HERE
, base::MessageLoop::QuitClosure());
47 ASSERT_TRUE(i
->size());
48 scoped_refptr
<Action
> last
= i
->front();
50 ASSERT_EQ(extension_id
, last
->extension_id());
51 ASSERT_EQ(Action::ACTION_CONTENT_SCRIPT
, last
->action_type());
52 ASSERT_EQ("[\"/google_cs.js\"]",
53 ActivityLogPolicy::Util::Serialize(last
->args()));
55 base::StringPrintf("http://www.google.com.bo:%u/test.html", port
),
56 last
->SerializePageUrl());
58 base::StringPrintf("www.google.com.bo:%u/test.html", port
),
60 ASSERT_EQ("{\"prerender\":true}",
61 ActivityLogPolicy::Util::Serialize(last
->other()));
62 ASSERT_EQ("", last
->api_name());
63 ASSERT_EQ("", last
->SerializeArgUrl());
67 IN_PROC_BROWSER_TEST_F(ActivityLogPrerenderTest
, TestScriptInjected
) {
68 host_resolver()->AddRule("*", "127.0.0.1");
69 ASSERT_TRUE(StartEmbeddedTestServer());
70 uint16 port
= embedded_test_server()->port();
72 // Get the extension (chrome/test/data/extensions/activity_log)
73 const Extension
* ext
=
74 LoadExtension(test_data_dir_
.AppendASCII("activity_log"));
77 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile());
78 ASSERT_TRUE(activity_log
);
80 // Disable rate limiting in PrerenderManager
81 prerender::PrerenderManager
* prerender_manager
=
82 prerender::PrerenderManagerFactory::GetForProfile(profile());
83 ASSERT_TRUE(prerender_manager
);
84 prerender_manager
->mutable_config().rate_limit_enabled
= false;
85 // Increase prerenderer limits, otherwise this test fails
87 prerender_manager
->mutable_config().max_bytes
= 1000 * 1024 * 1024;
88 prerender_manager
->mutable_config().time_to_live
=
89 base::TimeDelta::FromMinutes(10);
90 prerender_manager
->mutable_config().abandon_time_to_live
=
91 base::TimeDelta::FromMinutes(10);
93 content::WebContents
* web_contents
=
94 browser()->tab_strip_model()->GetActiveWebContents();
95 ASSERT_TRUE(web_contents
);
97 content::WindowedNotificationObserver
page_observer(
98 content::NOTIFICATION_LOAD_STOP
,
99 content::NotificationService::AllSources());
101 GURL
url(base::StringPrintf(
102 "http://www.google.com.bo:%u/test.html",
105 const gfx::Size
kSize(640, 480);
106 scoped_ptr
<prerender::PrerenderHandle
> prerender_handle(
107 prerender_manager
->AddPrerenderFromOmnibox(
109 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
112 page_observer
.Wait();
114 activity_log
->GetFilteredActions(
122 ActivityLogPrerenderTest::Prerender_Arguments
, ext
->id(), port
));
124 // Allow invocation of Prerender_Arguments
125 base::MessageLoop::current()->Run();
128 } // namespace extensions