Home | History | Annotate | Download | only in declarative_content
      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 "chrome/browser/extensions/extension_action.h"
      6 #include "chrome/browser/extensions/extension_action_manager.h"
      7 #include "chrome/browser/extensions/extension_apitest.h"
      8 #include "chrome/browser/extensions/extension_tab_util.h"
      9 #include "chrome/browser/extensions/extension_test_message_listener.h"
     10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     11 #include "chrome/common/extensions/features/feature_channel.h"
     12 #include "content/public/test/browser_test_utils.h"
     13 
     14 namespace extensions {
     15 namespace {
     16 
     17 class DeclarativeContentApiTest : public ExtensionApiTest {
     18  public:
     19   DeclarativeContentApiTest()
     20       // Set the channel to "trunk" since declarativeContent is restricted
     21       // to trunk.
     22       : current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {
     23   }
     24   virtual ~DeclarativeContentApiTest() {}
     25 
     26   extensions::ScopedCurrentChannel current_channel_;
     27 };
     28 
     29 IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, Overview) {
     30   ExtensionTestMessageListener ready("ready", true);
     31   const Extension* extension =
     32       LoadExtension(test_data_dir_.AppendASCII("declarative_content/overview"));
     33   ASSERT_TRUE(extension);
     34   const ExtensionAction* page_action =
     35       ExtensionActionManager::Get(browser()->profile())->
     36       GetPageAction(*extension);
     37   ASSERT_TRUE(page_action);
     38 
     39   ASSERT_TRUE(ready.WaitUntilSatisfied());
     40   content::WebContents* const tab =
     41       browser()->tab_strip_model()->GetWebContentsAt(0);
     42   const int tab_id = ExtensionTabUtil::GetTabId(tab);
     43 
     44   NavigateInRenderer(tab, GURL("http://test1/"));
     45 
     46   // The declarative API should show the page action instantly, rather
     47   // than waiting for the extension to run.
     48   EXPECT_TRUE(page_action->GetIsVisible(tab_id));
     49 
     50   // Make sure leaving a matching page unshows the page action.
     51   NavigateInRenderer(tab, GURL("http://not_checked/"));
     52   EXPECT_FALSE(page_action->GetIsVisible(tab_id));
     53 
     54   // Insert a password field to make sure that's noticed.
     55   ASSERT_TRUE(content::ExecuteScript(
     56       tab, "document.body.innerHTML = '<input type=\"password\">';"));
     57   // Give the mutation observer a chance to run and send back the
     58   // matching-selector update.
     59   ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
     60   EXPECT_TRUE(page_action->GetIsVisible(tab_id));
     61 
     62   // Remove it again to make sure that reverts the action.
     63   ASSERT_TRUE(content::ExecuteScript(
     64       tab, "document.body.innerHTML = 'Hello world';"));
     65   // Give the mutation observer a chance to run and send back the
     66   // matching-selector update.
     67   ASSERT_TRUE(content::ExecuteScript(tab, std::string()));
     68   EXPECT_FALSE(page_action->GetIsVisible(tab_id));
     69 }
     70 
     71 }  // namespace
     72 }  // namespace extensions
     73