summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/platform_app_browsertest_util.cc
blob: 0059e1506535db595e0a4d40a6a29decfde370a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/platform_app_browsertest_util.h"
#include "base/command_line.h"
#include "base/stringprintf.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_tabs_module.h"
#include "chrome/browser/extensions/shell_window_registry.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/extensions/shell_window.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"

using content::WebContents;
using extensions::Extension;

namespace utils = extension_function_test_utils;

void PlatformAppBrowserTest::SetUpCommandLine(
    CommandLine* command_line) {
  ExtensionBrowserTest::SetUpCommandLine(command_line);
  command_line->AppendSwitch(switches::kEnablePlatformApps);
  command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
}

const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
    const char* name) {
  ui_test_utils::WindowedNotificationObserver app_loaded_observer(
      content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
      content::NotificationService::AllSources());

  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
  EXPECT_TRUE(extension);

  application_launch::OpenApplication(
      browser()->profile(),
      extension,
      extension_misc::LAUNCH_NONE,
      GURL(),
      NEW_WINDOW,
      NULL);

  app_loaded_observer.Wait();

  return extension;
}

WebContents* PlatformAppBrowserTest::GetFirstShellWindowWebContents() {
  ShellWindowRegistry* app_registry =
      ShellWindowRegistry::Get(browser()->profile());
  ShellWindowRegistry::const_iterator iter;
  ShellWindowRegistry::ShellWindowSet shell_windows =
      app_registry->shell_windows();
  for (iter = shell_windows.begin(); iter != shell_windows.end(); ++iter) {
    return (*iter)->web_contents();
  }

  return NULL;
}

size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
    const Extension* extension) {
  GetAllWindowsFunction* function = new GetAllWindowsFunction();
  function->set_extension(extension);
  scoped_ptr<base::ListValue> result(utils::ToList(
      utils::RunFunctionAndReturnResult(function, "[]", browser())));
  return result->GetSize();
}

bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
    int window_id,
    const Extension* extension) {
  GetWindowFunction* function = new GetWindowFunction();
  function->set_extension(extension);
  utils::RunFunction(
          function,
          base::StringPrintf("[%u]", window_id),
          browser(),
          utils::NONE);
  return function->GetResultValue() != NULL;
}

size_t PlatformAppBrowserTest::GetShellWindowCount() {
  return ShellWindowRegistry::Get(browser()->profile())->
      shell_windows().size();
}

void PlatformAppBrowserTest::ClearCommandLineArgs() {
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  CommandLine::StringVector args = command_line->GetArgs();
  CommandLine::StringVector argv = command_line->argv();
  for (size_t i = 0; i < args.size(); i++)
    argv.pop_back();
  command_line->InitFromArgv(argv);
}

void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
  ClearCommandLineArgs();
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  FilePath test_doc(test_data_dir_.AppendASCII(test_file));
  test_doc = test_doc.NormalizePathSeparators();
  command_line->AppendArgPath(test_doc);
}

ShellWindow* PlatformAppBrowserTest::CreateShellWindow(
    const Extension* extension) {
  ShellWindow::CreateParams params;
  return ShellWindow::Create(
      browser()->profile(), extension, GURL(""), params);
}

void PlatformAppBrowserTest::CloseShellWindow(ShellWindow* window) {
  ui_test_utils::WindowedNotificationObserver destroyed_observer(
      content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
      content::NotificationService::AllSources());
  window->Close();
  destroyed_observer.Wait();
}