Home | History | Annotate | Download | only in commands
      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/prefs/scoped_user_pref_update.h"
      6 #include "chrome/browser/extensions/api/commands/command_service.h"
      7 #include "chrome/browser/extensions/extension_apitest.h"
      8 #include "chrome/browser/extensions/extension_service.h"
      9 #include "chrome/common/pref_names.h"
     10 #include "content/public/test/browser_test.h"
     11 #include "content/public/test/test_utils.h"
     12 #include "extensions/common/manifest_constants.h"
     13 
     14 namespace {
     15 
     16 // Get another command platform, whcih is used for simulating a command has been
     17 // assigned with a shortcut on another platform.
     18 std::string GetAnotherCommandPlatform() {
     19 #if defined(OS_WIN)
     20   return extensions::manifest_values::kKeybindingPlatformMac;
     21 #elif defined(OS_MACOSX)
     22   return extensions::manifest_values::kKeybindingPlatformChromeOs;
     23 #elif defined(OS_CHROMEOS)
     24   return extensions::manifest_values::kKeybindingPlatformLinux;
     25 #elif defined(OS_LINUX)
     26   return extensions::manifest_values::kKeybindingPlatformWin;
     27 #else
     28   return "";
     29 #endif
     30 }
     31 
     32 }  // namespace
     33 
     34 namespace extensions {
     35 
     36 typedef ExtensionApiTest CommandServiceTest;
     37 
     38 IN_PROC_BROWSER_TEST_F(CommandServiceTest, RemoveShortcutSurvivesUpdate) {
     39   base::ScopedTempDir scoped_temp_dir;
     40   EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
     41   base::FilePath pem_path = test_data_dir_.
     42       AppendASCII("keybinding").AppendASCII("keybinding.pem");
     43   base::FilePath path_v1 = PackExtensionWithOptions(
     44       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
     45                     .AppendASCII("v1"),
     46       scoped_temp_dir.path().AppendASCII("v1.crx"),
     47       pem_path,
     48       base::FilePath());
     49   base::FilePath path_v2 = PackExtensionWithOptions(
     50       test_data_dir_.AppendASCII("keybinding").AppendASCII("update")
     51                     .AppendASCII("v2"),
     52       scoped_temp_dir.path().AppendASCII("v2.crx"),
     53       pem_path,
     54       base::FilePath());
     55 
     56   ExtensionService* service = ExtensionSystem::Get(browser()->profile())->
     57       extension_service();
     58   CommandService* command_service = CommandService::Get(browser()->profile());
     59 
     60   const char kId[] = "pgoakhfeplldmjheffidklpoklkppipp";
     61 
     62   // Install v1 of the extension.
     63   ASSERT_TRUE(InstallExtension(path_v1, 1));
     64   EXPECT_TRUE(service->GetExtensionById(kId, false) != NULL);
     65 
     66   // Verify it has a command of Alt+Shift+F.
     67   ui::Accelerator accelerator = command_service->FindCommandByName(
     68       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
     69   EXPECT_EQ(ui::VKEY_F, accelerator.key_code());
     70   EXPECT_FALSE(accelerator.IsCtrlDown());
     71   EXPECT_TRUE(accelerator.IsShiftDown());
     72   EXPECT_TRUE(accelerator.IsAltDown());
     73 
     74   // Remove the keybinding.
     75   command_service->RemoveKeybindingPrefs(
     76       kId, manifest_values::kBrowserActionCommandEvent);
     77 
     78   // Verify it got removed.
     79   accelerator = command_service->FindCommandByName(
     80       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
     81   EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
     82 
     83   // Update to version 2.
     84   EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
     85   EXPECT_TRUE(service->GetExtensionById(kId, false) != NULL);
     86 
     87   // Verify it is still set to nothing.
     88   accelerator = command_service->FindCommandByName(
     89       kId, manifest_values::kBrowserActionCommandEvent).accelerator();
     90   EXPECT_EQ(ui::VKEY_UNKNOWN, accelerator.key_code());
     91 }
     92 
     93 IN_PROC_BROWSER_TEST_F(CommandServiceTest,
     94                        RemoveKeybindingPrefsShouldBePlatformSpecific) {
     95   base::FilePath extension_dir =
     96       test_data_dir_.AppendASCII("keybinding").AppendASCII("basics");
     97   const Extension* extension = InstallExtension(extension_dir, 1);
     98   ASSERT_TRUE(extension);
     99 
    100   DictionaryPrefUpdate updater(browser()->profile()->GetPrefs(),
    101                                prefs::kExtensionCommands);
    102   base::DictionaryValue* bindings = updater.Get();
    103 
    104   // Simulate command |toggle-feature| has been assigned with a shortcut on
    105   // another platform.
    106   std::string anotherPlatformKey = GetAnotherCommandPlatform() + ":Alt+G";
    107   const char kNamedCommandName[] = "toggle-feature";
    108   base::DictionaryValue* keybinding = new base::DictionaryValue();
    109   keybinding->SetString("extension", extension->id());
    110   keybinding->SetString("command_name", kNamedCommandName);
    111   keybinding->SetBoolean("global", false);
    112   bindings->Set(anotherPlatformKey, keybinding);
    113 
    114   CommandService* command_service = CommandService::Get(browser()->profile());
    115   command_service->RemoveKeybindingPrefs(extension->id(), kNamedCommandName);
    116 
    117   // Removal of keybinding preference should be platform-specific, so the key on
    118   // another platform should always remained.
    119   EXPECT_TRUE(bindings->HasKey(anotherPlatformKey));
    120 }
    121 
    122 }  // namespace extensions
    123