Skip to content

Commit

Permalink
Clean patch with DCC static content
Browse files Browse the repository at this point in the history
NOTRY=true

Review URL: https://codereview.chromium.org/102593005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@241673 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mkearney@chromium.org committed Dec 18, 2013
1 parent c586216 commit b295a82
Show file tree
Hide file tree
Showing 42 changed files with 3,836 additions and 1,229 deletions.
2 changes: 1 addition & 1 deletion chrome/common/extensions/docs/server2/app.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
application: chrome-apps-doc
version: 2-46-0
version: 3-0-0
runtime: python27
api_version: 1
threadsafe: false
Expand Down
2 changes: 1 addition & 1 deletion chrome/common/extensions/docs/server2/cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ cron:
- description: Repopulates all cached data.
url: /_cron
schedule: every 5 minutes
target: 2-46-0
target: 3-0-0
44 changes: 29 additions & 15 deletions chrome/common/extensions/docs/server2/sidenav_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,33 @@ def _AddLevels(items, level):
_AddLevels(item['items'], level + 1)


def _AddSelected(items, path):
'''Add 'selected' and 'child_selected' properties to |items| so that the
sidenav can be expanded to show which menu item has been selected. Returns
True if an item was marked 'selected'.
def _AddAnnotations(items, path, parent=None):
'''Add 'selected', 'child_selected' and 'related' properties to
|items| so that the sidenav can be expanded to show which menu item has
been selected and the related pages section can be drawn. 'related'
is added to all items with the same parent as the selected item.
If more than one item exactly matches the path, the deepest one is considered
'selected'. A 'parent' property is added to the selected path.
Returns True if an item was marked 'selected'.
'''
for item in items:
if item.get('href', '') == path:
item['selected'] = True
return True
if 'items' in item:
if _AddSelected(item['items'], path):
if _AddAnnotations(item['items'], path, item):
item['child_selected'] = True
return True

if item.get('href', '') == path:
item['selected'] = True
if parent:
item['parent'] = { 'title': parent.get('title', None),
'href': parent.get('href', None) }

for sibling in items:
sibling['related'] = True

return True

return False


Expand Down Expand Up @@ -78,13 +91,14 @@ def _QualifyHrefs(self, items):
item['href'] = self._server_instance.base_path + href

def Cron(self):
futures = [self._cache.GetFromFile('%s/%s_sidenav.json' %
(JSON_TEMPLATES, platform))
for platform in ('apps', 'extensions')]
return Future(delegate=Gettable(lambda: [f.Get() for f in futures]))
return self._cache.GetFromFile('%s/chrome_sidenav.json' % (JSON_TEMPLATES))

def get(self, key):
sidenav = copy.deepcopy(self._cache.GetFromFile(
'%s/%s_sidenav.json' % (JSON_TEMPLATES, key)).Get())
_AddSelected(sidenav, self._server_instance.base_path + self._request.path)
# TODO(mangini/kalman): Use |key| to decide which sidenav to use,
# which will require a more complex Cron method.
sidenav = self._cache.GetFromFile(
'%s/chrome_sidenav.json' % JSON_TEMPLATES).Get()
sidenav = copy.deepcopy(sidenav)
_AddAnnotations(sidenav,
self._server_instance.base_path + self._request.path)
return sidenav
75 changes: 36 additions & 39 deletions chrome/common/extensions/docs/server2/sidenav_data_source_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

import json
import unittest
import copy

from extensions_paths import JSON_TEMPLATES
from mock_file_system import MockFileSystem
from server_instance import ServerInstance
from servlet import Request
from sidenav_data_source import SidenavDataSource, _AddLevels, _AddSelected
from sidenav_data_source import SidenavDataSource, _AddLevels, _AddAnnotations
from test_file_system import TestFileSystem
from test_util import CaptureLogging

Expand Down Expand Up @@ -38,35 +39,30 @@ def testAddLevels(self):
_AddLevels(sidenav_json, 1)
self.assertEqual(expected, sidenav_json)

def testAddSelected(self):
sidenav_json = [
{ 'href': '/AH2.html' },
{
'href': '/H2.html',
'items': [{
'href': '/H3.html'
}]
}
]

expected = [
{ 'href': '/AH2.html' },
{
'child_selected': True,
'href': '/H2.html',
'items': [{
'href': '/H3.html',
'selected': True
}]
}
]

_AddSelected(sidenav_json, '/H3.html')
def testAddAnnotations(self):
item1 = { 'href': '/H1.html' }
item2_1 = { 'href': '/H2_1.html' }
item2_2 = { 'href': '/H2_2.html' }
item2 = { 'href': '/H2.html', 'items': [item2_1, item2_2] }

expected = [ item1, item2 ]

sidenav_json = copy.deepcopy(expected)

item2['child_selected'] = True
item2_1['selected'] = True
item2_1['related'] = True
item2_1['parent'] = { 'title': item2.get('title', None),
'href': item2.get('href', None) }

item2_2['related'] = True

self.assertTrue(_AddAnnotations(sidenav_json, item2_1['href']))
self.assertEqual(expected, sidenav_json)

def testWithDifferentBasePath(self):
file_system = TestFileSystem({
'apps_sidenav.json': json.dumps([
'chrome_sidenav.json': json.dumps([
{ 'href': '/H1.html' },
{ 'href': '/H2.html' },
{ 'href': '/base/path/H2.html' },
Expand All @@ -81,14 +77,14 @@ def testWithDifferentBasePath(self):
}, relative_to=JSON_TEMPLATES)

expected = [
{'href': '/base/path/H1.html', 'level': 2},
{'href': '/base/path/H2.html', 'level': 2, 'selected': True},
{'href': '/base/path/base/path/H2.html', 'level': 2},
{'href': 'https://qualified/X1.html', 'level': 2},
{'href': '/base/path/H1.html', 'level': 2, 'related': True},
{'href': '/base/path/H2.html', 'level': 2, 'selected': True, 'related': True},
{'href': '/base/path/base/path/H2.html', 'level': 2, 'related': True},
{'href': 'https://qualified/X1.html', 'level': 2, 'related': True},
{'items': [
{'href': '/base/path/H4.html', 'level': 3}
],
'href': '/base/path/H3.html', 'level': 2}
'href': '/base/path/H3.html', 'level': 2, 'related': True}
]

server_instance = ServerInstance.ForTest(file_system,
Expand All @@ -97,12 +93,12 @@ def testWithDifferentBasePath(self):
Request.ForTest('/H2.html'))

log_output = CaptureLogging(
lambda: self.assertEqual(expected, sidenav_data_source.get('apps')))
lambda: self.assertEqual(expected, sidenav_data_source.get('chrome')))
self.assertEqual(2, len(log_output))

def testSidenavDataSource(self):
file_system = MockFileSystem(TestFileSystem({
'apps_sidenav.json': json.dumps([{
'chrome_sidenav.json': json.dumps([{
'title': 'H1',
'href': 'H1.html',
'items': [{
Expand All @@ -120,8 +116,10 @@ def testSidenavDataSource(self):
'items': [{
'level': 3,
'selected': True,
'related': True,
'title': 'H2',
'href': '/H2.html'
'href': '/H2.html',
'parent': { 'href': '/H1.html', 'title': 'H1'}
}]
}]

Expand All @@ -130,7 +128,7 @@ def testSidenavDataSource(self):
self.assertTrue(*file_system.CheckAndReset())

log_output = CaptureLogging(
lambda: self.assertEqual(expected, sidenav_data_source.get('apps')))
lambda: self.assertEqual(expected, sidenav_data_source.get('chrome')))

self.assertEqual(1, len(log_output))
self.assertTrue(
Expand All @@ -143,20 +141,19 @@ def testSidenavDataSource(self):

def testCron(self):
file_system = TestFileSystem({
'apps_sidenav.json': '[{ "title": "H1" }]' ,
'extensions_sidenav.json': '[{ "title": "H2" }]'
'chrome_sidenav.json': '[{ "title": "H1" }]'
}, relative_to=JSON_TEMPLATES)

# Ensure Cron doesn't rely on request.
sidenav_data_source = SidenavDataSource(
ServerInstance.ForTest(file_system), request=None)
sidenav_data_source.Cron().Get()

# If Cron fails, apps_sidenav.json will not be cached, and the _cache_data
# If Cron fails, chrome_sidenav.json will not be cached, and the _cache_data
# access will fail.
# TODO(jshumway): Make a non hack version of this check.
sidenav_data_source._cache._file_object_store.Get(
'%s/apps_sidenav.json' % JSON_TEMPLATES).Get()._cache_data
'%s/chrome_sidenav.json' % JSON_TEMPLATES).Get()._cache_data


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit b295a82

Please sign in to comment.