Home | History | Annotate | Download | only in extensions
      1 // Copyright 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 // This file contains tests for extension loading, reloading, and
      6 // unloading behavior.
      7 
      8 #include "base/run_loop.h"
      9 #include "base/strings/stringprintf.h"
     10 #include "chrome/browser/extensions/extension_browsertest.h"
     11 #include "chrome/browser/extensions/extension_service.h"
     12 #include "chrome/browser/extensions/extension_system.h"
     13 #include "chrome/browser/extensions/test_extension_dir.h"
     14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     15 #include "chrome/test/base/in_process_browser_test.h"
     16 #include "chrome/test/base/ui_test_utils.h"
     17 #include "net/test/embedded_test_server/embedded_test_server.h"
     18 #include "testing/gmock/include/gmock/gmock.h"
     19 
     20 namespace extensions {
     21 namespace {
     22 
     23 class ExtensionLoadingTest : public ExtensionBrowserTest {
     24 };
     25 
     26 // Check the fix for http://crbug.com/178542.
     27 IN_PROC_BROWSER_TEST_F(ExtensionLoadingTest,
     28                        UpgradeAfterNavigatingFromOverriddenNewTabPage) {
     29   embedded_test_server()->ServeFilesFromDirectory(
     30       base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
     31   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
     32 
     33   TestExtensionDir extension_dir;
     34   const char* manifest_template =
     35       "{"
     36       "  \"name\": \"Overrides New Tab\","
     37       "  \"version\": \"%d\","
     38       "  \"description\": \"Overrides New Tab\","
     39       "  \"manifest_version\": 2,"
     40       "  \"background\": {"
     41       "    \"persistent\": false,"
     42       "    \"scripts\": [\"event.js\"]"
     43       "  },"
     44       "  \"chrome_url_overrides\": {"
     45       "    \"newtab\": \"newtab.html\""
     46       "  }"
     47       "}";
     48   extension_dir.WriteManifest(base::StringPrintf(manifest_template, 1));
     49   extension_dir.WriteFile(FILE_PATH_LITERAL("event.js"), "");
     50   extension_dir.WriteFile(FILE_PATH_LITERAL("newtab.html"),
     51                           "<h1>Overridden New Tab Page</h1>");
     52 
     53   const Extension* new_tab_extension =
     54       InstallExtension(extension_dir.Pack(), 1 /*new install*/);
     55   ASSERT_TRUE(new_tab_extension);
     56 
     57   // Visit the New Tab Page to get a renderer using the extension into history.
     58   ui_test_utils::NavigateToURL(browser(), GURL("chrome://newtab"));
     59 
     60   // Navigate that tab to a non-extension URL to swap out the extension's
     61   // renderer.
     62   const GURL test_link_from_NTP =
     63       embedded_test_server()->GetURL("/README.chromium");
     64   EXPECT_THAT(test_link_from_NTP.spec(), testing::EndsWith("/README.chromium"))
     65       << "Check that the test server started.";
     66   NavigateInRenderer(browser()->tab_strip_model()->GetActiveWebContents(),
     67                      test_link_from_NTP);
     68 
     69   // Increase the extension's version.
     70   extension_dir.WriteManifest(base::StringPrintf(manifest_template, 2));
     71 
     72   // Upgrade the extension.
     73   new_tab_extension = UpdateExtension(
     74       new_tab_extension->id(), extension_dir.Pack(), 0 /*expected upgrade*/);
     75   EXPECT_THAT(new_tab_extension->version()->components(),
     76               testing::ElementsAre(2));
     77 
     78   // The extension takes a couple round-trips to the renderer in order
     79   // to crash, so open a new tab to wait long enough.
     80   AddTabAtIndex(browser()->tab_strip_model()->count(),
     81                 GURL("http://www.google.com/"),
     82                 content::PAGE_TRANSITION_TYPED);
     83 
     84   // Check that the extension hasn't crashed.
     85   ExtensionService* service =
     86       ExtensionSystem::Get(profile())->extension_service();
     87   EXPECT_EQ(0U, service->terminated_extensions()->size());
     88   EXPECT_TRUE(service->extensions()->Contains(new_tab_extension->id()));
     89 }
     90 
     91 }  // namespace
     92 }  // namespace extensions
     93