1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from telemetry import story
8from telemetry.page import page
9from telemetry.story import story_filter as story_filter_module
10
11
12class FilterTest(unittest.TestCase):
13
14  def setUp(self):
15    story_set = story.StorySet()
16    self.p1 = page.Page(
17      url='file://your/smile/widen.html', page_set=story_set,
18      name='MayYour.smile_widen', labels=['label1', 'label2'])
19    self.p2 = page.Page(
20      url='file://share_a/smile/too.html', page_set=story_set,
21      name='ShareA.smiles_too', labels=['label1'])
22    self.p3 = page.Page(
23      url='file://share_a/smile/too.html', page_set=story_set,
24      labels=['label2'])
25    self.pages = [self.p1, self.p2, self.p3]
26
27  @staticmethod
28  def ProcessCommandLineArgs(parser=None, **kwargs):
29    class Options(object):
30      def __init__(
31          self, story_filter=None, story_filter_exclude=None,
32          story_label_filter=None, story_label_filter_exclude=None):
33        self.story_filter = story_filter
34        self.story_filter_exclude = story_filter_exclude
35        self.story_label_filter = story_label_filter
36        self.story_label_filter_exclude = story_label_filter_exclude
37    story_filter_module.StoryFilter.ProcessCommandLineArgs(
38        parser, Options(**kwargs))
39
40  def PageSelections(self):
41    return [story_filter_module.StoryFilter.IsSelected(p) for p in self.pages]
42
43  def testNoFilterMatchesAll(self):
44    self.ProcessCommandLineArgs()
45    self.assertEquals([True, True, True], self.PageSelections())
46
47  def testBadRegexCallsParserError(self):
48    class MockParserException(Exception):
49      pass
50    class MockParser(object):
51      def error(self, _):
52        raise MockParserException
53    with self.assertRaises(MockParserException):
54      self.ProcessCommandLineArgs(parser=MockParser(), story_filter='+')
55
56  def testUniqueSubstring(self):
57    self.ProcessCommandLineArgs(story_filter='smile_widen')
58    self.assertEquals([True, False, False], self.PageSelections())
59
60  def testSharedSubstring(self):
61    self.ProcessCommandLineArgs(story_filter='smile')
62    self.assertEquals([True, True, True], self.PageSelections())
63
64  def testNoMatch(self):
65    self.ProcessCommandLineArgs(story_filter='frown')
66    self.assertEquals([False, False, False], self.PageSelections())
67
68  def testExclude(self):
69    self.ProcessCommandLineArgs(story_filter_exclude='ShareA')
70    self.assertEquals([True, False, True], self.PageSelections())
71
72  def testExcludeTakesPriority(self):
73    self.ProcessCommandLineArgs(
74        story_filter='smile',
75        story_filter_exclude='wide')
76    self.assertEquals([False, True, True], self.PageSelections())
77
78  def testNoNameMatchesDisplayName(self):
79    self.ProcessCommandLineArgs(story_filter='share_a/smile')
80    self.assertEquals([False, False, True], self.PageSelections())
81
82  def testNoLabelMatch(self):
83    self.ProcessCommandLineArgs(story_label_filter='labelX')
84    self.assertEquals([False, False, False], self.PageSelections())
85
86  def testLabelsAllMatch(self):
87    self.ProcessCommandLineArgs(story_label_filter='label1,label2')
88    self.assertEquals([True, True, True], self.PageSelections())
89
90  def testExcludeLabelTakesPriority(self):
91    self.ProcessCommandLineArgs(
92        story_label_filter='label1',
93        story_label_filter_exclude='label2')
94    self.assertEquals([False, True, False], self.PageSelections())
95