This source file includes following definitions.
- SetUp
- TearDown
- RecordPortCount
- AddTab
- IN_PROC_BROWSER_TEST_F
#include <mach/mach.h>
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
#include "testing/perf/perf_test.h"
namespace {
class MachPortsTest : public InProcessBrowserTest {
public:
MachPortsTest()
: server_(net::SpawnedTestServer::TYPE_HTTP,
net::SpawnedTestServer::kLocalhost,
base::FilePath(FILE_PATH_LITERAL("data/mach_ports/moz"))) {
}
virtual void SetUp() OVERRIDE {
InProcessBrowserTest::SetUp();
ASSERT_TRUE(server_.Start());
}
virtual void TearDown() OVERRIDE {
std::string ports;
for (std::vector<int>::iterator it = port_counts_.begin();
it != port_counts_.end(); ++it) {
base::StringAppendF(&ports, "%d,", *it);
}
perf_test::PrintResultList("mach_ports", "", "", ports, "ports", true);
InProcessBrowserTest::TearDown();
}
void RecordPortCount() {
mach_port_name_array_t names;
mach_msg_type_number_t names_count = 0;
mach_port_type_array_t types;
mach_msg_type_number_t types_count = 0;
mach_port_t self = mach_task_self();
kern_return_t kr = mach_port_names(self,
&names, &names_count,
&types, &types_count);
ASSERT_EQ(KERN_SUCCESS, kr) << "Failed to get mach_port_names(): "
<< mach_error_string(kr);
ASSERT_EQ(names_count, types_count);
port_counts_.push_back(names_count);
vm_deallocate(self, reinterpret_cast<vm_address_t>(names),
names_count * sizeof(mach_port_name_array_t));
vm_deallocate(self, reinterpret_cast<vm_address_t>(types),
types_count * sizeof(mach_port_type_array_t));
}
void AddTab(const std::string& domain) {
GURL url = server_.GetURL("files/" + domain + "/").Resolve("?skip");
AddTabAtIndex(0, url, content::PageTransition::PAGE_TRANSITION_TYPED);
}
private:
net::SpawnedTestServer server_;
std::vector<int> port_counts_;
};
IN_PROC_BROWSER_TEST_F(MachPortsTest, GetCounts) {
browser()->window()->Show();
RecordPortCount();
AddTab("www.google.com");
RecordPortCount();
AddTab("www.cnn.com");
RecordPortCount();
AddTab("www.nytimes.com");
RecordPortCount();
TabStripModel* tab_strip_model = browser()->tab_strip_model();
int tab_count = tab_strip_model->count();
ASSERT_EQ(4, tab_count);
for (int i = 0; i < tab_count - 1; ++i) {
EXPECT_TRUE(
tab_strip_model->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE));
RecordPortCount();
}
}
}