Merge branch 'lamarpavel-test-browser-cache-more'

This commit is contained in:
Florian Bruhin 2015-11-10 18:57:44 +01:00
commit 788ea2720b
4 changed files with 195 additions and 14 deletions

View File

@ -137,9 +137,9 @@ Contributors, sorted by the number of commits in descending order:
* Florian Bruhin
* Antoni Boucher
* Bruno Oliveira
* Lamar Pavel
* Alexander Cogneau
* Martin Tournoij
* Lamar Pavel
* Raphael Pierzina
* Joel Torstensson
* Daniel

View File

@ -65,7 +65,8 @@ class DiskCache(QNetworkDiskCache):
"""Update cache size/activated if the config was changed."""
if (section, option) == ('storage', 'cache-size'):
self.setMaximumCacheSize(config.get('storage', 'cache-size'))
elif (section, option) == ('general', 'private-browsing'):
elif (section, option) == ('general', # pragma: no branch
'private-browsing'):
self._maybe_activate()
def cacheSize(self):

View File

@ -48,6 +48,8 @@ PERFECT_FILES = [
('tests/unit/commands/test_argparser.py',
'qutebrowser/commands/argparser.py'),
('tests/unit/browser/test_cache.py',
'qutebrowser/browser/cache.py'),
('tests/unit/browser/test_cookies.py',
'qutebrowser/browser/cookies.py'),
('tests/unit/browser/test_tabhistory.py',

View File

@ -35,6 +35,55 @@ def preload_cache(cache, url='http://www.example.com/', content=b'foobar'):
cache.insert(device)
def test_cache_config_change_cache_size(config_stub, tmpdir):
"""Change cache size and emit signal to trigger on_config_changed."""
max_cache_size = 1024
config_stub.data = {
'storage': {'cache-size': max_cache_size},
'general': {'private-browsing': False}
}
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.maximumCacheSize() == max_cache_size
config_stub.set('storage', 'cache-size', max_cache_size * 2)
assert disk_cache.maximumCacheSize() == max_cache_size * 2
def test_cache_config_enable_private_browsing(config_stub, tmpdir):
"""Change private-browsing config to True and emit signal."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.cacheSize() == 0
preload_cache(disk_cache)
assert disk_cache.cacheSize() > 0
config_stub.set('general', 'private-browsing', True)
assert disk_cache.cacheSize() == 0
def test_cache_config_disable_private_browsing(config_stub, tmpdir):
"""Change private-browsing config to False and emit signal."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
url = 'http://qutebrowser.org'
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl(url))
assert metadata.isValid()
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.prepare(metadata) is None
config_stub.set('general', 'private-browsing', False)
content = b'cute'
preload_cache(disk_cache, url, content)
assert disk_cache.data(QUrl(url)).readAll() == content
def test_cache_size_leq_max_cache_size(config_stub, tmpdir):
"""Test cacheSize <= MaximumCacheSize when cache is activated."""
limit = 100
@ -54,6 +103,63 @@ def test_cache_size_leq_max_cache_size(config_stub, tmpdir):
assert disk_cache.cacheSize() < limit+100
def test_cache_size_deactivated(config_stub, tmpdir):
"""Confirm that the cache size returns 0 when deactivated."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.cacheSize() == 0
def test_cache_existing_metadata_file(config_stub, tmpdir):
"""Test querying existing meta data file from activated cache."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
url = 'http://qutebrowser.org'
content = b'foobar'
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl(url))
assert metadata.isValid()
disk_cache = cache.DiskCache(str(tmpdir))
device = disk_cache.prepare(metadata)
assert device is not None
device.write(content)
disk_cache.insert(device)
disk_cache.updateMetaData(metadata)
files = list(tmpdir.visit(fil=lambda path: path.isfile()))
assert len(files) == 1
assert disk_cache.fileMetaData(str(files[0])) == metadata
def test_cache_nonexistent_metadata_file(config_stub, tmpdir):
"""Test querying nonexistent meta data file from activated cache."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
disk_cache = cache.DiskCache(str(tmpdir))
cache_file = disk_cache.fileMetaData("nosuchfile")
assert cache_file.isValid() == False
def test_cache_deactivated_metadata_file(config_stub, tmpdir):
"""Test querying meta data file when cache is deactivated."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.fileMetaData("foo") == QNetworkCacheMetaData()
def test_cache_deactivated_private_browsing(config_stub, tmpdir):
"""Test if cache is deactivated in private-browsing mode."""
config_stub.data = {
@ -104,12 +210,15 @@ def test_cache_deactivated_remove_data(config_stub, tmpdir):
assert disk_cache.remove(url) == False
def test_cache_insert_data(tmpdir):
def test_cache_insert_data(config_stub, tmpdir):
"""Test if entries inserted into the cache are actually there."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
url = 'http://qutebrowser.org'
content = b'foobar'
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.cacheSize() == 0
preload_cache(disk_cache, url, content)
@ -118,11 +227,37 @@ def test_cache_insert_data(tmpdir):
assert disk_cache.data(QUrl(url)).readAll() == content
def test_cache_remove_data(tmpdir):
"""Test if a previously inserted entry can be removed from the cache."""
def test_cache_deactivated_insert_data(config_stub, tmpdir):
"""Insert data when cache is deactivated."""
# First create QNetworkDiskCache just to get a valid QIODevice from it
url = 'http://qutebrowser.org'
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl(url))
device = disk_cache.prepare(metadata)
assert device is not None
# Now create a deactivated DiskCache and insert the valid device created
# above (there probably is a better way to get a valid QIODevice...)
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
deactivated_cache = cache.DiskCache(str(tmpdir))
assert deactivated_cache.insert(device) is None
def test_cache_remove_data(config_stub, tmpdir):
"""Test if a previously inserted entry can be removed from the cache."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
url = 'http://qutebrowser.org'
disk_cache = cache.DiskCache(str(tmpdir))
preload_cache(disk_cache, url)
assert disk_cache.cacheSize() > 0
@ -146,14 +281,27 @@ def test_cache_clear_activated(config_stub, tmpdir):
assert disk_cache.cacheSize() == 0
def test_cache_metadata(tmpdir):
def test_cache_clear_deactivated(config_stub, tmpdir):
"""Test method clear() on deactivated cache."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.clear() is None
def test_cache_metadata(config_stub, tmpdir):
"""Ensure that DiskCache.metaData() returns exactly what was inserted."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
url = 'http://qutebrowser.org'
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl(url))
assert metadata.isValid()
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
disk_cache = cache.DiskCache(str(tmpdir))
device = disk_cache.prepare(metadata)
device.write(b'foobar')
disk_cache.insert(device)
@ -161,11 +309,26 @@ def test_cache_metadata(tmpdir):
assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_update_metadata(tmpdir):
"""Test updating the meta data for an existing cache entry."""
def test_cache_deactivated_metadata(config_stub, tmpdir):
"""Test querying metaData() on not activated cache."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
url = 'http://qutebrowser.org'
disk_cache = QNetworkDiskCache()
disk_cache.setCacheDirectory(str(tmpdir))
disk_cache = cache.DiskCache(str(tmpdir))
assert disk_cache.metaData(QUrl(url)) == QNetworkCacheMetaData()
def test_cache_update_metadata(config_stub, tmpdir):
"""Test updating the meta data for an existing cache entry."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': False}
}
url = 'http://qutebrowser.org'
disk_cache = cache.DiskCache(str(tmpdir))
preload_cache(disk_cache, url, b'foo')
assert disk_cache.cacheSize() > 0
@ -176,6 +339,21 @@ def test_cache_update_metadata(tmpdir):
assert disk_cache.metaData(QUrl(url)) == metadata
def test_cache_deactivated_update_metadata(config_stub, tmpdir):
"""Test updating the meta data when cache is not activated."""
config_stub.data = {
'storage': {'cache-size': 1024},
'general': {'private-browsing': True}
}
url = 'http://qutebrowser.org'
disk_cache = cache.DiskCache(str(tmpdir))
metadata = QNetworkCacheMetaData()
metadata.setUrl(QUrl(url))
assert metadata.isValid()
assert disk_cache.updateMetaData(metadata) is None
def test_cache_full(config_stub, tmpdir):
"""Do a sanity test involving everything."""
config_stub.data = {