1 // Copyright 2014 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/files/file_path.h"
6 #include "base/path_service.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/extensions/component_loader.h"
9 #include "chrome/browser/extensions/error_console/error_console.h"
10 #include "chrome/browser/extensions/extension_browsertest.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/common/content_switches.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/test/extension_test_message_listener.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 namespace extensions
{
23 static const char* kHotwordHelperExtensionId
=
24 "dnhpdliibojhegemfjheidglijccjfmc";
26 class HotwordBrowserTest
: public ExtensionBrowserTest
{
28 HotwordBrowserTest() : error_console_(NULL
) { }
29 virtual ~HotwordBrowserTest() { }
32 void SetUpInProcessBrowserTestFixture() override
{
33 ExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
35 // Force the VoiceTrigger field trial on to enable the hotword_helper
37 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
38 switches::kForceFieldTrials
, "VoiceTrigger/Install/");
39 // Load the hotword_helper extension.
40 ComponentLoader::EnableBackgroundExtensionsForTesting();
42 // We need to enable the ErrorConsole FeatureSwitch in order to collect
43 // errors. This should be enabled on any channel <= Dev, but let's make
44 // sure (in case a test is running on, e.g., a beta channel).
45 FeatureSwitch::error_console()->SetOverrideValue(
46 FeatureSwitch::OVERRIDE_ENABLED
);
49 void SetUpOnMainThread() override
{
50 ExtensionBrowserTest::SetUpOnMainThread();
52 // Errors are only kept if we have Developer Mode enabled.
53 profile()->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode
, true);
55 error_console_
= ErrorConsole::Get(profile());
56 ASSERT_TRUE(error_console_
);
59 ErrorConsole
* error_console() { return error_console_
; }
62 // Weak reference to the ErrorConsole.
63 ErrorConsole
* error_console_
;
65 DISALLOW_COPY_AND_ASSIGN(HotwordBrowserTest
);
68 // Test we silently capture an exception from a message handler's response
69 // callback. This happens when the caller to chrome.runtime.sendMessage()
70 // doesn't specify a response callback.
71 IN_PROC_BROWSER_TEST_F(HotwordBrowserTest
, MessageSendResponseError
) {
72 // Enable error reporting for the hotword helper extension.
73 error_console()->SetReportingAllForExtension(kHotwordHelperExtensionId
, true);
75 ExtensionTestMessageListener
doneListener("done", false);
76 const Extension
* extension
= extension_service()->GetExtensionById(
77 kHotwordHelperExtensionId
, false);
78 ASSERT_TRUE(extension
);
79 const Extension
* test_extension
= LoadExtension(
80 test_data_dir_
.AppendASCII("hotword"));
81 ASSERT_TRUE(test_extension
);
83 ASSERT_TRUE(doneListener
.WaitUntilSatisfied());
84 ASSERT_TRUE(error_console()->GetErrorsForExtension(extension
->id()).empty());
87 } // namespace extensions