| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import copy | 5 import copy |
| 6 import unittest | 6 import unittest |
| 7 | 7 |
| 8 from content_classification_lens import (ContentClassificationLens, | 8 from content_classification_lens import (ContentClassificationLens, |
| 9 _RulesMatcher) | 9 _RulesMatcher) |
| 10 from request_track import (Request, TimingFromDict) | 10 from request_track import (Request, TimingFromDict) |
| 11 import test_utils | 11 import test_utils |
| 12 | 12 |
| 13 | 13 |
| 14 class ContentClassificationLensTestCase(unittest.TestCase): | 14 class ContentClassificationLensTestCase(unittest.TestCase): |
| 15 _REQUEST = Request.FromJsonDict({'url': 'http://bla.com', | 15 _REQUEST = Request.FromJsonDict({'url': 'http://bla.com', |
| 16 'request_id': '1234.1', | 16 'request_id': '1234.1', |
| 17 'frame_id': '123.1', | 17 'frame_id': '123.1', |
| 18 'initiator': {'type': 'other'}, | 18 'initiator': {'type': 'other'}, |
| 19 'timestamp': 2, | 19 'timestamp': 2, |
| 20 'timing': TimingFromDict({})}) | 20 'timing': TimingFromDict({})}) |
| 21 _MAIN_FRAME_ID = '123.1' | 21 _MAIN_FRAME_ID = '123.1' |
| 22 _PAGE_EVENTS = [{'method': 'Page.frameStartedLoading', | 22 _PAGE_EVENTS = [{'method': 'Page.frameStartedLoading', |
| 23 'frame_id': _MAIN_FRAME_ID}, | 23 'frame_id': _MAIN_FRAME_ID}, |
| 24 {'method': 'Page.frameAttached', | 24 {'method': 'Page.frameAttached', |
| 25 'frame_id': '123.13', 'parent_frame_id': _MAIN_FRAME_ID}] | 25 'frame_id': '123.13', 'parent_frame_id': _MAIN_FRAME_ID}] |
| 26 _RULES = ['bla.com'] | 26 _RULES = ['bla.com'] |
| 27 | 27 |
| 28 def testNoRules(self): |
| 29 trace = test_utils.LoadingTraceFromEvents( |
| 30 [self._REQUEST], self._PAGE_EVENTS) |
| 31 lens = ContentClassificationLens(trace, [], []) |
| 32 self.assertFalse(lens.IsAdRequest(self._REQUEST)) |
| 33 self.assertFalse(lens.IsTrackingRequest(self._REQUEST)) |
| 34 |
| 28 def testAdRequest(self): | 35 def testAdRequest(self): |
| 29 trace = test_utils.LoadingTraceFromEvents( | 36 trace = test_utils.LoadingTraceFromEvents( |
| 30 [self._REQUEST], self._PAGE_EVENTS) | 37 [self._REQUEST], self._PAGE_EVENTS) |
| 31 lens = ContentClassificationLens(trace, self._RULES, []) | 38 lens = ContentClassificationLens(trace, self._RULES, []) |
| 32 self.assertTrue(lens.IsAdRequest(self._REQUEST)) | 39 self.assertTrue(lens.IsAdRequest(self._REQUEST)) |
| 33 self.assertFalse(lens.IsTrackingRequest(self._REQUEST)) | 40 self.assertFalse(lens.IsTrackingRequest(self._REQUEST)) |
| 34 | 41 |
| 35 def testTrackingRequest(self): | 42 def testTrackingRequest(self): |
| 36 trace = test_utils.LoadingTraceFromEvents( | 43 trace = test_utils.LoadingTraceFromEvents( |
| 37 [self._REQUEST], self._PAGE_EVENTS) | 44 [self._REQUEST], self._PAGE_EVENTS) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 def testScriptRule(self): | 83 def testScriptRule(self): |
| 77 matcher = _RulesMatcher([self._SCRIPT_RULE], False) | 84 matcher = _RulesMatcher([self._SCRIPT_RULE], False) |
| 78 request = copy.deepcopy(self._SCRIPT_REQUEST) | 85 request = copy.deepcopy(self._SCRIPT_REQUEST) |
| 79 request.resource_type = 'Stylesheet' | 86 request.resource_type = 'Stylesheet' |
| 80 self.assertFalse(matcher.Matches(request)) | 87 self.assertFalse(matcher.Matches(request)) |
| 81 self.assertTrue(matcher.Matches(self._SCRIPT_REQUEST)) | 88 self.assertTrue(matcher.Matches(self._SCRIPT_REQUEST)) |
| 82 | 89 |
| 83 | 90 |
| 84 if __name__ == '__main__': | 91 if __name__ == '__main__': |
| 85 unittest.main() | 92 unittest.main() |
| OLD | NEW |