1 /*
2  * Copyright (C) 2014 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 package com.android.monkey;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.log.LogUtil.CLog;
21 
22 /** Test for running monkey test on clockwork. Adds automatic refreshing of the cards. */
23 public class ClockworkRetailMonkeyTest extends MonkeyBase {
24 
25     private Thread mRefreshThread;
26     private CardRefresher mRefresher = new CardRefresher();
27 
28     @Option(name = "card-refresh-interval", description = "How often the cards are refreshed (ms)")
29     private long mCardRefreshInterval = 2 * 60 * 1000;
30 
31     private static final String REFRESH_COMMAND =
32             "am broadcast -a com.google.android.clockwork.home.retail.action.STARTED_RETAIL_DREAM";
33 
34     private class CardRefresher extends Thread {
35 
36         private boolean mCanceled = false;
37 
38         @Override
run()39         public synchronized void run() {
40             while (!mCanceled) {
41                 try {
42                     this.wait(mCardRefreshInterval);
43                     CLog.i("Refreshing cards");
44                     getDevice().executeShellCommand(REFRESH_COMMAND);
45                 } catch (InterruptedException e) {
46                     // ignore
47                 } catch (DeviceNotAvailableException e) {
48                     // ignore
49                 }
50             }
51         }
52 
cancel()53         public synchronized void cancel() {
54             mCanceled = true;
55             this.notifyAll();
56         }
57     }
58 
59     @Override
onMonkeyStart()60     protected void onMonkeyStart() {
61         mRefreshThread = new Thread(mRefresher);
62         mRefreshThread.start();
63     }
64 
65     @Override
onMonkeyFinish()66     protected void onMonkeyFinish() {
67         mRefresher.cancel();
68         try {
69             mRefreshThread.join();
70         } catch (InterruptedException e) {
71             // ignore
72         }
73     }
74 }
75