Home | History | Annotate | Download | only in activity_log
      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.
      4 
      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"
     19 
     20 namespace extensions {
     21 
     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
     25 
     26 class ActivityLogPrerenderTest : public ExtensionApiTest {
     27  protected:
     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->AppendSwitch(switches::kEnableExtensionActivityLogTesting);
     33     command_line->AppendSwitchASCII(switches::kPrerenderMode,
     34                                     switches::kPrerenderModeSwitchValueEnabled);
     35   }
     36 
     37   static void Prerender_Arguments(
     38       const std::string& extension_id,
     39       int port,
     40       scoped_ptr<std::vector<scoped_refptr<Action> > > i) {
     41     // This is to exit RunLoop (base::MessageLoop::current()->Run()) below
     42     base::MessageLoop::current()->PostTask(
     43         FROM_HERE, base::MessageLoop::QuitClosure());
     44 
     45     ASSERT_TRUE(i->size());
     46     scoped_refptr<Action> last = i->front();
     47 
     48     std::string args = base::StringPrintf(
     49         "ID=%s CATEGORY=content_script API= ARGS=[\"/google_cs.js\"] "
     50         "PAGE_URL=http://www.google.com.bo:%d/test.html "
     51         "PAGE_TITLE=\"www.google.com.bo:%d/test.html\" "
     52         "OTHER={\"prerender\":true}",
     53         extension_id.c_str(), port, port);
     54     // TODO: Replace PrintForDebug with field testing
     55     // when this feature will be available
     56     ASSERT_EQ(args, last->PrintForDebug());
     57   }
     58 };
     59 
     60 IN_PROC_BROWSER_TEST_F(ActivityLogPrerenderTest, TestScriptInjected) {
     61   host_resolver()->AddRule("*", "127.0.0.1");
     62   StartEmbeddedTestServer();
     63   int port = embedded_test_server()->port();
     64 
     65   // Get the extension (chrome/test/data/extensions/activity_log)
     66   const Extension* ext =
     67       LoadExtension(test_data_dir_.AppendASCII("activity_log"));
     68   ASSERT_TRUE(ext);
     69 
     70   ActivityLog* activity_log = ActivityLog::GetInstance(profile());
     71   ASSERT_TRUE(activity_log);
     72 
     73   //Disable rate limiting in PrerenderManager
     74   prerender::PrerenderManager* prerender_manager =
     75       prerender::PrerenderManagerFactory::GetForProfile(profile());
     76   ASSERT_TRUE(prerender_manager);
     77   prerender_manager->mutable_config().rate_limit_enabled = false;
     78   // Increase maximum size of prerenderer, otherwise this test fails
     79   // on Windows XP.
     80   prerender_manager->mutable_config().max_bytes = 1000 * 1024 * 1024;
     81 
     82   content::WebContents* web_contents =
     83       browser()->tab_strip_model()->GetActiveWebContents();;
     84   ASSERT_TRUE(web_contents);
     85 
     86   content::WindowedNotificationObserver page_observer(
     87       content::NOTIFICATION_LOAD_STOP,
     88       content::NotificationService::AllSources());
     89 
     90   GURL url(base::StringPrintf(
     91       "http://www.google.com.bo:%d/test.html",
     92       port));
     93 
     94   const gfx::Size kSize(640, 480);
     95   scoped_ptr<prerender::PrerenderHandle> prerender_handle(
     96       prerender_manager->AddPrerenderFromLocalPredictor(
     97           url,
     98           web_contents->GetController().GetDefaultSessionStorageNamespace(),
     99           kSize));
    100 
    101   page_observer.Wait();
    102 
    103   activity_log->GetActions(
    104       ext->id(), 0, base::Bind(
    105           ActivityLogPrerenderTest::Prerender_Arguments, ext->id(), port));
    106 
    107   // Allow invocation of Prerender_Arguments
    108   base::MessageLoop::current()->Run();
    109 }
    110 
    111 }  // namespace extensions
    112