This source file includes following definitions.
- CreateResource
- SetUpCommandLine
- SetUpOnMainThread
- JSEval
- JSExpect
- JSExpectAsync
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include <string>
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.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/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/test/browser_test_utils.h"
#include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
namespace {
GURL CreateResource(const std::string& content) {
base::FilePath path;
EXPECT_TRUE(base::CreateTemporaryFile(&path));
EXPECT_TRUE(base::WriteFile(path, content.c_str(), content.size()));
return GURL("file:///" + path.AsUTF8Unsafe());
}
class ResourceLoaderBrowserTest : public InProcessBrowserTest {
public:
ResourceLoaderBrowserTest() {}
protected:
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(switches::kDisableWebSecurity);
}
virtual void SetUpOnMainThread() OVERRIDE {
std::string root_page =
"<html>"
"<head>"
"<script>"
" cr = { ui: { login: {} } };"
" cr.define = function (path, builder) {"
" cr.ui.login.ResourceLoader = builder();"
" };"
" $ = document.getElementById.bind(document);"
"</script>"
"<script>";
ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_OOBE_RESOURCE_LOADER_JS).AppendToString(&root_page);
root_page +=
"</script>"
"</head>"
"<body>"
"<div id=\"root\"></div>"
"</body>"
"</html>";
ui_test_utils::NavigateToURL(browser(), CreateResource(root_page));
JSExpect("!!document.querySelector('#root')");
JSEval("ResourceLoader = cr.ui.login.ResourceLoader;");
}
void JSEval(const std::string& script) {
EXPECT_TRUE(content::ExecuteScript(
browser()->tab_strip_model()->GetActiveWebContents(), script));
}
void JSExpect(const std::string& expression) {
bool result;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
browser()->tab_strip_model()->GetActiveWebContents(),
"window.domAutomationController.send(!!(" + expression + "));",
&result));
EXPECT_TRUE(result) << expression;
}
void JSExpectAsync(const std::string& function) {
bool result;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
browser()->tab_strip_model()->GetActiveWebContents(),
"(" + function + ")(function() {"
" window.domAutomationController.send(true);"
"});",
&result));
EXPECT_TRUE(result);
}
private:
DISALLOW_COPY_AND_ASSIGN(ResourceLoaderBrowserTest);
};
IN_PROC_BROWSER_TEST_F(ResourceLoaderBrowserTest, RegisterAssetsTest) {
JSExpect("!ResourceLoader.hasDeferredAssets('foo')");
JSEval("ResourceLoader.registerAssets({ id: 'foo' });");
JSExpect("ResourceLoader.hasDeferredAssets('foo')");
}
IN_PROC_BROWSER_TEST_F(ResourceLoaderBrowserTest, LoadAssetsTest) {
JSEval("stuff = {}");
std::string html_url = CreateResource("<h1 id=\"bar\">foo</h1>").spec();
std::string css_url = CreateResource("h1 { color: red; }").spec();
std::string js_url = CreateResource("stuff.loaded = true;").spec();
JSEval("ResourceLoader.registerAssets({"
" id: 'test-bundle',"
" html: [ { url: '" + html_url + "', targetID: 'root' } ]," +
" css: [ '" + css_url + "' ]," +
" js: [ '" + js_url + "' ]," +
"});");
JSExpect("!ResourceLoader.alreadyLoadedAssets('test-bundle')");
JSExpectAsync("ResourceLoader.loadAssets.bind(null, 'test-bundle')");
JSExpect("ResourceLoader.alreadyLoadedAssets('test-bundle')");
JSExpect("!!document.querySelector('div#root h1#bar')");
JSExpect("stuff.loaded");
JSExpect("!!document.head.querySelector('link').innerHTML.indexOf('red')");
}
}