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
|
// Copyright (c) 2009 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/extension_apitest.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/test/ui_test_utils.h"
namespace {
static const int kTimeoutMs = 60 * 1000; // 1 minute
};
ExtensionApiTest::ResultCatcher::ResultCatcher() {
registrar_.Add(this, NotificationType::EXTENSION_TEST_PASSED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSION_TEST_FAILED,
NotificationService::AllSources());
}
bool ExtensionApiTest::ResultCatcher::GetNextResult() {
// Depending on the tests, multiple results can come in from a single call
// to RunMessageLoop(), so we maintain a queue of results and just pull them
// off as the test calls this, going to the run loop only when the queue is
// empty.
if (!results_.size()) {
MessageLoop::current()->PostDelayedTask(
FROM_HERE, new MessageLoop::QuitTask, kTimeoutMs);
ui_test_utils::RunMessageLoop();
}
if (results_.size()) {
bool ret = results_.front();
results_.pop_front();
message_ = messages_.front();
messages_.pop_front();
return ret;
}
message_ = "No response from message loop.";
return false;
}
void ExtensionApiTest::ResultCatcher::Observe(
NotificationType type, const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSION_TEST_PASSED:
std::cout << "Got EXTENSION_TEST_PASSED notification.\n";
results_.push_back(true);
messages_.push_back("");
MessageLoopForUI::current()->Quit();
break;
case NotificationType::EXTENSION_TEST_FAILED:
std::cout << "Got EXTENSION_TEST_FAILED notification.\n";
results_.push_back(false);
messages_.push_back(*(Details<std::string>(details).ptr()));
MessageLoopForUI::current()->Quit();
break;
default:
NOTREACHED();
}
}
// Load an extension and wait for it to notify of PASSED or FAILED.
bool ExtensionApiTest::RunExtensionTest(const char* extension_name) {
ResultCatcher catcher;
LOG(INFO) << "Running ExtensionApiTest with: " << extension_name;
if (!LoadExtension(test_data_dir_.AppendASCII(extension_name))) {
message_ = "Failed to load extension.";
return false;
}
if (!catcher.GetNextResult()) {
message_ = catcher.message();
return false;
} else {
return true;
}
}
// Test that exactly one extension loaded.
Extension* ExtensionApiTest::GetSingleLoadedExtension() {
ExtensionsService* service = browser()->profile()->GetExtensionsService();
if (service->extensions()->size() != 1u) {
message_ = StringPrintf(
"Expected only one extension to be present. Found %u.",
static_cast<unsigned>(service->extensions()->size()));
return NULL;
}
Extension* extension = service->extensions()->at(0);
if (!extension) {
message_ = "extension pointer is NULL.";
return NULL;
}
return extension;
}
void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) {
ExtensionBrowserTest::SetUpCommandLine(command_line);
test_data_dir_ = test_data_dir_.AppendASCII("api_test");
// Needed for metrics extension API tests.
command_line->AppendSwitch(switches::kEnableMetricsExtensionApi);
}
|