This source file includes following definitions.
- SetUp
- InitializeSync
- CleanUpOnMainThread
- sync_setup_helper
- SetUpInProcessBrowserTestFixture
- TearDownInProcessBrowserTestFixture
- EnableDNSLookupForThisTest
- DisableDNSLookupForThisTest
- IN_PROC_BROWSER_TEST_F
#include <string>
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/api/push_messaging/sync_setup_helper.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.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/render_frame_host.h"
#include "extensions/common/extension_set.h"
#include "net/dns/mock_host_resolver.h"
namespace {
const char kTestExtensionId[] = "mfaehphpebmlbfdiegjnpidmibldjbjk";
const char kPasswordFileForTest[] = "password-file-for-test";
const char kOverrideUserDataDir[] = "override-user-data-dir";
}
namespace extensions {
class PushMessagingCanaryTest : public ExtensionApiTest {
public:
PushMessagingCanaryTest() {
sync_setup_helper_.reset(new SyncSetupHelper());
}
virtual ~PushMessagingCanaryTest() {
}
virtual void SetUp() OVERRIDE {
CommandLine* command_line = CommandLine::ForCurrentProcess();
ASSERT_TRUE(command_line->HasSwitch(kPasswordFileForTest));
base::FilePath password_file =
command_line->GetSwitchValuePath(kPasswordFileForTest);
ASSERT_TRUE(sync_setup_helper_->ReadPasswordFile(password_file));
const base::FilePath& override_user_data_dir =
command_line->GetSwitchValuePath(kOverrideUserDataDir);
ASSERT_TRUE(!override_user_data_dir.empty());
command_line->AppendSwitchPath(switches::kUserDataDir,
base::FilePath(override_user_data_dir));
LOG(INFO) << "command line file override switch is "
<< override_user_data_dir.value();
ExtensionApiTest::SetUp();
}
void InitializeSync() {
ASSERT_TRUE(sync_setup_helper_->InitializeSync(profile()));
}
virtual void CleanUpOnMainThread() OVERRIDE {
sync_setup_helper_.reset();
}
const SyncSetupHelper* sync_setup_helper() const {
return sync_setup_helper_.get();
}
protected:
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
EnableDNSLookupForThisTest(
new net::RuleBasedHostResolverProc(host_resolver()));
}
virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
DisableDNSLookupForThisTest();
}
void EnableDNSLookupForThisTest(
net::RuleBasedHostResolverProc* host_resolver) {
scoped_refptr<net::RuleBasedHostResolverProc> resolver =
new net::RuleBasedHostResolverProc(host_resolver);
resolver->AllowDirectLookup("*.google.com");
resolver->AllowDirectLookup("*.thawte.com");
resolver->AllowDirectLookup("*.geotrust.com");
resolver->AllowDirectLookup("*.gstatic.com");
resolver->AllowDirectLookup("*.googleapis.com");
mock_host_resolver_override_.reset(
new net::ScopedDefaultHostResolverProc(resolver.get()));
}
void DisableDNSLookupForThisTest() {
mock_host_resolver_override_.reset();
}
private:
scoped_ptr<SyncSetupHelper> sync_setup_helper_;
scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_;
};
IN_PROC_BROWSER_TEST_F(PushMessagingCanaryTest, MANUAL_ReceivesPush) {
InitializeSync();
const ExtensionSet* installed_extensions = extension_service()->extensions();
if (!installed_extensions->Contains(kTestExtensionId)) {
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("push_messaging_canary"));
ASSERT_TRUE(extension);
}
ASSERT_TRUE(installed_extensions->Contains(kTestExtensionId));
ResultCatcher catcher;
catcher.RestrictToProfile(profile());
const Extension* extension =
extension_service()->extensions()->GetByID(kTestExtensionId);
ASSERT_TRUE(extension);
ui_test_utils::NavigateToURL(
browser(), extension->GetResourceURL("push_messaging_canary.html"));
const std::string& client_id = sync_setup_helper()->client_id();
const std::string& client_secret = sync_setup_helper()->client_secret();
const std::string& refresh_token = sync_setup_helper()->refresh_token();
const base::string16& script_string = base::UTF8ToUTF16(base::StringPrintf(
"startTestWithCredentials('%s', '%s', '%s');",
client_id.c_str(), client_secret.c_str(), refresh_token.c_str()));
browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
ExecuteJavaScript(script_string);
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
}