15cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
25cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa# Use of this source code is governed by a BSD-style license that can be
35cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa# found in the LICENSE file.
45cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
55cad69c8edf641cae525ffd3317f6d6cc44b9d53mussaimport os
65cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
78753d83937751e6d2eb2646ea5af68979bb76312Ilja H. Friedelfrom autotest_lib.client.cros.graphics import graphics_utils
85cad69c8edf641cae525ffd3317f6d6cc44b9d53mussafrom autotest_lib.client.cros.video import method_logger
95cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
105cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
115cad69c8edf641cae525ffd3317f6d6cc44b9d53mussaclass ImportScreenShotCapturer(object):
125cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    """
135cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    Captures a screenshot with the required dimensions from a chromebook.
145cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
155cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    Uses utility capture but specifies the geometry/dimensions of final image.
165cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
175cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    We need this so that we can chop off things like browser address bar and
185cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    system task bar that are not only irrelevant to the test but would also add
195cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    noise to the test.
205cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
215cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    """
225cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
235cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
245cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    @method_logger.log
255cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    def __init__(self, destination_dir, screen_height_resolution,
265cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa                 top_pixels_to_crop, bottom_pixels_to_crop):
275cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        self.destination_dir = destination_dir
285cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        self.screen_height_resolution = screen_height_resolution
295cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        self.top_pixels_to_crop = top_pixels_to_crop
305cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        self.bottom_pixels_to_crop = bottom_pixels_to_crop
315cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
325cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
33c95277635c90ee616898d1f547440eff2dafc9cbMussa    def __enter__(self):
34c95277635c90ee616898d1f547440eff2dafc9cbMussa        return self
35c95277635c90ee616898d1f547440eff2dafc9cbMussa
36c95277635c90ee616898d1f547440eff2dafc9cbMussa
375cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    @method_logger.log
385cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa    def capture(self, filename):
395cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        """
405cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        Capture the screenshot.
415cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
425cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        Use pre-configured information to create a geometry that specifies the
435cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        final dimension and position of the image.
445cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
455cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        @param filename: string, the screenshot filename.
465cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
475cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        @returns a complete path to the screenshot generated.
485cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
495cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        """
505cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        fullpath = os.path.join(self.destination_dir, filename)
515cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
525cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        final_height = (self.screen_height_resolution -
535cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa                        self.top_pixels_to_crop - self.bottom_pixels_to_crop)
545cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
558753d83937751e6d2eb2646ea5af68979bb76312Ilja H. Friedel        graphics_utils.take_screenshot_crop_by_height(fullpath,
568753d83937751e6d2eb2646ea5af68979bb76312Ilja H. Friedel                                                      final_height,
578753d83937751e6d2eb2646ea5af68979bb76312Ilja H. Friedel                                                      0,
588753d83937751e6d2eb2646ea5af68979bb76312Ilja H. Friedel                                                      self.top_pixels_to_crop)
595cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa
605cad69c8edf641cae525ffd3317f6d6cc44b9d53mussa        return fullpath
61c95277635c90ee616898d1f547440eff2dafc9cbMussa
62c95277635c90ee616898d1f547440eff2dafc9cbMussa
63c95277635c90ee616898d1f547440eff2dafc9cbMussa    def __exit__(self, exc_type, exc_val, exc_tb):
64c95277635c90ee616898d1f547440eff2dafc9cbMussa        pass
65