This source file includes following definitions.
- IsAppInstalledInMainFrame
- IsAppInstalledInIFrame
- IsAppInstalledInFrame
- InstallStateInMainFrame
- InstallStateInIFrame
- InstallStateInFrame
- RunningStateInMainFrame
- RunningStateInIFrame
- RunningStateInFrame
- GetIFrame
- SetUpCommandLine
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include <string>
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest.h"
#include "net/dns/mock_host_resolver.h"
#include "url/gurl.h"
using extensions::Extension;
class ChromeAppAPITest : public ExtensionBrowserTest {
protected:
bool IsAppInstalledInMainFrame() {
return IsAppInstalledInFrame(
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame());
}
bool IsAppInstalledInIFrame() {
return IsAppInstalledInFrame(GetIFrame());
}
bool IsAppInstalledInFrame(content::RenderFrameHost* frame) {
const char kGetAppIsInstalled[] =
"window.domAutomationController.send(window.chrome.app.isInstalled);";
bool result;
CHECK(content::ExecuteScriptAndExtractBool(frame,
kGetAppIsInstalled,
&result));
return result;
}
std::string InstallStateInMainFrame() {
return InstallStateInFrame(
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame());
}
std::string InstallStateInIFrame() {
return InstallStateInFrame(GetIFrame());
}
std::string InstallStateInFrame(content::RenderFrameHost* frame) {
const char kGetAppInstallState[] =
"window.chrome.app.installState("
" function(s) { window.domAutomationController.send(s); });";
std::string result;
CHECK(content::ExecuteScriptAndExtractString(frame,
kGetAppInstallState,
&result));
return result;
}
std::string RunningStateInMainFrame() {
return RunningStateInFrame(
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame());
}
std::string RunningStateInIFrame() {
return RunningStateInFrame(GetIFrame());
}
std::string RunningStateInFrame(content::RenderFrameHost* frame) {
const char kGetAppRunningState[] =
"window.domAutomationController.send("
" window.chrome.app.runningState());";
std::string result;
CHECK(content::ExecuteScriptAndExtractString(frame,
kGetAppRunningState,
&result));
return result;
}
private:
content::RenderFrameHost* GetIFrame() {
return content::FrameMatchingPredicate(
browser()->tab_strip_model()->GetActiveWebContents(),
base::Bind(&content::FrameIsChildOfMainFrame));
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(switches::kAppsCheckoutURL,
"http://checkout.com:");
}
};
#if defined(OS_WIN)
#define MAYBE_IsInstalled DISABLED_IsInstalled
#else
#define MAYBE_IsInstalled IsInstalled
#endif
IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, MAYBE_IsInstalled) {
std::string app_host("app.com");
std::string nonapp_host("nonapp.com");
host_resolver()->AddRule(app_host, "127.0.0.1");
host_resolver()->AddRule(nonapp_host, "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
GURL test_file_url(test_server()->GetURL("extensions/test_file.html"));
GURL::Replacements replace_host;
replace_host.SetHostStr(app_host);
GURL app_url(test_file_url.ReplaceComponents(replace_host));
replace_host.SetHostStr(nonapp_host);
GURL non_app_url(test_file_url.ReplaceComponents(replace_host));
ui_test_utils::NavigateToURL(browser(), app_url);
EXPECT_FALSE(IsAppInstalledInMainFrame());
const Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("app_dot_com_app"));
ASSERT_TRUE(extension);
EXPECT_FALSE(IsAppInstalledInMainFrame());
ui_test_utils::NavigateToURL(browser(), non_app_url);
EXPECT_FALSE(IsAppInstalledInMainFrame());
const char kGetAppDetails[] =
"window.domAutomationController.send("
" JSON.stringify(window.chrome.app.getDetails()));";
std::string result;
ASSERT_TRUE(
content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
kGetAppDetails,
&result));
EXPECT_EQ("null", result);
ui_test_utils::NavigateToURL(browser(), app_url);
EXPECT_TRUE(IsAppInstalledInMainFrame());
ui_test_utils::NavigateToURL(browser(), app_url);
ASSERT_TRUE(
content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
kGetAppDetails,
&result));
scoped_ptr<base::DictionaryValue> app_details(
static_cast<base::DictionaryValue*>(base::JSONReader::Read(result)));
app_details->Remove("id", NULL);
EXPECT_TRUE(app_details.get());
EXPECT_TRUE(app_details->Equals(extension->manifest()->value()));
ASSERT_TRUE(
content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
"window.domAutomationController.send("
" function() {"
" var value = window.chrome.app.isInstalled;"
" window.chrome.app.isInstalled = !value;"
" if (window.chrome.app.isInstalled == value) {"
" return 'true';"
" } else {"
" return 'false';"
" }"
" }()"
");",
&result));
EXPECT_EQ("true", result);
}
IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, GetDetailsForFrame) {
std::string app_host("app.com");
std::string nonapp_host("nonapp.com");
std::string checkout_host("checkout.com");
host_resolver()->AddRule(app_host, "127.0.0.1");
host_resolver()->AddRule(nonapp_host, "127.0.0.1");
host_resolver()->AddRule(checkout_host, "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
GURL test_file_url(test_server()->GetURL(
"files/extensions/get_app_details_for_frame.html"));
GURL::Replacements replace_host;
replace_host.SetHostStr(checkout_host);
GURL checkout_url(test_file_url.ReplaceComponents(replace_host));
replace_host.SetHostStr(app_host);
GURL app_url(test_file_url.ReplaceComponents(replace_host));
const Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("app_dot_com_app"));
ASSERT_TRUE(extension);
ui_test_utils::NavigateToURL(browser(), app_url);
const char kTestUnsuccessfulAccess[] =
"window.domAutomationController.send(window.testUnsuccessfulAccess())";
bool result = false;
ASSERT_TRUE(
content::ExecuteScriptAndExtractBool(
browser()->tab_strip_model()->GetActiveWebContents(),
kTestUnsuccessfulAccess,
&result));
EXPECT_TRUE(result);
ui_test_utils::NavigateToURL(browser(), checkout_url);
const char kGetDetailsForFrame[] =
"window.domAutomationController.send("
" JSON.stringify(chrome.app.getDetailsForFrame(frames[0])))";
std::string json;
ASSERT_TRUE(
content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
kGetDetailsForFrame,
&json));
scoped_ptr<base::DictionaryValue> app_details(
static_cast<base::DictionaryValue*>(base::JSONReader::Read(json)));
app_details->Remove("id", NULL);
EXPECT_TRUE(app_details.get());
EXPECT_TRUE(app_details->Equals(extension->manifest()->value()));
}
IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningState) {
std::string app_host("app.com");
std::string non_app_host("nonapp.com");
host_resolver()->AddRule(app_host, "127.0.0.1");
host_resolver()->AddRule(non_app_host, "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
GURL test_file_url(test_server()->GetURL(
"files/extensions/get_app_details_for_frame.html"));
GURL::Replacements replace_host;
replace_host.SetHostStr(app_host);
GURL app_url(test_file_url.ReplaceComponents(replace_host));
replace_host.SetHostStr(non_app_host);
GURL non_app_url(test_file_url.ReplaceComponents(replace_host));
ui_test_utils::NavigateToURL(browser(), app_url);
EXPECT_EQ("not_installed", InstallStateInMainFrame());
EXPECT_EQ("cannot_run", RunningStateInMainFrame());
EXPECT_FALSE(IsAppInstalledInMainFrame());
const Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("app_dot_com_app"));
ASSERT_TRUE(extension);
EXPECT_EQ("installed", InstallStateInMainFrame());
EXPECT_EQ("ready_to_run", RunningStateInMainFrame());
EXPECT_FALSE(IsAppInstalledInMainFrame());
ui_test_utils::NavigateToURL(browser(), app_url);
EXPECT_EQ("installed", InstallStateInMainFrame());
EXPECT_EQ("running", RunningStateInMainFrame());
EXPECT_TRUE(IsAppInstalledInMainFrame());
browser()->profile()->GetExtensionService()->DisableExtension(
extension->id(), Extension::DISABLE_PERMISSIONS_INCREASE);
ui_test_utils::NavigateToURL(browser(), app_url);
EXPECT_EQ("disabled", InstallStateInMainFrame());
EXPECT_EQ("cannot_run", RunningStateInMainFrame());
EXPECT_FALSE(IsAppInstalledInMainFrame());
browser()->profile()->GetExtensionService()->EnableExtension(extension->id());
EXPECT_EQ("installed", InstallStateInMainFrame());
EXPECT_EQ("ready_to_run", RunningStateInMainFrame());
EXPECT_FALSE(IsAppInstalledInMainFrame());
ui_test_utils::NavigateToURL(browser(), non_app_url);
EXPECT_EQ("not_installed", InstallStateInMainFrame());
EXPECT_EQ("cannot_run", RunningStateInMainFrame());
EXPECT_FALSE(IsAppInstalledInMainFrame());
EXPECT_EQ("installed", InstallStateInIFrame());
EXPECT_EQ("cannot_run", RunningStateInIFrame());
EXPECT_FALSE(IsAppInstalledInIFrame());
}
IN_PROC_BROWSER_TEST_F(ChromeAppAPITest, InstallAndRunningStateFrame) {
std::string app_host("app.com");
std::string non_app_host("nonapp.com");
host_resolver()->AddRule(app_host, "127.0.0.1");
host_resolver()->AddRule(non_app_host, "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
GURL test_file_url(test_server()->GetURL(
"files/extensions/get_app_details_for_frame_reversed.html"));
GURL::Replacements replace_host;
replace_host.SetHostStr(app_host);
GURL app_url(test_file_url.ReplaceComponents(replace_host));
replace_host.SetHostStr(non_app_host);
GURL non_app_url(test_file_url.ReplaceComponents(replace_host));
ui_test_utils::NavigateToURL(browser(), app_url);
EXPECT_EQ("not_installed", InstallStateInIFrame());
EXPECT_EQ("cannot_run", RunningStateInIFrame());
EXPECT_FALSE(IsAppInstalledInIFrame());
}