18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/bind.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/apps/app_browsertest_util.h"
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/browser_process.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/component_loader.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/extension_browsertest.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/extensions/api/feedback_private.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/test/base/ui_test_utils.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/common/content_switches.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window_registry.h"
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/event_router.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void StopMessageLoopCallback() {
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::MessageLoopForUI::current()->Quit();
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace extensions {
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class FeedbackTest : public ExtensionBrowserTest {
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUp() OVERRIDE {
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    InProcessBrowserTest::SetUp();
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    command_line->AppendSwitch(::switches::kEnableUserMediaScreenCapturing);
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    InProcessBrowserTest::SetUpCommandLine(command_line);
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) protected:
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool IsFeedbackAppAvailable() {
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return extensions::EventRouter::Get(browser()->profile())
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        ->ExtensionHasEventListener(
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            kFeedbackExtensionId,
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            extensions::api::feedback_private::OnFeedbackRequested::kEventName);
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void StartFeedbackUI() {
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    base::Closure callback = base::Bind(&StopMessageLoopCallback);
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    extensions::FeedbackPrivateGetStringsFunction::set_test_callback(&callback);
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    InvokeFeedbackUI();
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    content::RunMessageLoop();
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    extensions::FeedbackPrivateGetStringsFunction::set_test_callback(NULL);
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void VerifyFeedbackAppLaunch() {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AppWindow* window =
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ASSERT_TRUE(window);
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    const Extension* feedback_app = window->GetExtension();
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ASSERT_TRUE(feedback_app);
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    EXPECT_EQ(feedback_app->id(), std::string(kFeedbackExtensionId));
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) private:
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void InvokeFeedbackUI() {
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    extensions::FeedbackPrivateAPI* api =
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        extensions::FeedbackPrivateAPI::GetFactoryInstance()->Get(
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            browser()->profile());
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    api->RequestFeedback("Test description",
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                         "Test tag",
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                         GURL("http://www.test.com"));
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// See http://crbug.com/369886.
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(FeedbackTest, DISABLED_ShowFeedback) {
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WaitForExtensionViewsToLoad();
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(IsFeedbackAppAvailable());
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  StartFeedbackUI();
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  VerifyFeedbackAppLaunch();
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace extensions
90