1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.deskclock.widget;
18 
19 import android.content.Context;
20 import android.view.View;
21 import android.widget.Button;
22 import android.widget.ImageView;
23 
24 import com.android.deskclock.DeskClockFragment;
25 import com.android.deskclock.FabContainer;
26 
27 /**
28  * DeskClock is the normal container for the fab and its left and right buttons. In order to test
29  * each tab in isolation, tests avoid inflating all of DeskClock and instead set this mock fab
30  * container into the DeskClockFragment under test. It mimics the behavior of a fab container,
31  * albeit without animation, so fragment tests can verify the state of the fab any time they like.
32  */
33 public final class MockFabContainer implements FabContainer {
34 
35     private final DeskClockFragment deskClockFragment;
36 
37     private ImageView fab;
38     private Button leftButton;
39     private Button rightButton;
40 
MockFabContainer(DeskClockFragment fragment, Context context)41     public MockFabContainer(DeskClockFragment fragment, Context context) {
42         deskClockFragment = fragment;
43         fab = new ImageView(context);
44         leftButton = new Button(context);
45         rightButton = new Button(context);
46 
47         updateFab(FabContainer.FAB_AND_BUTTONS_IMMEDIATE);
48 
49         fab.setOnClickListener(new View.OnClickListener() {
50             @Override
51             public void onClick(View view) {
52                 deskClockFragment.onFabClick(fab);
53             }
54         });
55         leftButton.setOnClickListener(new View.OnClickListener() {
56             @Override
57             public void onClick(View view) {
58                 deskClockFragment.onLeftButtonClick(leftButton);
59             }
60         });
61         rightButton.setOnClickListener(new View.OnClickListener() {
62             @Override
63             public void onClick(View view) {
64                 deskClockFragment.onRightButtonClick(rightButton);
65             }
66         });
67     }
68 
69     @Override
updateFab(@pdateFabFlag int updateType)70     public void updateFab(@UpdateFabFlag int updateType) {
71         if ((updateType & FabContainer.FAB_ANIMATION_MASK) != 0) {
72             deskClockFragment.onUpdateFab(fab);
73         }
74         if ((updateType & FabContainer.FAB_REQUEST_FOCUS_MASK) != 0) {
75             fab.requestFocus();
76         }
77         if ((updateType & FabContainer.BUTTONS_ANIMATION_MASK) != 0) {
78             deskClockFragment.onUpdateFabButtons(leftButton, rightButton);
79         }
80         if ((updateType & FabContainer.BUTTONS_DISABLE_MASK) != 0) {
81             leftButton.setClickable(false);
82             rightButton.setClickable(false);
83         }
84     }
85 
getFab()86     public ImageView getFab() {
87         return fab;
88     }
89 
getLeftButton()90     public Button getLeftButton() {
91         return leftButton;
92     }
93 
getRightButton()94     public Button getRightButton() {
95         return rightButton;
96     }
97 }
98