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/strings/stringprintf.h"
6 #include "chrome/browser/extensions/activity_log/activity_log.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_browsertest.h"
9 #include "chrome/browser/prerender/prerender_handle.h"
10 #include "chrome/browser/prerender/prerender_manager.h"
11 #include "chrome/browser/prerender/prerender_manager_factory.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
20 namespace extensions
{
22 // Only the prerender tests are in this file. To add tests for activity
23 // logging please see:
24 // chrome/test/data/extensions/api_test/activity_log_private/README
26 class ActivityLogPrerenderTest
: public ExtensionApiTest
{
28 // Make sure the activity log is turned on.
29 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
30 ExtensionBrowserTest::SetUpCommandLine(command_line
);
31 command_line
->AppendSwitch(switches::kEnableExtensionActivityLogging
);
32 command_line
->AppendSwitchASCII(switches::kPrerenderMode
,
33 switches::kPrerenderModeSwitchValueEnabled
);
36 static void Prerender_Arguments(
37 const std::string
& extension_id
,
39 scoped_ptr
<std::vector
<scoped_refptr
<Action
> > > i
) {
40 // This is to exit RunLoop (base::MessageLoop::current()->Run()) below
41 base::MessageLoop::current()->PostTask(
42 FROM_HERE
, base::MessageLoop::QuitClosure());
44 ASSERT_TRUE(i
->size());
45 scoped_refptr
<Action
> last
= i
->front();
47 ASSERT_EQ(extension_id
, last
->extension_id());
48 ASSERT_EQ(Action::ACTION_CONTENT_SCRIPT
, last
->action_type());
49 ASSERT_EQ("[\"/google_cs.js\"]",
50 ActivityLogPolicy::Util::Serialize(last
->args()));
52 base::StringPrintf("http://www.google.com.bo:%d/test.html", port
),
53 last
->SerializePageUrl());
55 base::StringPrintf("www.google.com.bo:%d/test.html", port
),
57 ASSERT_EQ("{\"prerender\":true}",
58 ActivityLogPolicy::Util::Serialize(last
->other()));
59 ASSERT_EQ("", last
->api_name());
60 ASSERT_EQ("", last
->SerializeArgUrl());
64 IN_PROC_BROWSER_TEST_F(ActivityLogPrerenderTest
, TestScriptInjected
) {
65 host_resolver()->AddRule("*", "127.0.0.1");
66 ASSERT_TRUE(StartEmbeddedTestServer());
67 int port
= embedded_test_server()->port();
69 // Get the extension (chrome/test/data/extensions/activity_log)
70 const Extension
* ext
=
71 LoadExtension(test_data_dir_
.AppendASCII("activity_log"));
74 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile());
75 ASSERT_TRUE(activity_log
);
77 //Disable rate limiting in PrerenderManager
78 prerender::PrerenderManager
* prerender_manager
=
79 prerender::PrerenderManagerFactory::GetForProfile(profile());
80 ASSERT_TRUE(prerender_manager
);
81 prerender_manager
->mutable_config().rate_limit_enabled
= false;
82 // Increase prerenderer limits, otherwise this test fails
84 prerender_manager
->mutable_config().max_bytes
= 1000 * 1024 * 1024;
85 prerender_manager
->mutable_config().time_to_live
=
86 base::TimeDelta::FromMinutes(10);
87 prerender_manager
->mutable_config().abandon_time_to_live
=
88 base::TimeDelta::FromMinutes(10);
90 content::WebContents
* web_contents
=
91 browser()->tab_strip_model()->GetActiveWebContents();;
92 ASSERT_TRUE(web_contents
);
94 content::WindowedNotificationObserver
page_observer(
95 content::NOTIFICATION_LOAD_STOP
,
96 content::NotificationService::AllSources());
98 GURL
url(base::StringPrintf(
99 "http://www.google.com.bo:%d/test.html",
102 const gfx::Size
kSize(640, 480);
103 scoped_ptr
<prerender::PrerenderHandle
> prerender_handle(
104 prerender_manager
->AddPrerenderFromLocalPredictor(
106 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
109 page_observer
.Wait();
111 activity_log
->GetFilteredActions(
119 ActivityLogPrerenderTest::Prerender_Arguments
, ext
->id(), port
));
121 // Allow invocation of Prerender_Arguments
122 base::MessageLoop::current()->Run();
125 } // namespace extensions